verbatim.xsl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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: verbatim.xsl 9590 2012-09-02 20:53:23Z tom_schr $
  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. <!-- XSLTHL highlighting is turned off by default. See highlighting/README
  18. for instructions on how to turn on XSLTHL -->
  19. <xsl:template name="apply-highlighting">
  20. <xsl:apply-templates/>
  21. </xsl:template>
  22. <lxslt:component prefix="xverb"
  23. functions="numberLines"/>
  24. <xsl:template match="programlisting|screen|synopsis">
  25. <xsl:param name="suppress-numbers" select="'0'"/>
  26. <xsl:variable name="id"><xsl:call-template name="object.id"/></xsl:variable>
  27. <xsl:variable name="content">
  28. <xsl:choose>
  29. <xsl:when test="$suppress-numbers = '0'
  30. and @linenumbering = 'numbered'
  31. and $use.extensions != '0'
  32. and $linenumbering.extension != '0'">
  33. <xsl:call-template name="number.rtf.lines">
  34. <xsl:with-param name="rtf">
  35. <xsl:choose>
  36. <xsl:when test="$highlight.source != 0">
  37. <xsl:call-template name="apply-highlighting"/>
  38. </xsl:when>
  39. <xsl:otherwise>
  40. <xsl:apply-templates/>
  41. </xsl:otherwise>
  42. </xsl:choose>
  43. </xsl:with-param>
  44. </xsl:call-template>
  45. </xsl:when>
  46. <xsl:otherwise>
  47. <xsl:choose>
  48. <xsl:when test="$highlight.source != 0">
  49. <xsl:call-template name="apply-highlighting"/>
  50. </xsl:when>
  51. <xsl:otherwise>
  52. <xsl:apply-templates/>
  53. </xsl:otherwise>
  54. </xsl:choose>
  55. </xsl:otherwise>
  56. </xsl:choose>
  57. </xsl:variable>
  58. <xsl:variable name="keep.together">
  59. <xsl:call-template name="pi.dbfo_keep-together"/>
  60. </xsl:variable>
  61. <xsl:variable name="block.content">
  62. <xsl:choose>
  63. <xsl:when test="$shade.verbatim != 0">
  64. <fo:block id="{$id}"
  65. xsl:use-attribute-sets="monospace.verbatim.properties shade.verbatim.style">
  66. <xsl:if test="$keep.together != ''">
  67. <xsl:attribute name="keep-together.within-column"><xsl:value-of
  68. select="$keep.together"/></xsl:attribute>
  69. </xsl:if>
  70. <xsl:choose>
  71. <xsl:when test="$hyphenate.verbatim != 0 and
  72. $exsl.node.set.available != 0">
  73. <xsl:apply-templates select="exsl:node-set($content)"
  74. mode="hyphenate.verbatim"/>
  75. </xsl:when>
  76. <xsl:otherwise>
  77. <xsl:copy-of select="$content"/>
  78. </xsl:otherwise>
  79. </xsl:choose>
  80. </fo:block>
  81. </xsl:when>
  82. <xsl:otherwise>
  83. <fo:block id="{$id}"
  84. xsl:use-attribute-sets="monospace.verbatim.properties">
  85. <xsl:if test="$keep.together != ''">
  86. <xsl:attribute name="keep-together.within-column"><xsl:value-of
  87. select="$keep.together"/></xsl:attribute>
  88. </xsl:if>
  89. <xsl:choose>
  90. <xsl:when test="$hyphenate.verbatim != 0 and
  91. $exsl.node.set.available != 0">
  92. <xsl:apply-templates select="exsl:node-set($content)"
  93. mode="hyphenate.verbatim"/>
  94. </xsl:when>
  95. <xsl:otherwise>
  96. <xsl:copy-of select="$content"/>
  97. </xsl:otherwise>
  98. </xsl:choose>
  99. </fo:block>
  100. </xsl:otherwise>
  101. </xsl:choose>
  102. </xsl:variable>
  103. <xsl:choose>
  104. <!-- Need a block-container for these features -->
  105. <xsl:when test="@width != '' or
  106. (self::programlisting and
  107. starts-with($writing.mode, 'rl'))">
  108. <fo:block-container start-indent="0pt" end-indent="0pt">
  109. <xsl:if test="@width != ''">
  110. <xsl:attribute name="width">
  111. <xsl:value-of select="concat(@width, '*', $monospace.verbatim.font.width)"/>
  112. </xsl:attribute>
  113. </xsl:if>
  114. <!-- All known program code is left-to-right -->
  115. <xsl:if test="self::programlisting and
  116. starts-with($writing.mode, 'rl')">
  117. <xsl:attribute name="writing-mode">lr-tb</xsl:attribute>
  118. </xsl:if>
  119. <xsl:copy-of select="$block.content"/>
  120. </fo:block-container>
  121. </xsl:when>
  122. <xsl:otherwise>
  123. <xsl:copy-of select="$block.content"/>
  124. </xsl:otherwise>
  125. </xsl:choose>
  126. </xsl:template>
  127. <xsl:template match="literallayout">
  128. <xsl:param name="suppress-numbers" select="'0'"/>
  129. <xsl:variable name="id"><xsl:call-template name="object.id"/></xsl:variable>
  130. <xsl:variable name="keep.together">
  131. <xsl:call-template name="pi.dbfo_keep-together"/>
  132. </xsl:variable>
  133. <xsl:variable name="content">
  134. <xsl:choose>
  135. <xsl:when test="$suppress-numbers = '0'
  136. and @linenumbering = 'numbered'
  137. and $use.extensions != '0'
  138. and $linenumbering.extension != '0'">
  139. <xsl:call-template name="number.rtf.lines">
  140. <xsl:with-param name="rtf">
  141. <xsl:apply-templates/>
  142. </xsl:with-param>
  143. </xsl:call-template>
  144. </xsl:when>
  145. <xsl:otherwise>
  146. <xsl:apply-templates/>
  147. </xsl:otherwise>
  148. </xsl:choose>
  149. </xsl:variable>
  150. <xsl:choose>
  151. <xsl:when test="@class='monospaced'">
  152. <xsl:choose>
  153. <xsl:when test="$shade.verbatim != 0">
  154. <fo:block id="{$id}"
  155. xsl:use-attribute-sets="monospace.verbatim.properties shade.verbatim.style">
  156. <xsl:if test="$keep.together != ''">
  157. <xsl:attribute name="keep-together.within-column"><xsl:value-of
  158. select="$keep.together"/></xsl:attribute>
  159. </xsl:if>
  160. <xsl:copy-of select="$content"/>
  161. </fo:block>
  162. </xsl:when>
  163. <xsl:otherwise>
  164. <fo:block id="{$id}"
  165. xsl:use-attribute-sets="monospace.verbatim.properties">
  166. <xsl:if test="$keep.together != ''">
  167. <xsl:attribute name="keep-together.within-column"><xsl:value-of
  168. select="$keep.together"/></xsl:attribute>
  169. </xsl:if>
  170. <xsl:copy-of select="$content"/>
  171. </fo:block>
  172. </xsl:otherwise>
  173. </xsl:choose>
  174. </xsl:when>
  175. <xsl:otherwise>
  176. <xsl:choose>
  177. <xsl:when test="$shade.verbatim != 0">
  178. <fo:block id="{$id}"
  179. xsl:use-attribute-sets="verbatim.properties shade.verbatim.style">
  180. <xsl:if test="$keep.together != ''">
  181. <xsl:attribute name="keep-together.within-column"><xsl:value-of
  182. select="$keep.together"/></xsl:attribute>
  183. </xsl:if>
  184. <xsl:copy-of select="$content"/>
  185. </fo:block>
  186. </xsl:when>
  187. <xsl:otherwise>
  188. <fo:block id="{$id}"
  189. xsl:use-attribute-sets="verbatim.properties">
  190. <xsl:if test="$keep.together != ''">
  191. <xsl:attribute name="keep-together.within-column"><xsl:value-of
  192. select="$keep.together"/></xsl:attribute>
  193. </xsl:if>
  194. <xsl:copy-of select="$content"/>
  195. </fo:block>
  196. </xsl:otherwise>
  197. </xsl:choose>
  198. </xsl:otherwise>
  199. </xsl:choose>
  200. </xsl:template>
  201. <xsl:template match="address">
  202. <xsl:param name="suppress-numbers" select="'0'"/>
  203. <xsl:variable name="content">
  204. <xsl:choose>
  205. <xsl:when test="$suppress-numbers = '0'
  206. and @linenumbering = 'numbered'
  207. and $use.extensions != '0'
  208. and $linenumbering.extension != '0'">
  209. <xsl:call-template name="number.rtf.lines">
  210. <xsl:with-param name="rtf">
  211. <xsl:apply-templates/>
  212. </xsl:with-param>
  213. </xsl:call-template>
  214. </xsl:when>
  215. <xsl:otherwise>
  216. <xsl:apply-templates/>
  217. </xsl:otherwise>
  218. </xsl:choose>
  219. </xsl:variable>
  220. <fo:block xsl:use-attribute-sets="verbatim.properties">
  221. <xsl:copy-of select="$content"/>
  222. </fo:block>
  223. </xsl:template>
  224. <xsl:template name="number.rtf.lines">
  225. <xsl:param name="rtf" select="''"/>
  226. <xsl:param name="pi.context" select="."/>
  227. <!-- Save the global values -->
  228. <xsl:variable name="global.linenumbering.everyNth"
  229. select="$linenumbering.everyNth"/>
  230. <xsl:variable name="global.linenumbering.separator"
  231. select="$linenumbering.separator"/>
  232. <xsl:variable name="global.linenumbering.width"
  233. select="$linenumbering.width"/>
  234. <!-- Extract the <?dbfo linenumbering.*?> PI values -->
  235. <xsl:variable name="pi.linenumbering.everyNth">
  236. <xsl:call-template name="pi.dbfo_linenumbering.everyNth">
  237. <xsl:with-param name="node" select="$pi.context"/>
  238. </xsl:call-template>
  239. </xsl:variable>
  240. <xsl:variable name="pi.linenumbering.separator">
  241. <xsl:call-template name="pi.dbfo_linenumbering.separator">
  242. <xsl:with-param name="node" select="$pi.context"/>
  243. </xsl:call-template>
  244. </xsl:variable>
  245. <xsl:variable name="pi.linenumbering.width">
  246. <xsl:call-template name="pi.dbfo_linenumbering.width">
  247. <xsl:with-param name="node" select="$pi.context"/>
  248. </xsl:call-template>
  249. </xsl:variable>
  250. <!-- Construct the 'in-context' values -->
  251. <xsl:variable name="linenumbering.everyNth">
  252. <xsl:choose>
  253. <xsl:when test="$pi.linenumbering.everyNth != ''">
  254. <xsl:value-of select="$pi.linenumbering.everyNth"/>
  255. </xsl:when>
  256. <xsl:otherwise>
  257. <xsl:value-of select="$global.linenumbering.everyNth"/>
  258. </xsl:otherwise>
  259. </xsl:choose>
  260. </xsl:variable>
  261. <xsl:variable name="linenumbering.separator">
  262. <xsl:choose>
  263. <xsl:when test="$pi.linenumbering.separator != ''">
  264. <xsl:value-of select="$pi.linenumbering.separator"/>
  265. </xsl:when>
  266. <xsl:otherwise>
  267. <xsl:value-of select="$global.linenumbering.separator"/>
  268. </xsl:otherwise>
  269. </xsl:choose>
  270. </xsl:variable>
  271. <xsl:variable name="linenumbering.width">
  272. <xsl:choose>
  273. <xsl:when test="$pi.linenumbering.width != ''">
  274. <xsl:value-of select="$pi.linenumbering.width"/>
  275. </xsl:when>
  276. <xsl:otherwise>
  277. <xsl:value-of select="$global.linenumbering.width"/>
  278. </xsl:otherwise>
  279. </xsl:choose>
  280. </xsl:variable>
  281. <xsl:variable name="linenumbering.startinglinenumber">
  282. <xsl:choose>
  283. <xsl:when test="$pi.context/@startinglinenumber">
  284. <xsl:value-of select="$pi.context/@startinglinenumber"/>
  285. </xsl:when>
  286. <xsl:when test="$pi.context/@continuation='continues'">
  287. <xsl:variable name="lastLine">
  288. <xsl:choose>
  289. <xsl:when test="$pi.context/self::programlisting">
  290. <xsl:call-template name="lastLineNumber">
  291. <xsl:with-param name="listings"
  292. select="preceding::programlisting[@linenumbering='numbered']"/>
  293. </xsl:call-template>
  294. </xsl:when>
  295. <xsl:when test="$pi.context/self::screen">
  296. <xsl:call-template name="lastLineNumber">
  297. <xsl:with-param name="listings"
  298. select="preceding::screen[@linenumbering='numbered']"/>
  299. </xsl:call-template>
  300. </xsl:when>
  301. <xsl:when test="$pi.context/self::literallayout">
  302. <xsl:call-template name="lastLineNumber">
  303. <xsl:with-param name="listings"
  304. select="preceding::literallayout[@linenumbering='numbered']"/>
  305. </xsl:call-template>
  306. </xsl:when>
  307. <xsl:when test="$pi.context/self::address">
  308. <xsl:call-template name="lastLineNumber">
  309. <xsl:with-param name="listings"
  310. select="preceding::address[@linenumbering='numbered']"/>
  311. </xsl:call-template>
  312. </xsl:when>
  313. <xsl:when test="$pi.context/self::synopsis">
  314. <xsl:call-template name="lastLineNumber">
  315. <xsl:with-param name="listings"
  316. select="preceding::synopsis[@linenumbering='numbered']"/>
  317. </xsl:call-template>
  318. </xsl:when>
  319. <xsl:otherwise>
  320. <xsl:message>
  321. <xsl:text>Unexpected verbatim environment: </xsl:text>
  322. <xsl:value-of select="local-name(.)"/>
  323. </xsl:message>
  324. <xsl:value-of select="0"/>
  325. </xsl:otherwise>
  326. </xsl:choose>
  327. </xsl:variable>
  328. <xsl:value-of select="$lastLine + 1"/>
  329. </xsl:when>
  330. <xsl:otherwise>1</xsl:otherwise>
  331. </xsl:choose>
  332. </xsl:variable>
  333. <xsl:choose>
  334. <xsl:when test="function-available('sverb:numberLines')">
  335. <xsl:copy-of select="sverb:numberLines($rtf)"/>
  336. </xsl:when>
  337. <xsl:when test="function-available('xverb:numberLines')">
  338. <xsl:copy-of select="xverb:numberLines($rtf)"/>
  339. </xsl:when>
  340. <xsl:otherwise>
  341. <xsl:message terminate="yes">
  342. <xsl:text>No numberLines function available.</xsl:text>
  343. </xsl:message>
  344. </xsl:otherwise>
  345. </xsl:choose>
  346. </xsl:template>
  347. <!-- ======================================================================== -->
  348. <xsl:template name="lastLineNumber">
  349. <xsl:param name="listings"/>
  350. <xsl:param name="number" select="0"/>
  351. <xsl:variable name="lines">
  352. <xsl:call-template name="countLines">
  353. <xsl:with-param name="listing" select="string($listings[1])"/>
  354. </xsl:call-template>
  355. </xsl:variable>
  356. <xsl:choose>
  357. <xsl:when test="not($listings)">
  358. <xsl:value-of select="$number"/>
  359. </xsl:when>
  360. <xsl:when test="$listings[1]/@startinglinenumber">
  361. <xsl:value-of select="$number + $listings[1]/@startinglinenumber + $lines - 1"/>
  362. </xsl:when>
  363. <xsl:when test="$listings[1]/@continuation='continues'">
  364. <xsl:call-template name="lastLineNumber">
  365. <xsl:with-param name="listings" select="$listings[position() &gt; 1]"/>
  366. <xsl:with-param name="number" select="$number + $lines"/>
  367. </xsl:call-template>
  368. </xsl:when>
  369. <xsl:otherwise>
  370. <xsl:value-of select="$lines"/>
  371. </xsl:otherwise>
  372. </xsl:choose>
  373. </xsl:template>
  374. <xsl:template name="countLines">
  375. <xsl:param name="listing"/>
  376. <xsl:param name="count" select="1"/>
  377. <xsl:choose>
  378. <xsl:when test="contains($listing, '&#10;')">
  379. <xsl:call-template name="countLines">
  380. <xsl:with-param name="listing" select="substring-after($listing, '&#10;')"/>
  381. <xsl:with-param name="count" select="$count + 1"/>
  382. </xsl:call-template>
  383. </xsl:when>
  384. <xsl:otherwise>
  385. <xsl:value-of select="$count"/>
  386. </xsl:otherwise>
  387. </xsl:choose>
  388. </xsl:template>
  389. <!-- ======================================================================== -->
  390. <xsl:template match="node()|@*" mode="hyphenate.verbatim">
  391. <xsl:copy>
  392. <xsl:copy-of select="@*"/>
  393. <xsl:apply-templates mode="hyphenate.verbatim"/>
  394. </xsl:copy>
  395. </xsl:template>
  396. <xsl:template match="text()" mode="hyphenate.verbatim" priority="2">
  397. <xsl:call-template name="hyphenate.verbatim.block">
  398. <xsl:with-param name="content" select="."/>
  399. </xsl:call-template>
  400. </xsl:template>
  401. <xsl:template name="hyphenate.verbatim.block">
  402. <xsl:param name="content" select="''"/>
  403. <xsl:param name="count" select="1"/>
  404. <!-- recurse on lines first to keep recursion depth reasonable -->
  405. <xsl:choose>
  406. <xsl:when test="contains($content, '&#xA;')">
  407. <xsl:variable name="line" select="substring-before($content, '&#xA;')"/>
  408. <xsl:variable name="rest" select="substring-after($content, '&#xA;')"/>
  409. <xsl:call-template name="hyphenate.verbatim">
  410. <xsl:with-param name="content" select="concat($line, '&#xA;')"/>
  411. </xsl:call-template>
  412. <xsl:call-template name="hyphenate.verbatim.block">
  413. <xsl:with-param name="content" select="$rest"/>
  414. <xsl:with-param name="count" select="$count + 1"/>
  415. </xsl:call-template>
  416. </xsl:when>
  417. <xsl:otherwise>
  418. <xsl:call-template name="hyphenate.verbatim">
  419. <xsl:with-param name="content" select="$content"/>
  420. </xsl:call-template>
  421. </xsl:otherwise>
  422. </xsl:choose>
  423. </xsl:template>
  424. <xsl:template name="hyphenate.verbatim">
  425. <xsl:param name="content"/>
  426. <xsl:variable name="head" select="substring($content, 1, 1)"/>
  427. <xsl:variable name="tail" select="substring($content, 2)"/>
  428. <xsl:choose>
  429. <!-- Place soft-hyphen after space or non-breakable space. -->
  430. <xsl:when test="$head = ' ' or $head = '&#160;'">
  431. <xsl:text>&#160;</xsl:text>
  432. <xsl:text>&#x00AD;</xsl:text>
  433. </xsl:when>
  434. <xsl:when test="$hyphenate.verbatim.characters != '' and
  435. translate($head, $hyphenate.verbatim.characters, '') = '' and not($tail = '')">
  436. <xsl:value-of select="$head"/>
  437. <xsl:text>&#x00AD;</xsl:text>
  438. </xsl:when>
  439. <xsl:otherwise>
  440. <xsl:value-of select="$head"/>
  441. </xsl:otherwise>
  442. </xsl:choose>
  443. <xsl:if test="$tail">
  444. <xsl:call-template name="hyphenate.verbatim">
  445. <xsl:with-param name="content" select="$tail"/>
  446. </xsl:call-template>
  447. </xsl:if>
  448. </xsl:template>
  449. </xsl:stylesheet>