html-rtf.xsl 11 KB

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