lfs-xref.xsl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?xml version='1.0' encoding='ISO-8859-1'?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns="http://www.w3.org/1999/xhtml"
  4. version="1.0">
  5. <!-- Making a proper punctuation in xref (only for English language).-->
  6. <xsl:template match="xref" name="xref">
  7. <xsl:variable name="targets" select="key('id',@linkend)"/>
  8. <xsl:variable name="target" select="$targets[1]"/>
  9. <xsl:variable name="refelem" select="local-name($target)"/>
  10. <xsl:variable name="role" select="@role"/>
  11. <xsl:call-template name="check.id.unique">
  12. <xsl:with-param name="linkend" select="@linkend"/>
  13. </xsl:call-template>
  14. <xsl:call-template name="anchor"/>
  15. <xsl:choose>
  16. <xsl:when test="count($target) = 0">
  17. <xsl:message>
  18. <xsl:text>XRef to nonexistent id: </xsl:text>
  19. <xsl:value-of select="@linkend"/>
  20. </xsl:message>
  21. <xsl:text>???</xsl:text>
  22. </xsl:when>
  23. <xsl:when test="$target/@xreflabel">
  24. <a>
  25. <xsl:attribute name="href">
  26. <xsl:call-template name="href.target">
  27. <xsl:with-param name="object" select="$target"/>
  28. </xsl:call-template>
  29. </xsl:attribute>
  30. <xsl:call-template name="xref.xreflabel">
  31. <xsl:with-param name="target" select="$target"/>
  32. </xsl:call-template>
  33. </a>
  34. </xsl:when>
  35. <xsl:otherwise>
  36. <xsl:variable name="href">
  37. <xsl:call-template name="href.target">
  38. <xsl:with-param name="object" select="$target"/>
  39. </xsl:call-template>
  40. </xsl:variable>
  41. <xsl:apply-templates select="$target" mode="xref-to-prefix"/>
  42. <a href="{$href}">
  43. <xsl:if test="$target/title or $target/*/title">
  44. <xsl:attribute name="title">
  45. <xsl:apply-templates select="$target" mode="xref-title"/>
  46. </xsl:attribute>
  47. </xsl:if>
  48. <xsl:apply-templates select="$target" mode="xref-to">
  49. <xsl:with-param name="referrer" select="."/>
  50. <xsl:with-param name="role" select="$role"/>
  51. <xsl:with-param name="xrefstyle">
  52. <xsl:value-of select="@xrefstyle"/>
  53. </xsl:with-param>
  54. </xsl:apply-templates>
  55. </a>
  56. <xsl:apply-templates select="$target" mode="xref-to-suffix"/>
  57. </xsl:otherwise>
  58. </xsl:choose>
  59. </xsl:template>
  60. <xsl:template match="section|simplesect|sect1|sect2|sect3|sect4|sect5|refsect1
  61. |refsect2|refsect3|refsection" mode="xref-to">
  62. <xsl:param name="referrer"/>
  63. <xsl:param name="xrefstyle"/>
  64. <xsl:param name="role"/>
  65. <xsl:apply-templates select="." mode="object.xref.markup">
  66. <xsl:with-param name="purpose" select="'xref'"/>
  67. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  68. <xsl:with-param name="referrer" select="$referrer"/>
  69. <xsl:with-param name="role" select="$role"/>
  70. </xsl:apply-templates>
  71. </xsl:template>
  72. <xsl:template match="*" mode="object.xref.markup">
  73. <xsl:param name="purpose"/>
  74. <xsl:param name="xrefstyle"/>
  75. <xsl:param name="referrer"/>
  76. <xsl:param name="role"/>
  77. <xsl:variable name="template">
  78. <xsl:choose>
  79. <xsl:when test="starts-with(normalize-space($xrefstyle), 'select:')">
  80. <xsl:call-template name="make.gentext.template">
  81. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  82. <xsl:with-param name="purpose" select="$purpose"/>
  83. <xsl:with-param name="referrer" select="$referrer"/>
  84. </xsl:call-template>
  85. </xsl:when>
  86. <xsl:when test="starts-with(normalize-space($xrefstyle), 'template:')">
  87. <xsl:value-of select="substring-after(normalize-space($xrefstyle), 'template:')"/>
  88. </xsl:when>
  89. <xsl:otherwise>
  90. <xsl:apply-templates select="." mode="object.xref.template">
  91. <xsl:with-param name="purpose" select="$purpose"/>
  92. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  93. <xsl:with-param name="referrer" select="$referrer"/>
  94. </xsl:apply-templates>
  95. </xsl:otherwise>
  96. </xsl:choose>
  97. </xsl:variable>
  98. <xsl:if test="$template = ''">
  99. <xsl:message>
  100. <xsl:text>object.xref.markup: empty xref template</xsl:text>
  101. <xsl:text> for linkend="</xsl:text>
  102. <xsl:value-of select="@id"/>
  103. <xsl:text>" and @xrefstyle="</xsl:text>
  104. <xsl:value-of select="$xrefstyle"/>
  105. <xsl:text>"</xsl:text>
  106. </xsl:message>
  107. </xsl:if>
  108. <xsl:call-template name="substitute-markup">
  109. <xsl:with-param name="purpose" select="$purpose"/>
  110. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  111. <xsl:with-param name="referrer" select="$referrer"/>
  112. <xsl:with-param name="template" select="$template"/>
  113. <xsl:with-param name="role" select="$role"/>
  114. </xsl:call-template>
  115. </xsl:template>
  116. <xsl:template name="substitute-markup">
  117. <xsl:param name="template" select="''"/>
  118. <xsl:param name="allow-anchors" select="'0'"/>
  119. <xsl:param name="title" select="''"/>
  120. <xsl:param name="subtitle" select="''"/>
  121. <xsl:param name="label" select="''"/>
  122. <xsl:param name="pagenumber" select="''"/>
  123. <xsl:param name="purpose"/>
  124. <xsl:param name="xrefstyle"/>
  125. <xsl:param name="referrer"/>
  126. <xsl:param name="role"/>
  127. <xsl:choose>
  128. <xsl:when test="contains($template, '%')">
  129. <xsl:value-of select="substring-before($template, '%')"/>
  130. <xsl:variable name="candidate"
  131. select="substring(substring-after($template, '%'), 1, 1)"/>
  132. <xsl:choose>
  133. <xsl:when test="$candidate = 't'">
  134. <xsl:apply-templates select="." mode="insert.title.markup">
  135. <xsl:with-param name="purpose" select="$purpose"/>
  136. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  137. <xsl:with-param name="role" select="$role"/>
  138. <xsl:with-param name="title">
  139. <xsl:choose>
  140. <xsl:when test="$title != ''">
  141. <xsl:copy-of select="$title"/>
  142. </xsl:when>
  143. <xsl:otherwise>
  144. <xsl:apply-templates select="." mode="title.markup">
  145. <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
  146. </xsl:apply-templates>
  147. </xsl:otherwise>
  148. </xsl:choose>
  149. </xsl:with-param>
  150. </xsl:apply-templates>
  151. </xsl:when>
  152. <xsl:when test="$candidate = 's'">
  153. <xsl:apply-templates select="." mode="insert.subtitle.markup">
  154. <xsl:with-param name="purpose" select="$purpose"/>
  155. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  156. <xsl:with-param name="subtitle">
  157. <xsl:choose>
  158. <xsl:when test="$subtitle != ''">
  159. <xsl:copy-of select="$subtitle"/>
  160. </xsl:when>
  161. <xsl:otherwise>
  162. <xsl:apply-templates select="." mode="subtitle.markup">
  163. <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
  164. </xsl:apply-templates>
  165. </xsl:otherwise>
  166. </xsl:choose>
  167. </xsl:with-param>
  168. </xsl:apply-templates>
  169. </xsl:when>
  170. <xsl:when test="$candidate = 'n'">
  171. <xsl:apply-templates select="." mode="insert.label.markup">
  172. <xsl:with-param name="purpose" select="$purpose"/>
  173. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  174. <xsl:with-param name="label">
  175. <xsl:choose>
  176. <xsl:when test="$label != ''">
  177. <xsl:copy-of select="$label"/>
  178. </xsl:when>
  179. <xsl:otherwise>
  180. <xsl:apply-templates select="." mode="label.markup"/>
  181. </xsl:otherwise>
  182. </xsl:choose>
  183. </xsl:with-param>
  184. </xsl:apply-templates>
  185. </xsl:when>
  186. <xsl:when test="$candidate = 'p'">
  187. <xsl:apply-templates select="." mode="insert.pagenumber.markup">
  188. <xsl:with-param name="purpose" select="$purpose"/>
  189. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  190. <xsl:with-param name="pagenumber">
  191. <xsl:choose>
  192. <xsl:when test="$pagenumber != ''">
  193. <xsl:copy-of select="$pagenumber"/>
  194. </xsl:when>
  195. <xsl:otherwise>
  196. <xsl:apply-templates select="." mode="pagenumber.markup"/>
  197. </xsl:otherwise>
  198. </xsl:choose>
  199. </xsl:with-param>
  200. </xsl:apply-templates>
  201. </xsl:when>
  202. <xsl:when test="$candidate = 'd'">
  203. <xsl:apply-templates select="." mode="insert.direction.markup">
  204. <xsl:with-param name="purpose" select="$purpose"/>
  205. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  206. <xsl:with-param name="direction">
  207. <xsl:choose>
  208. <xsl:when test="$referrer">
  209. <xsl:variable name="referent-is-below">
  210. <xsl:for-each select="preceding::xref">
  211. <xsl:if test="generate-id(.) = generate-id($referrer)">1</xsl:if>
  212. </xsl:for-each>
  213. </xsl:variable>
  214. <xsl:choose>
  215. <xsl:when test="$referent-is-below = ''">
  216. <xsl:call-template name="gentext">
  217. <xsl:with-param name="key" select="'above'"/>
  218. </xsl:call-template>
  219. </xsl:when>
  220. <xsl:otherwise>
  221. <xsl:call-template name="gentext">
  222. <xsl:with-param name="key" select="'below'"/>
  223. </xsl:call-template>
  224. </xsl:otherwise>
  225. </xsl:choose>
  226. </xsl:when>
  227. <xsl:otherwise>
  228. <xsl:message>Attempt to use %d in gentext with no referrer!</xsl:message>
  229. </xsl:otherwise>
  230. </xsl:choose>
  231. </xsl:with-param>
  232. </xsl:apply-templates>
  233. </xsl:when>
  234. <xsl:when test="$candidate = '%' ">
  235. <xsl:text>%</xsl:text>
  236. </xsl:when>
  237. <xsl:otherwise>
  238. <xsl:text>%</xsl:text><xsl:value-of select="$candidate"/>
  239. </xsl:otherwise>
  240. </xsl:choose>
  241. <!-- recurse with the rest of the template string -->
  242. <xsl:variable name="rest"
  243. select="substring($template,
  244. string-length(substring-before($template, '%'))+3)"/>
  245. <xsl:call-template name="substitute-markup">
  246. <xsl:with-param name="template" select="$rest"/>
  247. <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
  248. <xsl:with-param name="title" select="$title"/>
  249. <xsl:with-param name="subtitle" select="$subtitle"/>
  250. <xsl:with-param name="label" select="$label"/>
  251. <xsl:with-param name="pagenumber" select="$pagenumber"/>
  252. <xsl:with-param name="purpose" select="$purpose"/>
  253. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  254. <xsl:with-param name="referrer" select="$referrer"/>
  255. <xsl:with-param name="role" select="$role"/>
  256. </xsl:call-template>
  257. </xsl:when>
  258. <xsl:otherwise>
  259. <xsl:value-of select="$template"/>
  260. </xsl:otherwise>
  261. </xsl:choose>
  262. </xsl:template>
  263. </xsl:stylesheet>