verbatim.xsl 14 KB

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