verbatim.xsl 14 KB

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