html-rtf.xsl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?xml version="1.0" encoding="ASCII"?>
  2. <!--This file was created automatically by html2xhtml-->
  3. <!--from the HTML stylesheets.-->
  4. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" xmlns:set="http://exslt.org/sets" xmlns="http://www.w3.org/1999/xhtml" exclude-result-prefixes="exsl set" version="1.0">
  5. <!-- ********************************************************************
  6. $Id: html-rtf.xsl 8345 2009-03-16 06:44:07Z bobstayton $
  7. ********************************************************************
  8. This file is part of the XSL DocBook Stylesheet distribution.
  9. See ../README or http://docbook.sf.net/release/xsl/current/ for
  10. copyright and other information.
  11. ******************************************************************** -->
  12. <!-- ==================================================================== -->
  13. <!-- This module contains templates that match against HTML nodes. It is used
  14. to post-process result tree fragments for some sorts of cleanup.
  15. These templates can only ever be fired by a processor that supports
  16. exslt:node-set(). -->
  17. <!-- ==================================================================== -->
  18. <!-- insert.html.p mode templates insert a particular RTF at the beginning
  19. of the first paragraph in the primary RTF. -->
  20. <xsl:template match="/" mode="insert.html.p">
  21. <xsl:param name="mark" select="'?'"/>
  22. <xsl:apply-templates mode="insert.html.p">
  23. <xsl:with-param name="mark" select="$mark"/>
  24. </xsl:apply-templates>
  25. </xsl:template>
  26. <xsl:template match="*" mode="insert.html.p">
  27. <xsl:param name="mark" select="'?'"/>
  28. <xsl:copy>
  29. <xsl:copy-of select="@*"/>
  30. <xsl:apply-templates mode="insert.html.p">
  31. <xsl:with-param name="mark" select="$mark"/>
  32. </xsl:apply-templates>
  33. </xsl:copy>
  34. </xsl:template>
  35. <xsl:template xmlns:html="http://www.w3.org/1999/xhtml" match="html:p|p" mode="insert.html.p">
  36. <xsl:param name="mark" select="'?'"/>
  37. <xsl:copy>
  38. <xsl:copy-of select="@*"/>
  39. <xsl:if test="not(preceding::p|preceding::html:p)">
  40. <xsl:copy-of select="$mark"/>
  41. </xsl:if>
  42. <xsl:apply-templates mode="insert.html.p">
  43. <xsl:with-param name="mark" select="$mark"/>
  44. </xsl:apply-templates>
  45. </xsl:copy>
  46. </xsl:template>
  47. <xsl:template match="text()|processing-instruction()|comment()" mode="insert.html.p">
  48. <xsl:param name="mark" select="'?'"/>
  49. <xsl:copy/>
  50. </xsl:template>
  51. <!-- ==================================================================== -->
  52. <!-- insert.html.text mode templates insert a particular RTF at the beginning
  53. of the first text-node in the primary RTF. -->
  54. <xsl:template match="/" mode="insert.html.text">
  55. <xsl:param name="mark" select="'?'"/>
  56. <xsl:apply-templates mode="insert.html.text">
  57. <xsl:with-param name="mark" select="$mark"/>
  58. </xsl:apply-templates>
  59. </xsl:template>
  60. <xsl:template match="*" mode="insert.html.text">
  61. <xsl:param name="mark" select="'?'"/>
  62. <xsl:copy>
  63. <xsl:copy-of select="@*"/>
  64. <xsl:apply-templates mode="insert.html.text">
  65. <xsl:with-param name="mark" select="$mark"/>
  66. </xsl:apply-templates>
  67. </xsl:copy>
  68. </xsl:template>
  69. <xsl:template match="text()|processing-instruction()|comment()" mode="insert.html.text">
  70. <xsl:param name="mark" select="'?'"/>
  71. <xsl:if test="not(preceding::text())">
  72. <xsl:copy-of select="$mark"/>
  73. </xsl:if>
  74. <xsl:copy/>
  75. </xsl:template>
  76. <xsl:template match="processing-instruction()|comment()" mode="insert.html.text">
  77. <xsl:param name="mark" select="'?'"/>
  78. <xsl:copy/>
  79. </xsl:template>
  80. <!-- ==================================================================== -->
  81. <!-- unwrap.p mode templates remove blocks from HTML p elements (and
  82. other places where blocks aren't allowed) -->
  83. <xsl:template name="unwrap.p">
  84. <xsl:param name="p"/>
  85. <xsl:choose>
  86. <xsl:when test="$exsl.node.set.available != 0 and function-available('set:leading') and function-available('set:trailing')">
  87. <xsl:apply-templates select="exsl:node-set($p)" mode="unwrap.p"/>
  88. </xsl:when>
  89. <xsl:otherwise>
  90. <xsl:copy-of select="$p"/>
  91. </xsl:otherwise>
  92. </xsl:choose>
  93. </xsl:template>
  94. <xsl:template xmlns:html="http://www.w3.org/1999/xhtml" match="html:p|p" mode="unwrap.p">
  95. <!-- xmlns:html is necessary for the xhtml stylesheet case -->
  96. <xsl:variable name="blocks" select="address|blockquote|div|hr|h1|h2|h3|h4|h5|h6 |layer|p|pre|table|dl|menu|ol|ul|form |html:address|html:blockquote|html:div|html:hr |html:h1|html:h2|html:h3|html:h4|html:h5|html:h6 |html:layer|html:p|html:pre|html:table|html:dl |html:menu|html:ol|html:ul|html:form"/>
  97. <xsl:choose>
  98. <xsl:when test="$blocks">
  99. <xsl:call-template name="unwrap.p.nodes">
  100. <xsl:with-param name="wrap" select="."/>
  101. <xsl:with-param name="first" select="1"/>
  102. <xsl:with-param name="nodes" select="node()"/>
  103. <xsl:with-param name="blocks" select="$blocks"/>
  104. </xsl:call-template>
  105. </xsl:when>
  106. <xsl:otherwise>
  107. <xsl:copy>
  108. <xsl:copy-of select="@*"/>
  109. <xsl:apply-templates mode="unwrap.p"/>
  110. </xsl:copy>
  111. </xsl:otherwise>
  112. </xsl:choose>
  113. </xsl:template>
  114. <xsl:template match="*" mode="unwrap.p">
  115. <xsl:copy>
  116. <xsl:copy-of select="@*"/>
  117. <xsl:apply-templates mode="unwrap.p"/>
  118. </xsl:copy>
  119. </xsl:template>
  120. <xsl:template match="text()|processing-instruction()|comment()" mode="unwrap.p">
  121. <xsl:copy/>
  122. </xsl:template>
  123. <xsl:template name="unwrap.p.nodes">
  124. <xsl:param name="wrap" select="."/>
  125. <xsl:param name="first" select="0"/>
  126. <xsl:param name="nodes"/>
  127. <xsl:param name="blocks"/>
  128. <xsl:variable name="block" select="$blocks[1]"/>
  129. <!-- This template should never get called if these functions aren't available -->
  130. <!-- but this test is still necessary so that processors don't choke on the -->
  131. <!-- function calls if they don't support the set: functions -->
  132. <xsl:if test="function-available('set:leading') and function-available('set:trailing')">
  133. <xsl:choose>
  134. <xsl:when test="$blocks">
  135. <xsl:variable name="leading" select="set:leading($nodes,$block)"/>
  136. <xsl:variable name="trailing" select="set:trailing($nodes,$block)"/>
  137. <xsl:if test="(($wrap/@id or $wrap/@xml:id) and $first = 1) or $leading">
  138. <xsl:element name="{local-name($wrap)}" namespace="http://www.w3.org/1999/xhtml">
  139. <xsl:for-each select="$wrap/@*">
  140. <xsl:if test="$first != 0 or local-name(.) != 'id'">
  141. <xsl:copy/>
  142. </xsl:if>
  143. </xsl:for-each>
  144. <xsl:apply-templates select="$leading" mode="unwrap.p"/>
  145. </xsl:element>
  146. </xsl:if>
  147. <xsl:apply-templates select="$block" mode="unwrap.p"/>
  148. <xsl:if test="$trailing">
  149. <xsl:call-template name="unwrap.p.nodes">
  150. <xsl:with-param name="wrap" select="$wrap"/>
  151. <xsl:with-param name="nodes" select="$trailing"/>
  152. <xsl:with-param name="blocks" select="$blocks[position() &gt; 1]"/>
  153. </xsl:call-template>
  154. </xsl:if>
  155. </xsl:when>
  156. <xsl:otherwise>
  157. <xsl:if test="(($wrap/@id or $wrap/@xml:id) and $first = 1) or $nodes">
  158. <xsl:element name="{local-name($wrap)}" namespace="http://www.w3.org/1999/xhtml">
  159. <xsl:for-each select="$wrap/@*">
  160. <xsl:if test="$first != 0 or local-name(.) != 'id'">
  161. <xsl:copy/>
  162. </xsl:if>
  163. </xsl:for-each>
  164. <xsl:apply-templates select="$nodes" mode="unwrap.p"/>
  165. </xsl:element>
  166. </xsl:if>
  167. </xsl:otherwise>
  168. </xsl:choose>
  169. </xsl:if>
  170. </xsl:template>
  171. <!-- ==================================================================== -->
  172. <!-- make.verbatim.mode replaces spaces and newlines -->
  173. <xsl:template match="/" mode="make.verbatim.mode">
  174. <xsl:apply-templates mode="make.verbatim.mode"/>
  175. </xsl:template>
  176. <xsl:template match="*" mode="make.verbatim.mode">
  177. <xsl:copy>
  178. <xsl:copy-of select="@*"/>
  179. <xsl:apply-templates mode="make.verbatim.mode"/>
  180. </xsl:copy>
  181. </xsl:template>
  182. <xsl:template match="processing-instruction()|comment()" mode="make.verbatim.mode">
  183. <xsl:copy/>
  184. </xsl:template>
  185. <xsl:template match="text()" mode="make.verbatim.mode">
  186. <xsl:variable name="text" select="translate(., ' ', '&#160;')"/>
  187. <xsl:choose>
  188. <xsl:when test="not(contains($text, '&#10;'))">
  189. <xsl:value-of select="$text"/>
  190. </xsl:when>
  191. <xsl:otherwise>
  192. <xsl:variable name="len" select="string-length($text)"/>
  193. <xsl:choose>
  194. <xsl:when test="$len = 1">
  195. <br/><xsl:text>
  196. </xsl:text>
  197. </xsl:when>
  198. <xsl:otherwise>
  199. <xsl:variable name="half" select="$len div 2"/>
  200. <xsl:call-template name="make-verbatim-recursive">
  201. <xsl:with-param name="text" select="substring($text, 1, $half)"/>
  202. </xsl:call-template>
  203. <xsl:call-template name="make-verbatim-recursive">
  204. <xsl:with-param name="text" select="substring($text, ($half + 1), $len)"/>
  205. </xsl:call-template>
  206. </xsl:otherwise>
  207. </xsl:choose>
  208. </xsl:otherwise>
  209. </xsl:choose>
  210. </xsl:template>
  211. <xsl:template name="make-verbatim-recursive">
  212. <xsl:param name="text" select="''"/>
  213. <xsl:choose>
  214. <xsl:when test="not(contains($text, '&#10;'))">
  215. <xsl:value-of select="$text"/>
  216. </xsl:when>
  217. <xsl:otherwise>
  218. <xsl:variable name="len" select="string-length($text)"/>
  219. <xsl:choose>
  220. <xsl:when test="$len = 1">
  221. <br/><xsl:text>
  222. </xsl:text>
  223. </xsl:when>
  224. <xsl:otherwise>
  225. <xsl:variable name="half" select="$len div 2"/>
  226. <xsl:call-template name="make-verbatim-recursive">
  227. <xsl:with-param name="text" select="substring($text, 1, $half)"/>
  228. </xsl:call-template>
  229. <xsl:call-template name="make-verbatim-recursive">
  230. <xsl:with-param name="text" select="substring($text, ($half + 1), $len)"/>
  231. </xsl:call-template>
  232. </xsl:otherwise>
  233. </xsl:choose>
  234. </xsl:otherwise>
  235. </xsl:choose>
  236. </xsl:template>
  237. <!-- ==================================================================== -->
  238. <!-- remove.empty.div mode templates remove empty blocks -->
  239. <xsl:template name="remove.empty.div">
  240. <xsl:param name="div"/>
  241. <xsl:choose>
  242. <xsl:when test="$exsl.node.set.available != 0">
  243. <xsl:apply-templates select="exsl:node-set($div)" mode="remove.empty.div"/>
  244. </xsl:when>
  245. <xsl:otherwise>
  246. <xsl:copy-of select="$div"/>
  247. </xsl:otherwise>
  248. </xsl:choose>
  249. </xsl:template>
  250. <xsl:template xmlns:html="http://www.w3.org/1999/xhtml" match="html:p|p|html:div|div" mode="remove.empty.div">
  251. <xsl:if test="node()">
  252. <xsl:copy>
  253. <xsl:copy-of select="@*"/>
  254. <xsl:apply-templates mode="remove.empty.div"/>
  255. </xsl:copy>
  256. </xsl:if>
  257. </xsl:template>
  258. <xsl:template match="*" mode="remove.empty.div">
  259. <xsl:copy>
  260. <xsl:copy-of select="@*"/>
  261. <xsl:apply-templates mode="remove.empty.div"/>
  262. </xsl:copy>
  263. </xsl:template>
  264. <xsl:template match="text()|processing-instruction()|comment()" mode="remove.empty.div">
  265. <xsl:copy/>
  266. </xsl:template>
  267. <!-- ==================================================================== -->
  268. </xsl:stylesheet>