verbatim.xsl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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:sverb="http://nwalsh.com/xslt/ext/com.nwalsh.saxon.Verbatim" xmlns:xverb="xalan://com.nwalsh.xalan.Verbatim" xmlns:lxslt="http://xml.apache.org/xslt" xmlns:exsl="http://exslt.org/common" xmlns="http://www.w3.org/1999/xhtml" exclude-result-prefixes="sverb xverb lxslt exsl" version="1.0">
  5. <!-- ********************************************************************
  6. $Id: verbatim.xsl 9589 2012-09-02 20:52:15Z tom_schr $
  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. <!-- XSLTHL highlighting is turned off by default. See highlighting/README
  13. for instructions on how to turn on XSLTHL -->
  14. <xsl:template name="apply-highlighting">
  15. <xsl:apply-templates/>
  16. </xsl:template>
  17. <lxslt:component prefix="xverb" functions="numberLines"/>
  18. <xsl:template match="programlisting|screen|synopsis">
  19. <xsl:param name="suppress-numbers" select="'0'"/>
  20. <xsl:call-template name="anchor"/>
  21. <xsl:variable name="div.element">pre</xsl:variable>
  22. <xsl:if test="$shade.verbatim != 0">
  23. <xsl:message>
  24. <xsl:text>The shade.verbatim parameter is deprecated. </xsl:text>
  25. <xsl:text>Use CSS instead,</xsl:text>
  26. </xsl:message>
  27. <xsl:message>
  28. <xsl:text>for example: pre.</xsl:text>
  29. <xsl:value-of select="local-name(.)"/>
  30. <xsl:text> { background-color: #E0E0E0; }</xsl:text>
  31. </xsl:message>
  32. </xsl:if>
  33. <xsl:choose>
  34. <xsl:when test="$suppress-numbers = '0' and @linenumbering = 'numbered' and $use.extensions != '0' and $linenumbering.extension != '0'">
  35. <xsl:variable name="rtf">
  36. <xsl:choose>
  37. <xsl:when test="$highlight.source != 0">
  38. <xsl:call-template name="apply-highlighting"/>
  39. </xsl:when>
  40. <xsl:otherwise>
  41. <xsl:apply-templates/>
  42. </xsl:otherwise>
  43. </xsl:choose>
  44. </xsl:variable>
  45. <xsl:element name="{$div.element}" namespace="http://www.w3.org/1999/xhtml">
  46. <xsl:apply-templates select="." mode="common.html.attributes"/>
  47. <xsl:call-template name="id.attribute"/>
  48. <xsl:if test="@width != ''">
  49. <xsl:attribute name="width">
  50. <xsl:value-of select="@width"/>
  51. </xsl:attribute>
  52. </xsl:if>
  53. <xsl:call-template name="number.rtf.lines">
  54. <xsl:with-param name="rtf" select="$rtf"/>
  55. </xsl:call-template>
  56. </xsl:element>
  57. </xsl:when>
  58. <xsl:otherwise>
  59. <xsl:element name="{$div.element}" namespace="http://www.w3.org/1999/xhtml">
  60. <xsl:apply-templates select="." mode="common.html.attributes"/>
  61. <xsl:call-template name="id.attribute"/>
  62. <xsl:if test="@width != ''">
  63. <xsl:attribute name="width">
  64. <xsl:value-of select="@width"/>
  65. </xsl:attribute>
  66. </xsl:if>
  67. <xsl:choose>
  68. <xsl:when test="$highlight.source != 0">
  69. <xsl:call-template name="apply-highlighting"/>
  70. </xsl:when>
  71. <xsl:otherwise>
  72. <xsl:apply-templates/>
  73. </xsl:otherwise>
  74. </xsl:choose>
  75. </xsl:element>
  76. </xsl:otherwise>
  77. </xsl:choose>
  78. </xsl:template>
  79. <xsl:template match="literallayout">
  80. <xsl:param name="suppress-numbers" select="'0'"/>
  81. <xsl:variable name="rtf">
  82. <xsl:apply-templates/>
  83. </xsl:variable>
  84. <xsl:if test="$shade.verbatim != 0 and @class='monospaced'">
  85. <xsl:message>
  86. <xsl:text>The shade.verbatim parameter is deprecated. </xsl:text>
  87. <xsl:text>Use CSS instead,</xsl:text>
  88. </xsl:message>
  89. <xsl:message>
  90. <xsl:text>for example: pre.</xsl:text>
  91. <xsl:value-of select="local-name(.)"/>
  92. <xsl:text> { background-color: #E0E0E0; }</xsl:text>
  93. </xsl:message>
  94. </xsl:if>
  95. <xsl:choose>
  96. <xsl:when test="$suppress-numbers = '0' and @linenumbering = 'numbered' and $use.extensions != '0' and $linenumbering.extension != '0'">
  97. <xsl:choose>
  98. <xsl:when test="@class='monospaced'">
  99. <pre>
  100. <xsl:apply-templates select="." mode="common.html.attributes"/>
  101. <xsl:call-template name="id.attribute"/>
  102. <xsl:call-template name="number.rtf.lines">
  103. <xsl:with-param name="rtf" select="$rtf"/>
  104. </xsl:call-template>
  105. </pre>
  106. </xsl:when>
  107. <xsl:otherwise>
  108. <div>
  109. <xsl:apply-templates select="." mode="common.html.attributes"/>
  110. <xsl:call-template name="id.attribute"/>
  111. <p>
  112. <xsl:call-template name="number.rtf.lines">
  113. <xsl:with-param name="rtf" select="$rtf"/>
  114. </xsl:call-template>
  115. </p>
  116. </div>
  117. </xsl:otherwise>
  118. </xsl:choose>
  119. </xsl:when>
  120. <xsl:otherwise>
  121. <xsl:choose>
  122. <xsl:when test="@class='monospaced'">
  123. <pre>
  124. <xsl:apply-templates select="." mode="common.html.attributes"/>
  125. <xsl:call-template name="id.attribute"/>
  126. <xsl:copy-of select="$rtf"/>
  127. </pre>
  128. </xsl:when>
  129. <xsl:otherwise>
  130. <div>
  131. <xsl:apply-templates select="." mode="common.html.attributes"/>
  132. <xsl:call-template name="id.attribute"/>
  133. <p>
  134. <xsl:call-template name="make-verbatim">
  135. <xsl:with-param name="rtf" select="$rtf"/>
  136. </xsl:call-template>
  137. </p>
  138. </div>
  139. </xsl:otherwise>
  140. </xsl:choose>
  141. </xsl:otherwise>
  142. </xsl:choose>
  143. </xsl:template>
  144. <xsl:template match="address">
  145. <xsl:param name="suppress-numbers" select="'0'"/>
  146. <xsl:variable name="rtf">
  147. <xsl:apply-templates/>
  148. </xsl:variable>
  149. <xsl:choose>
  150. <xsl:when test="$suppress-numbers = '0' and @linenumbering = 'numbered' and $use.extensions != '0' and $linenumbering.extension != '0'">
  151. <div>
  152. <xsl:apply-templates select="." mode="common.html.attributes"/>
  153. <xsl:call-template name="id.attribute"/>
  154. <p>
  155. <xsl:call-template name="number.rtf.lines">
  156. <xsl:with-param name="rtf" select="$rtf"/>
  157. </xsl:call-template>
  158. </p>
  159. </div>
  160. </xsl:when>
  161. <xsl:otherwise>
  162. <div>
  163. <xsl:apply-templates select="." mode="common.html.attributes"/>
  164. <xsl:call-template name="id.attribute"/>
  165. <p>
  166. <xsl:call-template name="make-verbatim">
  167. <xsl:with-param name="rtf" select="$rtf"/>
  168. </xsl:call-template>
  169. </p>
  170. </div>
  171. </xsl:otherwise>
  172. </xsl:choose>
  173. </xsl:template>
  174. <xsl:template name="number.rtf.lines">
  175. <xsl:param name="rtf" select="''"/>
  176. <xsl:param name="pi.context" select="."/>
  177. <!-- Save the global values -->
  178. <xsl:variable name="global.linenumbering.everyNth" select="$linenumbering.everyNth"/>
  179. <xsl:variable name="global.linenumbering.separator" select="$linenumbering.separator"/>
  180. <xsl:variable name="global.linenumbering.width" select="$linenumbering.width"/>
  181. <!-- Extract the <?dbhtml linenumbering.*?> PI values -->
  182. <xsl:variable name="pi.linenumbering.everyNth">
  183. <xsl:call-template name="pi.dbhtml_linenumbering.everyNth">
  184. <xsl:with-param name="node" select="$pi.context"/>
  185. </xsl:call-template>
  186. </xsl:variable>
  187. <xsl:variable name="pi.linenumbering.separator">
  188. <xsl:call-template name="pi.dbhtml_linenumbering.separator">
  189. <xsl:with-param name="node" select="$pi.context"/>
  190. </xsl:call-template>
  191. </xsl:variable>
  192. <xsl:variable name="pi.linenumbering.width">
  193. <xsl:call-template name="pi.dbhtml_linenumbering.width">
  194. <xsl:with-param name="node" select="$pi.context"/>
  195. </xsl:call-template>
  196. </xsl:variable>
  197. <!-- Construct the 'in-context' values -->
  198. <xsl:variable name="linenumbering.everyNth">
  199. <xsl:choose>
  200. <xsl:when test="$pi.linenumbering.everyNth != ''">
  201. <xsl:value-of select="$pi.linenumbering.everyNth"/>
  202. </xsl:when>
  203. <xsl:otherwise>
  204. <xsl:value-of select="$global.linenumbering.everyNth"/>
  205. </xsl:otherwise>
  206. </xsl:choose>
  207. </xsl:variable>
  208. <xsl:variable name="linenumbering.separator">
  209. <xsl:choose>
  210. <xsl:when test="$pi.linenumbering.separator != ''">
  211. <xsl:value-of select="$pi.linenumbering.separator"/>
  212. </xsl:when>
  213. <xsl:otherwise>
  214. <xsl:value-of select="$global.linenumbering.separator"/>
  215. </xsl:otherwise>
  216. </xsl:choose>
  217. </xsl:variable>
  218. <xsl:variable name="linenumbering.width">
  219. <xsl:choose>
  220. <xsl:when test="$pi.linenumbering.width != ''">
  221. <xsl:value-of select="$pi.linenumbering.width"/>
  222. </xsl:when>
  223. <xsl:otherwise>
  224. <xsl:value-of select="$global.linenumbering.width"/>
  225. </xsl:otherwise>
  226. </xsl:choose>
  227. </xsl:variable>
  228. <xsl:variable name="linenumbering.startinglinenumber">
  229. <xsl:choose>
  230. <xsl:when test="$pi.context/@startinglinenumber">
  231. <xsl:value-of select="$pi.context/@startinglinenumber"/>
  232. </xsl:when>
  233. <xsl:when test="$pi.context/@continuation='continues'">
  234. <xsl:variable name="lastLine">
  235. <xsl:choose>
  236. <xsl:when test="$pi.context/self::programlisting">
  237. <xsl:call-template name="lastLineNumber">
  238. <xsl:with-param name="listings" select="preceding::programlisting[@linenumbering='numbered']"/>
  239. </xsl:call-template>
  240. </xsl:when>
  241. <xsl:when test="$pi.context/self::screen">
  242. <xsl:call-template name="lastLineNumber">
  243. <xsl:with-param name="listings" select="preceding::screen[@linenumbering='numbered']"/>
  244. </xsl:call-template>
  245. </xsl:when>
  246. <xsl:when test="$pi.context/self::literallayout">
  247. <xsl:call-template name="lastLineNumber">
  248. <xsl:with-param name="listings" select="preceding::literallayout[@linenumbering='numbered']"/>
  249. </xsl:call-template>
  250. </xsl:when>
  251. <xsl:when test="$pi.context/self::address">
  252. <xsl:call-template name="lastLineNumber">
  253. <xsl:with-param name="listings" select="preceding::address[@linenumbering='numbered']"/>
  254. </xsl:call-template>
  255. </xsl:when>
  256. <xsl:when test="$pi.context/self::synopsis">
  257. <xsl:call-template name="lastLineNumber">
  258. <xsl:with-param name="listings" select="preceding::synopsis[@linenumbering='numbered']"/>
  259. </xsl:call-template>
  260. </xsl:when>
  261. <xsl:otherwise>
  262. <xsl:message>
  263. <xsl:text>Unexpected verbatim environment: </xsl:text>
  264. <xsl:value-of select="local-name($pi.context)"/>
  265. </xsl:message>
  266. <xsl:value-of select="0"/>
  267. </xsl:otherwise>
  268. </xsl:choose>
  269. </xsl:variable>
  270. <xsl:value-of select="$lastLine + 1"/>
  271. </xsl:when>
  272. <xsl:otherwise>1</xsl:otherwise>
  273. </xsl:choose>
  274. </xsl:variable>
  275. <xsl:choose>
  276. <xsl:when test="function-available('sverb:numberLines')">
  277. <xsl:copy-of select="sverb:numberLines($rtf)"/>
  278. </xsl:when>
  279. <xsl:when test="function-available('xverb:numberLines')">
  280. <xsl:copy-of select="xverb:numberLines($rtf)"/>
  281. </xsl:when>
  282. <xsl:otherwise>
  283. <xsl:message terminate="yes">
  284. <xsl:text>No numberLines function available.</xsl:text>
  285. </xsl:message>
  286. </xsl:otherwise>
  287. </xsl:choose>
  288. </xsl:template>
  289. <xsl:template name="make-verbatim">
  290. <xsl:param name="rtf"/>
  291. <!-- I want to make this RTF verbatim. There are two possibilities: either
  292. I have access to the exsl:node-set extension function and I can "do it right"
  293. or I have to rely on CSS. -->
  294. <xsl:choose>
  295. <xsl:when test="$exsl.node.set.available != 0">
  296. <xsl:apply-templates select="exsl:node-set($rtf)" mode="make.verbatim.mode"/>
  297. </xsl:when>
  298. <xsl:otherwise>
  299. <span style="white-space: pre;">
  300. <xsl:copy-of select="$rtf"/>
  301. </span>
  302. </xsl:otherwise>
  303. </xsl:choose>
  304. </xsl:template>
  305. <!-- ======================================================================== -->
  306. <xsl:template name="lastLineNumber">
  307. <xsl:param name="listings"/>
  308. <xsl:param name="number" select="0"/>
  309. <xsl:variable name="lines">
  310. <xsl:call-template name="countLines">
  311. <xsl:with-param name="listing" select="string($listings[1])"/>
  312. </xsl:call-template>
  313. </xsl:variable>
  314. <xsl:choose>
  315. <xsl:when test="not($listings)">
  316. <xsl:value-of select="$number"/>
  317. </xsl:when>
  318. <xsl:when test="$listings[1]/@startinglinenumber">
  319. <xsl:value-of select="$number + $listings[1]/@startinglinenumber + $lines - 1"/>
  320. </xsl:when>
  321. <xsl:when test="$listings[1]/@continuation='continues'">
  322. <xsl:call-template name="lastLineNumber">
  323. <xsl:with-param name="listings" select="$listings[position() &gt; 1]"/>
  324. <xsl:with-param name="number" select="$number + $lines"/>
  325. </xsl:call-template>
  326. </xsl:when>
  327. <xsl:otherwise>
  328. <xsl:value-of select="$lines"/>
  329. </xsl:otherwise>
  330. </xsl:choose>
  331. </xsl:template>
  332. <xsl:template name="countLines">
  333. <xsl:param name="listing"/>
  334. <xsl:param name="count" select="1"/>
  335. <xsl:choose>
  336. <xsl:when test="contains($listing, '&#10;')">
  337. <xsl:call-template name="countLines">
  338. <xsl:with-param name="listing" select="substring-after($listing, '&#10;')"/>
  339. <xsl:with-param name="count" select="$count + 1"/>
  340. </xsl:call-template>
  341. </xsl:when>
  342. <xsl:otherwise>
  343. <xsl:value-of select="$count"/>
  344. </xsl:otherwise>
  345. </xsl:choose>
  346. </xsl:template>
  347. </xsl:stylesheet>