verbatim.xsl 12 KB

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