home *** CD-ROM | disk | FTP | other *** search
/ PC World 2004 May / PCWorld_2004-05_cd.bin / komunikace / apache / apache_2.0.48-win32-x86-no_ssl.msi / Data.Cab / F252969_html.xsl < prev    next >
Encoding:
Extensible Markup Language  |  2003-06-30  |  8.9 KB  |  304 lines

  1. <?xml version="1.0"?>
  2. <xsl:stylesheet version="1.0"
  3.               xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  4.                   xmlns="http://www.w3.org/1999/xhtml">
  5.  
  6.  
  7. <!-- ==================================================================== -->
  8. <!-- Ordinary HTML that must be converted to latex                        -->
  9. <!-- ==================================================================== -->
  10.  
  11. <xsl:template match="ul">
  12. <xsl:text>\begin{itemize}
  13. </xsl:text>
  14. <xsl:apply-templates/>
  15. <xsl:text>\end{itemize}
  16. </xsl:text>
  17. </xsl:template>
  18.  
  19. <xsl:template match="ol">
  20. <xsl:text>\begin{enumerate}
  21. </xsl:text>
  22. <xsl:apply-templates/>
  23. <xsl:text>\end{enumerate}
  24. </xsl:text>
  25. </xsl:template>
  26.  
  27. <xsl:template match="li">
  28. <xsl:text>\item </xsl:text>
  29. <xsl:apply-templates/>
  30. <xsl:text>
  31. </xsl:text>
  32. </xsl:template>
  33.  
  34. <xsl:template match="dl">
  35. <xsl:text>\begin{description}
  36. </xsl:text>
  37. <xsl:apply-templates/>
  38. <xsl:text>\end{description}
  39. </xsl:text>
  40. </xsl:template>
  41.  
  42. <xsl:template match="dt">
  43. <xsl:text>\item[</xsl:text><xsl:apply-templates/>
  44. <xsl:text>] </xsl:text>
  45. </xsl:template>
  46.  
  47. <xsl:template match="dd">
  48. <xsl:apply-templates/>
  49. </xsl:template>
  50.  
  51. <!-- Latex doesn't like successive line breaks, so replace any
  52.      sequence of two or more br separated only by white-space with
  53.      one line break followed by smallskips. -->
  54. <xsl:template match="br">
  55. <xsl:choose>
  56. <xsl:when test="name(preceding-sibling::node()[1])='br' or name(preceding-sibling::node()[1])='indent'">
  57. <xsl:text>\smallskip </xsl:text>
  58. </xsl:when>
  59. <xsl:when test="name(preceding-sibling::node()[2])='br' or name(preceding-sibling::node()[2])='indent'">
  60.   <xsl:choose>
  61.   <xsl:when test="normalize-space(preceding-sibling::node()[1])=''">
  62.     <xsl:text>\smallskip </xsl:text>
  63.   </xsl:when>
  64.   <xsl:otherwise>
  65.     <!-- Don't put a line break if we are the last thing -->
  66.     <xsl:if test="not(position()=last()) and not(position()=last()-1 and normalize-space(following-sibling::node()[1])='')">
  67.       <xsl:text>\\ </xsl:text>
  68.     </xsl:if>
  69.   </xsl:otherwise>
  70.   </xsl:choose>
  71. </xsl:when>
  72. <xsl:otherwise>
  73.     <!-- Don't put a line break if we are the last thing -->
  74.     <xsl:if test="not(position()=last()) and not(position()=last()-1 and normalize-space(following-sibling::node()[1])='')">
  75.       <xsl:text>\\ </xsl:text>
  76.     </xsl:if>
  77. </xsl:otherwise>
  78. </xsl:choose>
  79. </xsl:template>
  80.  
  81. <xsl:template match="p">
  82. <xsl:apply-templates/>
  83. <xsl:text>\par
  84. </xsl:text>
  85. </xsl:template>
  86.  
  87. <xsl:template match="code">
  88. <xsl:text>\texttt{</xsl:text>
  89. <xsl:apply-templates/>
  90. <xsl:text>}</xsl:text>
  91. </xsl:template>
  92.  
  93. <xsl:template match="strong">
  94. <xsl:text>\textbf{</xsl:text>
  95. <xsl:apply-templates/>
  96. <xsl:text>}</xsl:text>
  97. </xsl:template>
  98.  
  99. <xsl:template match="em">
  100. <xsl:text>\textit{</xsl:text>
  101. <xsl:apply-templates/>
  102. <xsl:text>}</xsl:text>
  103. </xsl:template>
  104.  
  105. <!-- Value-of used here explicitly because we don't wan't latex-escaping
  106. performed.  Of course, this will conflict with html where some tags are
  107. interpreted in pre -->
  108. <xsl:template match="pre">
  109. <xsl:text>\begin{verbatim}
  110. </xsl:text>
  111. <xsl:value-of select="."/>
  112. <xsl:text>\end{verbatim}
  113. </xsl:text>
  114. </xsl:template>
  115.  
  116. <xsl:template match="blockquote">
  117. <xsl:text>\begin{quotation}
  118. </xsl:text>
  119. <xsl:apply-templates/>
  120. <xsl:text>\end{quotation}
  121. </xsl:text>
  122. </xsl:template>
  123.  
  124. <!-- XXX: We need to deal with table headers -->
  125.  
  126. <xsl:template match="table">
  127. <xsl:variable name="table-type">
  128.   <xsl:choose>
  129.   <xsl:when test="count(tr) > 15">longtable</xsl:when>
  130.   <xsl:otherwise>tabular</xsl:otherwise>
  131.   </xsl:choose>
  132. </xsl:variable>
  133.  
  134. <xsl:text>\begin{</xsl:text><xsl:value-of select="$table-type"/>
  135. <xsl:text>}{|</xsl:text>
  136. <xsl:choose>
  137. <xsl:when test="columnspec">
  138.   <xsl:for-each select="columnspec/column">
  139.     <xsl:text>l</xsl:text>
  140.     <xsl:if test="../../@border and not(position()=last())">
  141.       <xsl:text>|</xsl:text>
  142.     </xsl:if>
  143.   </xsl:for-each>
  144. </xsl:when>
  145. <xsl:otherwise>
  146.   <xsl:for-each select="tr[1]/*">
  147.     <xsl:text>l</xsl:text>
  148.     <xsl:if test="../../@border and not(position()=last())">
  149.       <xsl:text>|</xsl:text>
  150.     </xsl:if>
  151.   </xsl:for-each>
  152. </xsl:otherwise>
  153. </xsl:choose>
  154. <xsl:text>|}\hline
  155. </xsl:text>
  156. <xsl:apply-templates select="tr"/>
  157. <xsl:text>\hline\end{</xsl:text>
  158. <xsl:value-of select="$table-type"/>
  159. <xsl:text>}
  160. </xsl:text>
  161. </xsl:template>
  162.  
  163. <xsl:template match="tr">
  164.   <xsl:apply-templates select="td|th"/>
  165.   <xsl:text>\\</xsl:text>
  166.   <xsl:if test="../@border and not(position()=last())">
  167.     <xsl:text>\hline</xsl:text>
  168.   </xsl:if>
  169.   <xsl:text>
  170. </xsl:text>
  171. </xsl:template>
  172.  
  173. <xsl:template match="td">
  174.     <xsl:variable name="pos" select="position()"/>
  175.     <xsl:text>\begin{minipage}[t]{</xsl:text>
  176.     <xsl:choose>
  177.     <xsl:when test="../../columnspec">
  178.       <xsl:value-of select="../../columnspec/column[$pos]/@width"/>
  179.     </xsl:when>
  180.     <xsl:otherwise>
  181.       <xsl:value-of select=".95 div last()"/>
  182.     </xsl:otherwise>
  183.     </xsl:choose>
  184.     <xsl:text>\textwidth}\small </xsl:text>
  185.     <xsl:apply-templates/>
  186.     <xsl:text>\end{minipage}</xsl:text>
  187.     <xsl:if test="not(position()=last())">
  188.       <xsl:text> & </xsl:text>
  189.     </xsl:if>
  190. </xsl:template>
  191.  
  192. <xsl:template match="th">
  193.     <xsl:variable name="pos" select="position()"/>
  194.     <xsl:text>\begin{minipage}[t]{</xsl:text>
  195.     <xsl:choose>
  196.     <xsl:when test="../../columnspec">
  197.       <xsl:value-of select="../../columnspec/column[$pos]/@width"/>
  198.     </xsl:when>
  199.     <xsl:otherwise>
  200.       <xsl:value-of select=".95 div last()"/>
  201.     </xsl:otherwise>
  202.     </xsl:choose>
  203.     <xsl:text>\textwidth}\bfseries </xsl:text>
  204.     <xsl:apply-templates/>
  205.     <xsl:text>\end{minipage}</xsl:text>
  206.     <xsl:if test="not(position()=last())">
  207.       <xsl:text> & </xsl:text>
  208.     </xsl:if>
  209. </xsl:template>
  210.  
  211. <!--
  212.    This is a horrible hack, but it seems to mostly work.  It does a
  213.    few things:
  214.  
  215.    1. Transforms references starting in http:// to footnotes with the
  216.       appropriate hyperref macro to make them clickable.  (This needs
  217.       to be expanded to deal with news: and needs to be adjusted to
  218.       deal with "#", which is creating bad links at the moment.)
  219.  
  220.    2. For intra-document references, constructs the appropriate absolute
  221.       reference using a latex \pageref.  
  222.       This involves applying a simplified version of the
  223.       general URL resolution rules to deal with ../.  It only works for
  224.       one level of subdirectory.
  225.  
  226.    3. It is also necessary to deal with the fact that index pages
  227.       get references as "/".
  228. -->
  229. <xsl:template match="a">
  230. <xsl:apply-templates/>
  231. <xsl:if test="@href">
  232. <xsl:variable name="relpath" select="document(/*/@metafile)/metafile/relpath" />
  233. <xsl:variable name="path" select="document(/*/@metafile)/metafile/path" />
  234. <xsl:variable name="fileref">
  235.   <xsl:choose>
  236.   <xsl:when test="contains(@href, '.html')">
  237.     <xsl:value-of select="substring-before(@href, '.html')"/>
  238.   </xsl:when>
  239.   <xsl:otherwise>
  240.     <xsl:value-of select="concat(@href, 'index')"/>
  241.   </xsl:otherwise>
  242.   </xsl:choose>
  243. </xsl:variable>
  244. <xsl:choose>
  245.  
  246. <xsl:when test="starts-with(@href, 'http:') or starts-with(@href, 'news:') or starts-with(@href, 'mailto:')">
  247.   <xsl:if test="not(.=@href)">
  248.     <xsl:text>\footnote{</xsl:text>
  249.       <xsl:text>\href{</xsl:text>
  250.       <xsl:call-template name="replace-string">
  251.         <xsl:with-param name="text" select="@href"/>
  252.         <xsl:with-param name="replace" select="'#'"/>
  253.         <xsl:with-param name="with" select="'\#'"/>
  254.       </xsl:call-template>
  255.       <xsl:text>}{</xsl:text>
  256.     <xsl:call-template name="ltescape">
  257.       <xsl:with-param name="string" select="@href"/>
  258.     </xsl:call-template>
  259.     <xsl:text>}}</xsl:text>
  260.   </xsl:if>
  261. </xsl:when>
  262. <xsl:when test="starts-with(@href, '#')">
  263. <!-- Don't do inter-section references -->
  264. </xsl:when>
  265. <xsl:otherwise>
  266.   <xsl:text> (p.\ \pageref{</xsl:text>
  267.     <xsl:call-template name="replace-string">
  268.       <xsl:with-param name="replace" select="'#'"/>
  269.       <xsl:with-param name="with" select="':'"/>
  270.       <xsl:with-param name="text">
  271.       <xsl:choose>
  272.       <xsl:when test="$relpath='.'">
  273.         <xsl:value-of select="concat('/',$fileref)"/>
  274.       </xsl:when>
  275.       <xsl:otherwise>
  276.         <xsl:choose>
  277.         <xsl:when test="starts-with($fileref,'..')">
  278.           <xsl:value-of select="substring-after($fileref,'..')"/>
  279.         </xsl:when>
  280.         <xsl:otherwise>
  281.           <xsl:value-of select="concat($path,$fileref)"/>
  282.         </xsl:otherwise>
  283.         </xsl:choose>
  284.       </xsl:otherwise>
  285.       </xsl:choose>
  286.       </xsl:with-param>
  287.      </xsl:call-template>
  288.   <xsl:text>}) </xsl:text>
  289. </xsl:otherwise>
  290. </xsl:choose>
  291. </xsl:if>
  292. </xsl:template>
  293.  
  294. <xsl:template match="img">
  295. <xsl:text>[Image not coverted]</xsl:text>
  296. <!--
  297. <xsl:variable name="path" select="document(/*/@metafile)/metafile/path" />
  298. <xsl:text>\includegraphics{</xsl:text>
  299. <xsl:value-of select="concat('.',$path,@src)"/>
  300. <xsl:text>}</xsl:text>
  301. -->
  302. </xsl:template>
  303.  
  304. </xsl:stylesheet>