lfs-xref.xsl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?xml version='1.0' encoding='ISO-8859-1'?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns:xlink="http://www.w3.org/1999/xlink"
  4. xmlns:fo="http://www.w3.org/1999/XSL/Format"
  5. exclude-result-prefixes="xlink"
  6. version="1.0">
  7. <!-- This stylesheet fixes English punctuation in xref links
  8. (as was requested by the publisher) via adding @role propagation
  9. in xref tags.
  10. This hack may not work with xref flavours not used in the book.
  11. For other languages, just remove the xref @role attributes
  12. in the book XML sources and/or comment-out the inclusion of
  13. this file in lfs-pdf.xsl -->
  14. <!-- xref:
  15. Added role variable and use it when calling mode xref-to.-->
  16. <!-- The original template is in {docbook-xsl}/fo/xref.xsl -->
  17. <xsl:template match="xref" name="xref">
  18. <xsl:param name="xhref" select="@xlink:href"/>
  19. <!-- is the @xlink:href a local idref link? -->
  20. <xsl:param name="xlink.idref">
  21. <xsl:if test="starts-with($xhref,'#')
  22. and (not(contains($xhref,'&#40;'))
  23. or starts-with($xhref, '#xpointer&#40;id&#40;'))">
  24. <xsl:call-template name="xpointer.idref">
  25. <xsl:with-param name="xpointer" select="$xhref"/>
  26. </xsl:call-template>
  27. </xsl:if>
  28. </xsl:param>
  29. <xsl:param name="xlink.targets" select="key('id',$xlink.idref)"/>
  30. <xsl:param name="linkend.targets" select="key('id',@linkend)"/>
  31. <xsl:param name="target" select="($xlink.targets | $linkend.targets)[1]"/>
  32. <xsl:param name="refelem" select="local-name($target)"/>
  33. <!-- Added role variable -->
  34. <xsl:variable name="role" select="@role"/>
  35. <xsl:variable name="xrefstyle">
  36. <xsl:choose>
  37. <xsl:when test="@role and not(@xrefstyle)
  38. and $use.role.as.xrefstyle != 0">
  39. <xsl:value-of select="@role"/>
  40. </xsl:when>
  41. <xsl:otherwise>
  42. <xsl:value-of select="@xrefstyle"/>
  43. </xsl:otherwise>
  44. </xsl:choose>
  45. </xsl:variable>
  46. <xsl:variable name="content">
  47. <fo:inline xsl:use-attribute-sets="xref.properties">
  48. <xsl:choose>
  49. <xsl:when test="@endterm">
  50. <xsl:variable name="etargets" select="key('id',@endterm)"/>
  51. <xsl:variable name="etarget" select="$etargets[1]"/>
  52. <xsl:choose>
  53. <xsl:when test="count($etarget) = 0">
  54. <xsl:message>
  55. <xsl:value-of select="count($etargets)"/>
  56. <xsl:text>Endterm points to nonexistent ID: </xsl:text>
  57. <xsl:value-of select="@endterm"/>
  58. </xsl:message>
  59. <xsl:text>???</xsl:text>
  60. </xsl:when>
  61. <xsl:otherwise>
  62. <xsl:apply-templates select="$etarget" mode="endterm"/>
  63. </xsl:otherwise>
  64. </xsl:choose>
  65. </xsl:when>
  66. <xsl:when test="$target/@xreflabel">
  67. <xsl:call-template name="xref.xreflabel">
  68. <xsl:with-param name="target" select="$target"/>
  69. </xsl:call-template>
  70. </xsl:when>
  71. <xsl:when test="$target">
  72. <xsl:if test="not(parent::citation)">
  73. <xsl:apply-templates select="$target" mode="xref-to-prefix"/>
  74. </xsl:if>
  75. <xsl:apply-templates select="$target" mode="xref-to">
  76. <xsl:with-param name="referrer" select="."/>
  77. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  78. <!-- Propagate role -->
  79. <xsl:with-param name="role" select="$role"/>
  80. </xsl:apply-templates>
  81. <xsl:if test="not(parent::citation)">
  82. <xsl:apply-templates select="$target" mode="xref-to-suffix"/>
  83. </xsl:if>
  84. </xsl:when>
  85. <xsl:otherwise>
  86. <xsl:message>
  87. <xsl:text>ERROR: xref linking to </xsl:text>
  88. <xsl:value-of select="@linkend|@xlink:href"/>
  89. <xsl:text> has no generated link text.</xsl:text>
  90. </xsl:message>
  91. <xsl:text>???</xsl:text>
  92. </xsl:otherwise>
  93. </xsl:choose>
  94. </fo:inline>
  95. </xsl:variable>
  96. <!-- Convert it into an active link -->
  97. <xsl:call-template name="simple.xlink">
  98. <xsl:with-param name="content" select="$content"/>
  99. </xsl:call-template>
  100. <!-- Add standard page reference? -->
  101. <xsl:choose>
  102. <xsl:when test="not($target)">
  103. <!-- page numbers only for local targets -->
  104. </xsl:when>
  105. <xsl:when test="starts-with(normalize-space($xrefstyle), 'select:')
  106. and contains($xrefstyle, 'nopage')">
  107. <!-- negative xrefstyle in instance turns it off -->
  108. </xsl:when>
  109. <!-- positive xrefstyle already handles it -->
  110. <xsl:when test="not(starts-with(normalize-space($xrefstyle), 'select:')
  111. and (contains($xrefstyle, 'page')
  112. or contains($xrefstyle, 'Page')))
  113. and ( $insert.xref.page.number = 'yes'
  114. or $insert.xref.page.number = '1')
  115. or local-name($target) = 'para'">
  116. <xsl:apply-templates select="$target" mode="page.citation">
  117. <xsl:with-param name="id" select="$target/@id|$target/@xml:id"/>
  118. </xsl:apply-templates>
  119. </xsl:when>
  120. </xsl:choose>
  121. </xsl:template>
  122. <!-- sect* mode xref-to:
  123. Propagate role to mode object.xref.markup -->
  124. <!-- The original template is in {docbook-xsl}/fo/xref.xsl -->
  125. <xsl:template match="section|simplesect|sect1|sect2|sect3|sect4|sect5
  126. |refsect1|refsect2|refsect3|refsection" mode="xref-to">
  127. <xsl:param name="referrer"/>
  128. <xsl:param name="xrefstyle"/>
  129. <xsl:param name="verbose" select="1"/>
  130. <xsl:param name="role"/>
  131. <xsl:apply-templates select="." mode="object.xref.markup">
  132. <xsl:with-param name="purpose" select="'xref'"/>
  133. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  134. <xsl:with-param name="referrer" select="$referrer"/>
  135. <xsl:with-param name="verbose" select="$verbose"/>
  136. <xsl:with-param name="role" select="$role"/>
  137. </xsl:apply-templates>
  138. </xsl:template>
  139. <!-- mode object.xref.markup:
  140. Propagate role to named template substitute-markup -->
  141. <!-- The original template is in {docbook-xsl}/common/gentext.xsl -->
  142. <xsl:template match="*" mode="object.xref.markup">
  143. <xsl:param name="purpose"/>
  144. <xsl:param name="xrefstyle"/>
  145. <xsl:param name="referrer"/>
  146. <xsl:param name="verbose" select="1"/>
  147. <xsl:param name="role"/>
  148. <xsl:variable name="template">
  149. <xsl:choose>
  150. <xsl:when test="starts-with(normalize-space($xrefstyle), 'select:')">
  151. <xsl:call-template name="make.gentext.template">
  152. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  153. <xsl:with-param name="purpose" select="$purpose"/>
  154. <xsl:with-param name="referrer" select="$referrer"/>
  155. </xsl:call-template>
  156. </xsl:when>
  157. <xsl:when test="starts-with(normalize-space($xrefstyle), 'template:')">
  158. <xsl:value-of select="substring-after(normalize-space($xrefstyle), 'template:')"/>
  159. </xsl:when>
  160. <xsl:otherwise>
  161. <xsl:apply-templates select="." mode="object.xref.template">
  162. <xsl:with-param name="purpose" select="$purpose"/>
  163. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  164. <xsl:with-param name="referrer" select="$referrer"/>
  165. </xsl:apply-templates>
  166. </xsl:otherwise>
  167. </xsl:choose>
  168. </xsl:variable>
  169. <xsl:if test="$template = '' and $verbose != 0">
  170. <xsl:message>
  171. <xsl:text>object.xref.markup: empty xref template</xsl:text>
  172. <xsl:text> for linkend="</xsl:text>
  173. <xsl:value-of select="@id|@xml:id"/>
  174. <xsl:text>" and @xrefstyle="</xsl:text>
  175. <xsl:value-of select="$xrefstyle"/>
  176. <xsl:text>"</xsl:text>
  177. </xsl:message>
  178. </xsl:if>
  179. <xsl:call-template name="substitute-markup">
  180. <xsl:with-param name="purpose" select="$purpose"/>
  181. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  182. <xsl:with-param name="referrer" select="$referrer"/>
  183. <xsl:with-param name="template" select="$template"/>
  184. <xsl:with-param name="verbose" select="$verbose"/>
  185. <xsl:with-param name="role" select="$role"/>
  186. </xsl:call-template>
  187. </xsl:template>
  188. <!-- substitute-markup:
  189. Propagate role to mode insert.title.markup -->
  190. <!-- The original template is in {docbook-xsl}/common/gentext.xsl -->
  191. <xsl:template name="substitute-markup">
  192. <xsl:param name="template" select="''"/>
  193. <xsl:param name="allow-anchors" select="'0'"/>
  194. <xsl:param name="title" select="''"/>
  195. <xsl:param name="subtitle" select="''"/>
  196. <xsl:param name="docname" select="''"/>
  197. <xsl:param name="label" select="''"/>
  198. <xsl:param name="pagenumber" select="''"/>
  199. <xsl:param name="purpose"/>
  200. <xsl:param name="xrefstyle"/>
  201. <xsl:param name="referrer"/>
  202. <xsl:param name="verbose"/>
  203. <xsl:param name="role"/>
  204. <xsl:choose>
  205. <xsl:when test="contains($template, '%')">
  206. <xsl:value-of select="substring-before($template, '%')"/>
  207. <xsl:variable name="candidate"
  208. select="substring(substring-after($template, '%'), 1, 1)"/>
  209. <xsl:choose>
  210. <xsl:when test="$candidate = 't'">
  211. <xsl:apply-templates select="." mode="insert.title.markup">
  212. <xsl:with-param name="purpose" select="$purpose"/>
  213. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  214. <xsl:with-param name="role" select="$role"/>
  215. <xsl:with-param name="title">
  216. <xsl:choose>
  217. <xsl:when test="$title != ''">
  218. <xsl:copy-of select="$title"/>
  219. </xsl:when>
  220. <xsl:otherwise>
  221. <xsl:apply-templates select="." mode="title.markup">
  222. <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
  223. <xsl:with-param name="verbose" select="$verbose"/>
  224. </xsl:apply-templates>
  225. </xsl:otherwise>
  226. </xsl:choose>
  227. </xsl:with-param>
  228. </xsl:apply-templates>
  229. </xsl:when>
  230. <xsl:when test="$candidate = 's'">
  231. <xsl:apply-templates select="." mode="insert.subtitle.markup">
  232. <xsl:with-param name="purpose" select="$purpose"/>
  233. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  234. <xsl:with-param name="subtitle">
  235. <xsl:choose>
  236. <xsl:when test="$subtitle != ''">
  237. <xsl:copy-of select="$subtitle"/>
  238. </xsl:when>
  239. <xsl:otherwise>
  240. <xsl:apply-templates select="." mode="subtitle.markup">
  241. <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
  242. </xsl:apply-templates>
  243. </xsl:otherwise>
  244. </xsl:choose>
  245. </xsl:with-param>
  246. </xsl:apply-templates>
  247. </xsl:when>
  248. <xsl:when test="$candidate = 'n'">
  249. <xsl:apply-templates select="." mode="insert.label.markup">
  250. <xsl:with-param name="purpose" select="$purpose"/>
  251. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  252. <xsl:with-param name="label">
  253. <xsl:choose>
  254. <xsl:when test="$label != ''">
  255. <xsl:copy-of select="$label"/>
  256. </xsl:when>
  257. <xsl:otherwise>
  258. <xsl:apply-templates select="." mode="label.markup"/>
  259. </xsl:otherwise>
  260. </xsl:choose>
  261. </xsl:with-param>
  262. </xsl:apply-templates>
  263. </xsl:when>
  264. <xsl:when test="$candidate = 'p'">
  265. <xsl:apply-templates select="." mode="insert.pagenumber.markup">
  266. <xsl:with-param name="purpose" select="$purpose"/>
  267. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  268. <xsl:with-param name="pagenumber">
  269. <xsl:choose>
  270. <xsl:when test="$pagenumber != ''">
  271. <xsl:copy-of select="$pagenumber"/>
  272. </xsl:when>
  273. <xsl:otherwise>
  274. <xsl:apply-templates select="." mode="pagenumber.markup"/>
  275. </xsl:otherwise>
  276. </xsl:choose>
  277. </xsl:with-param>
  278. </xsl:apply-templates>
  279. </xsl:when>
  280. <xsl:when test="$candidate = 'o'">
  281. <!-- olink target document title -->
  282. <xsl:apply-templates select="." mode="insert.olink.docname.markup">
  283. <xsl:with-param name="purpose" select="$purpose"/>
  284. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  285. <xsl:with-param name="docname">
  286. <xsl:choose>
  287. <xsl:when test="$docname != ''">
  288. <xsl:copy-of select="$docname"/>
  289. </xsl:when>
  290. <xsl:otherwise>
  291. <xsl:apply-templates select="." mode="olink.docname.markup"/>
  292. </xsl:otherwise>
  293. </xsl:choose>
  294. </xsl:with-param>
  295. </xsl:apply-templates>
  296. </xsl:when>
  297. <xsl:when test="$candidate = 'd'">
  298. <xsl:apply-templates select="." mode="insert.direction.markup">
  299. <xsl:with-param name="purpose" select="$purpose"/>
  300. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  301. <xsl:with-param name="direction">
  302. <xsl:choose>
  303. <xsl:when test="$referrer">
  304. <xsl:variable name="referent-is-below">
  305. <xsl:for-each select="preceding::xref">
  306. <xsl:if test="generate-id(.) = generate-id($referrer)">1</xsl:if>
  307. </xsl:for-each>
  308. </xsl:variable>
  309. <xsl:choose>
  310. <xsl:when test="$referent-is-below = ''">
  311. <xsl:call-template name="gentext">
  312. <xsl:with-param name="key" select="'above'"/>
  313. </xsl:call-template>
  314. </xsl:when>
  315. <xsl:otherwise>
  316. <xsl:call-template name="gentext">
  317. <xsl:with-param name="key" select="'below'"/>
  318. </xsl:call-template>
  319. </xsl:otherwise>
  320. </xsl:choose>
  321. </xsl:when>
  322. <xsl:otherwise>
  323. <xsl:message>Attempt to use %d in gentext with no referrer!</xsl:message>
  324. </xsl:otherwise>
  325. </xsl:choose>
  326. </xsl:with-param>
  327. </xsl:apply-templates>
  328. </xsl:when>
  329. <xsl:when test="$candidate = '%' ">
  330. <xsl:text>%</xsl:text>
  331. </xsl:when>
  332. <xsl:otherwise>
  333. <xsl:text>%</xsl:text><xsl:value-of select="$candidate"/>
  334. </xsl:otherwise>
  335. </xsl:choose>
  336. <!-- recurse with the rest of the template string -->
  337. <xsl:variable name="rest"
  338. select="substring($template,
  339. string-length(substring-before($template, '%'))+3)"/>
  340. <xsl:call-template name="substitute-markup">
  341. <xsl:with-param name="template" select="$rest"/>
  342. <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
  343. <xsl:with-param name="title" select="$title"/>
  344. <xsl:with-param name="subtitle" select="$subtitle"/>
  345. <xsl:with-param name="docname" select="$docname"/>
  346. <xsl:with-param name="label" select="$label"/>
  347. <xsl:with-param name="pagenumber" select="$pagenumber"/>
  348. <xsl:with-param name="purpose" select="$purpose"/>
  349. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  350. <xsl:with-param name="referrer" select="$referrer"/>
  351. <xsl:with-param name="verbose" select="$verbose"/>
  352. <xsl:with-param name="role" select="$role"/>
  353. </xsl:call-template>
  354. </xsl:when>
  355. <xsl:otherwise>
  356. <xsl:value-of select="$template"/>
  357. </xsl:otherwise>
  358. </xsl:choose>
  359. </xsl:template>
  360. <!-- insert.title.markup:
  361. Apply the role value. -->
  362. <!-- The original template is in {docbook-xsl}/fo/xref.xsl -->
  363. <xsl:template match="*" mode="insert.title.markup">
  364. <xsl:param name="purpose"/>
  365. <xsl:param name="xrefstyle"/>
  366. <xsl:param name="title"/>
  367. <xsl:param name="role"/>
  368. <xsl:choose>
  369. <xsl:when test="$purpose = 'xref' and titleabbrev">
  370. <xsl:apply-templates select="." mode="titleabbrev.markup"/>
  371. </xsl:when>
  372. <xsl:otherwise>
  373. <xsl:copy-of select="$title"/>
  374. <xsl:value-of select="$role"/>
  375. </xsl:otherwise>
  376. </xsl:choose>
  377. </xsl:template>
  378. </xsl:stylesheet>