1
0

toc.xsl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. version='1.0'>
  5. <!-- ********************************************************************
  6. $Id: toc.xsl 8323 2009-03-12 22:52:17Z bobstayton $
  7. ********************************************************************
  8. This file is part of the XSL DocBook Stylesheet distribution.
  9. See ../README or http://docbook.sf.net/release/xsl/current/ for
  10. copyright and other information.
  11. ******************************************************************** -->
  12. <!-- ==================================================================== -->
  13. <!-- only set, book and part puts toc in its own page sequence -->
  14. <xsl:template match="set/toc | book/toc | part/toc">
  15. <xsl:variable name="toc.params">
  16. <xsl:call-template name="find.path.params">
  17. <xsl:with-param name="node" select="parent::*"/>
  18. <xsl:with-param name="table" select="normalize-space($generate.toc)"/>
  19. </xsl:call-template>
  20. </xsl:variable>
  21. <!-- Do not output the toc element if one is already generated
  22. by the use of $generate.toc parameter, or if
  23. generating a source toc is turned off -->
  24. <xsl:if test="not(contains($toc.params, 'toc')) and
  25. ($process.source.toc != 0 or $process.empty.source.toc != 0)">
  26. <!-- Don't generate a page sequence unless there is content -->
  27. <xsl:variable name="content">
  28. <xsl:choose>
  29. <xsl:when test="* and $process.source.toc != 0">
  30. <xsl:apply-templates />
  31. </xsl:when>
  32. <xsl:when test="count(*) = 0 and $process.empty.source.toc != 0">
  33. <!-- trick to switch context node to parent element -->
  34. <xsl:for-each select="parent::*">
  35. <xsl:choose>
  36. <xsl:when test="self::set">
  37. <xsl:call-template name="set.toc">
  38. <xsl:with-param name="toc.title.p"
  39. select="contains($toc.params, 'title')"/>
  40. </xsl:call-template>
  41. </xsl:when>
  42. <xsl:when test="self::book">
  43. <xsl:call-template name="division.toc">
  44. <xsl:with-param name="toc.title.p"
  45. select="contains($toc.params, 'title')"/>
  46. </xsl:call-template>
  47. </xsl:when>
  48. <xsl:when test="self::part">
  49. <xsl:call-template name="division.toc">
  50. <xsl:with-param name="toc.title.p"
  51. select="contains($toc.params, 'title')"/>
  52. </xsl:call-template>
  53. </xsl:when>
  54. </xsl:choose>
  55. </xsl:for-each>
  56. </xsl:when>
  57. </xsl:choose>
  58. </xsl:variable>
  59. <xsl:if test="string-length(normalize-space($content)) != 0">
  60. <xsl:variable name="lot-master-reference">
  61. <xsl:call-template name="select.pagemaster">
  62. <xsl:with-param name="pageclass" select="'lot'"/>
  63. </xsl:call-template>
  64. </xsl:variable>
  65. <xsl:call-template name="page.sequence">
  66. <xsl:with-param name="master-reference"
  67. select="$lot-master-reference"/>
  68. <xsl:with-param name="element" select="'toc'"/>
  69. <xsl:with-param name="gentext-key" select="'TableofContents'"/>
  70. <xsl:with-param name="content" select="$content"/>
  71. </xsl:call-template>
  72. </xsl:if>
  73. </xsl:if>
  74. </xsl:template>
  75. <xsl:template match="chapter/toc | appendix/toc | preface/toc | article/toc">
  76. <xsl:variable name="toc.params">
  77. <xsl:call-template name="find.path.params">
  78. <xsl:with-param name="node" select="parent::*"/>
  79. <xsl:with-param name="table" select="normalize-space($generate.toc)"/>
  80. </xsl:call-template>
  81. </xsl:variable>
  82. <!-- Do not output the toc element if one is already generated
  83. by the use of $generate.toc parameter, or if
  84. generating a source toc is turned off -->
  85. <xsl:if test="not(contains($toc.params, 'toc')) and
  86. ($process.source.toc != 0 or $process.empty.source.toc != 0)">
  87. <xsl:choose>
  88. <xsl:when test="* and $process.source.toc != 0">
  89. <fo:block>
  90. <xsl:apply-templates/>
  91. </fo:block>
  92. </xsl:when>
  93. <xsl:when test="count(*) = 0 and $process.empty.source.toc != 0">
  94. <!-- trick to switch context node to section element -->
  95. <xsl:for-each select="parent::*">
  96. <xsl:call-template name="component.toc">
  97. <xsl:with-param name="toc.title.p"
  98. select="contains($toc.params, 'title')"/>
  99. </xsl:call-template>
  100. </xsl:for-each>
  101. </xsl:when>
  102. </xsl:choose>
  103. <xsl:call-template name="component.toc.separator"/>
  104. </xsl:if>
  105. </xsl:template>
  106. <xsl:template match="section/toc
  107. |sect1/toc
  108. |sect2/toc
  109. |sect3/toc
  110. |sect4/toc
  111. |sect5/toc">
  112. <xsl:variable name="toc.params">
  113. <xsl:call-template name="find.path.params">
  114. <xsl:with-param name="node" select="parent::*"/>
  115. <xsl:with-param name="table" select="normalize-space($generate.toc)"/>
  116. </xsl:call-template>
  117. </xsl:variable>
  118. <!-- Do not output the toc element if one is already generated
  119. by the use of $generate.toc parameter, or if
  120. generating a source toc is turned off -->
  121. <xsl:if test="not(contains($toc.params, 'toc')) and
  122. ($process.source.toc != 0 or $process.empty.source.toc != 0)">
  123. <xsl:choose>
  124. <xsl:when test="* and $process.source.toc != 0">
  125. <fo:block>
  126. <xsl:apply-templates/>
  127. </fo:block>
  128. </xsl:when>
  129. <xsl:when test="count(*) = 0 and $process.empty.source.toc != 0">
  130. <!-- trick to switch context node to section element -->
  131. <xsl:for-each select="parent::*">
  132. <xsl:call-template name="section.toc">
  133. <xsl:with-param name="toc.title.p"
  134. select="contains($toc.params, 'title')"/>
  135. </xsl:call-template>
  136. </xsl:for-each>
  137. </xsl:when>
  138. </xsl:choose>
  139. <xsl:call-template name="section.toc.separator"/>
  140. </xsl:if>
  141. </xsl:template>
  142. <!-- ==================================================================== -->
  143. <xsl:template match="tocpart|tocchap
  144. |toclevel1|toclevel2|toclevel3|toclevel4|toclevel5">
  145. <xsl:apply-templates select="tocentry"/>
  146. <xsl:if test="tocchap|toclevel1|toclevel2|toclevel3|toclevel4|toclevel5">
  147. <fo:block start-indent="{count(ancestor::*)*2}pc">
  148. <xsl:apply-templates select="tocchap|toclevel1|toclevel2|toclevel3|toclevel4|toclevel5"/>
  149. </fo:block>
  150. </xsl:if>
  151. </xsl:template>
  152. <xsl:template match="tocentry|lotentry|tocdiv|tocfront|tocback">
  153. <fo:block text-align-last="justify"
  154. end-indent="2pc"
  155. last-line-end-indent="-2pc">
  156. <fo:inline keep-with-next.within-line="always">
  157. <xsl:choose>
  158. <xsl:when test="@linkend">
  159. <fo:basic-link internal-destination="{@linkend}">
  160. <xsl:apply-templates/>
  161. </fo:basic-link>
  162. </xsl:when>
  163. <xsl:otherwise>
  164. <xsl:apply-templates/>
  165. </xsl:otherwise>
  166. </xsl:choose>
  167. </fo:inline>
  168. <xsl:choose>
  169. <xsl:when test="@linkend">
  170. <fo:inline keep-together.within-line="always">
  171. <xsl:text> </xsl:text>
  172. <fo:leader leader-pattern="dots"
  173. keep-with-next.within-line="always"/>
  174. <xsl:text> </xsl:text>
  175. <fo:basic-link internal-destination="{@linkend}">
  176. <xsl:choose>
  177. <xsl:when test="@pagenum">
  178. <xsl:value-of select="@pagenum"/>
  179. </xsl:when>
  180. <xsl:otherwise>
  181. <fo:page-number-citation ref-id="{@linkend}"/>
  182. </xsl:otherwise>
  183. </xsl:choose>
  184. </fo:basic-link>
  185. </fo:inline>
  186. </xsl:when>
  187. <xsl:when test="@pagenum">
  188. <fo:inline keep-together.within-line="always">
  189. <xsl:text> </xsl:text>
  190. <fo:leader leader-pattern="dots"
  191. keep-with-next.within-line="always"/>
  192. <xsl:text> </xsl:text>
  193. <xsl:value-of select="@pagenum"/>
  194. </fo:inline>
  195. </xsl:when>
  196. <xsl:otherwise>
  197. <!-- just the leaders, what else can I do? -->
  198. <fo:inline keep-together.within-line="always">
  199. <xsl:text> </xsl:text>
  200. <fo:leader leader-pattern="space"
  201. keep-with-next.within-line="always"/>
  202. </fo:inline>
  203. </xsl:otherwise>
  204. </xsl:choose>
  205. </fo:block>
  206. </xsl:template>
  207. <xsl:template match="toc/title">
  208. <fo:block font-weight="bold">
  209. <xsl:apply-templates/>
  210. </fo:block>
  211. </xsl:template>
  212. <xsl:template match="toc/subtitle">
  213. <fo:block font-weight="bold">
  214. <xsl:apply-templates/>
  215. </fo:block>
  216. </xsl:template>
  217. <xsl:template match="toc/titleabbrev">
  218. </xsl:template>
  219. <!-- ==================================================================== -->
  220. <!-- A lot element must have content, because there is no attribute
  221. to select what kind of list should be generated -->
  222. <xsl:template match="book/lot | part/lot">
  223. <!-- Don't generate a page sequence unless there is content -->
  224. <xsl:variable name="content">
  225. <xsl:choose>
  226. <xsl:when test="* and $process.source.toc != 0">
  227. <xsl:apply-templates />
  228. </xsl:when>
  229. <xsl:when test="not(child::*) and $process.empty.source.toc != 0">
  230. <xsl:call-template name="process.empty.lot"/>
  231. </xsl:when>
  232. </xsl:choose>
  233. </xsl:variable>
  234. <xsl:if test="string-length(normalize-space($content)) != 0">
  235. <xsl:variable name="lot-master-reference">
  236. <xsl:call-template name="select.pagemaster">
  237. <xsl:with-param name="pageclass" select="'lot'"/>
  238. </xsl:call-template>
  239. </xsl:variable>
  240. <xsl:call-template name="page.sequence">
  241. <xsl:with-param name="master-reference"
  242. select="$lot-master-reference"/>
  243. <xsl:with-param name="element" select="'toc'"/>
  244. <xsl:with-param name="content" select="$content"/>
  245. </xsl:call-template>
  246. </xsl:if>
  247. </xsl:template>
  248. <xsl:template match="chapter/lot | appendix/lot | preface/lot | article/lot">
  249. <xsl:choose>
  250. <xsl:when test="* and $process.source.toc != 0">
  251. <fo:block>
  252. <xsl:apply-templates/>
  253. </fo:block>
  254. <xsl:call-template name="component.toc.separator"/>
  255. </xsl:when>
  256. <xsl:when test="not(child::*) and $process.empty.source.toc != 0">
  257. <xsl:call-template name="process.empty.lot"/>
  258. </xsl:when>
  259. </xsl:choose>
  260. </xsl:template>
  261. <xsl:template match="section/lot
  262. |sect1/lot
  263. |sect2/lot
  264. |sect3/lot
  265. |sect4/lot
  266. |sect5/lot">
  267. <xsl:choose>
  268. <xsl:when test="* and $process.source.toc != 0">
  269. <fo:block>
  270. <xsl:apply-templates/>
  271. </fo:block>
  272. <xsl:call-template name="section.toc.separator"/>
  273. </xsl:when>
  274. <xsl:when test="not(child::*) and $process.empty.source.toc != 0">
  275. <xsl:call-template name="process.empty.lot"/>
  276. </xsl:when>
  277. </xsl:choose>
  278. </xsl:template>
  279. <xsl:template name="process.empty.lot">
  280. <!-- An empty lot element does not provide any information to indicate
  281. what should be included in it. You can customize this
  282. template to generate a lot based on @role or something -->
  283. <xsl:message>
  284. <xsl:text>Warning: don't know what to generate for </xsl:text>
  285. <xsl:text>lot that has no children.</xsl:text>
  286. </xsl:message>
  287. </xsl:template>
  288. <xsl:template match="lot/title">
  289. <fo:block font-weight="bold">
  290. <xsl:apply-templates/>
  291. </fo:block>
  292. </xsl:template>
  293. <xsl:template match="lot/subtitle">
  294. <fo:block font-weight="bold">
  295. <xsl:apply-templates/>
  296. </fo:block>
  297. </xsl:template>
  298. <xsl:template match="lot/titleabbrev">
  299. </xsl:template>
  300. </xsl:stylesheet>