1<?xml version='1.0'?>
2<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3                version='1.0'>
4
5<!-- ********************************************************************
6     $Id$
7     ********************************************************************
8
9     This file is part of the XSL DocBook Stylesheet distribution.
10     See /README or http://nwalsh.com/docbook/xsl/ for copyright
11     and other information.
12
13     ******************************************************************** -->
14
15<xsl:param name="autotoc.label.separator" select="' '"/>
16
17<xsl:template name="href.target">
18  <xsl:param name="object" select="."/>
19  <xsl:text>#</xsl:text>
20  <xsl:call-template name="object.id">
21    <xsl:with-param name="object" select="$object"/>
22  </xsl:call-template>
23</xsl:template>
24
25<xsl:variable name="toc.listitem.type">
26  <xsl:choose>
27    <xsl:when test="$toc.list.type = 'dl'">dt</xsl:when>
28    <xsl:otherwise>li</xsl:otherwise>
29  </xsl:choose>
30</xsl:variable>
31
32<!-- this is just hack because dl and ul aren't completely isomorphic -->
33<xsl:variable name="toc.dd.type">
34  <xsl:choose>
35    <xsl:when test="$toc.list.type = 'dl'">dd</xsl:when>
36    <xsl:otherwise></xsl:otherwise>
37  </xsl:choose>
38</xsl:variable>
39
40<xsl:template name="set.toc">
41  <xsl:variable name="nodes" select="book|setindex"/>
42  <xsl:if test="$nodes">
43    <div class="toc">
44      <p>
45        <b>
46          <xsl:call-template name="gentext">
47            <xsl:with-param name="key">TableofContents</xsl:with-param>
48          </xsl:call-template>
49        </b>
50      </p>
51      <xsl:element name="{$toc.list.type}">
52        <xsl:apply-templates select="$nodes" mode="toc"/>
53      </xsl:element>
54    </div>
55  </xsl:if>
56</xsl:template>
57
58<xsl:template name="division.toc">
59  <xsl:if test="$generate.division.toc != 0">
60    <xsl:variable name="nodes" select="part|reference
61                                       |preface|chapter|appendix
62                                       |article
63                                       |bibliography|glossary|index
64                                       |refentry
65                                       |bridgehead"/>
66    <xsl:if test="$nodes">
67      <div class="toc">
68        <p>
69          <b>
70           <xsl:call-template name="gentext">
71             <xsl:with-param name="key">TableofContents</xsl:with-param>
72           </xsl:call-template>
73          </b>
74        </p>
75        <xsl:element name="{$toc.list.type}">
76          <xsl:apply-templates select="$nodes" mode="toc"/>
77        </xsl:element>
78      </div>
79    </xsl:if>
80  </xsl:if>
81</xsl:template>
82
83<xsl:template name="component.toc">
84  <xsl:if test="$generate.component.toc != 0">
85    <xsl:variable name="nodes" select="section|sect1|refentry
86                                       |article|bibliography|glossary
87                                       |appendix|bridgehead[not(@renderas)]
88                                       |.//bridgehead[@renderas='sect1']"/>
89    <xsl:if test="$nodes">
90      <div class="toc">
91        <p>
92          <b>
93           <xsl:call-template name="gentext">
94             <xsl:with-param name="key">TableofContents</xsl:with-param>
95           </xsl:call-template>
96          </b>
97        </p>
98        <xsl:element name="{$toc.list.type}">
99          <xsl:apply-templates select="$nodes" mode="toc"/>
100        </xsl:element>
101      </div>
102    </xsl:if>
103  </xsl:if>
104</xsl:template>
105
106<xsl:template name="section.toc">
107  <xsl:variable name="nodes"
108                select="section|sect1|sect2|sect3|sect4|sect5|refentry
109                        |bridgehead"/>
110  <xsl:if test="$nodes">
111    <div class="toc">
112      <p>
113        <b>
114          <xsl:call-template name="gentext">
115            <xsl:with-param name="key">TableofContents</xsl:with-param>
116          </xsl:call-template>
117        </b>
118      </p>
119      <xsl:element name="{$toc.list.type}">
120        <xsl:apply-templates select="$nodes" mode="toc"/>
121      </xsl:element>
122    </div>
123  </xsl:if>
124</xsl:template>
125
126<!-- ==================================================================== -->
127
128<xsl:template match="book|setindex" mode="toc">
129  <xsl:variable name="nodes" select="part|reference
130                                     |preface|chapter|appendix
131                                     |article
132                                     |bibliography|glossary|index
133                                     |refentry
134                                     |bridgehead"/>
135
136  <xsl:variable name="subtoc">
137    <xsl:element name="{$toc.list.type}">
138      <xsl:apply-templates mode="toc" select="$nodes"/>
139    </xsl:element>
140  </xsl:variable>
141
142  <xsl:variable name="subtoc.list">
143    <xsl:choose>
144      <xsl:when test="$toc.dd.type = ''">
145        <xsl:copy-of select="$subtoc"/>
146      </xsl:when>
147      <xsl:otherwise>
148        <xsl:element name="{$toc.dd.type}">
149          <xsl:copy-of select="$subtoc"/>
150        </xsl:element>
151      </xsl:otherwise>
152    </xsl:choose>
153  </xsl:variable>
154
155  <xsl:element name="{$toc.listitem.type}">
156    <xsl:apply-templates select="." mode="label.markup"/>
157    <xsl:value-of select="$autotoc.label.separator"/>
158    <a>
159      <xsl:attribute name="href">
160        <xsl:call-template name="href.target"/>
161      </xsl:attribute>
162      <xsl:apply-templates select="." mode="title.markup"/>
163    </a>
164    <xsl:if test="$toc.listitem.type = 'li'
165                  and $toc.section.depth>0 and count($nodes)&gt;0">
166      <xsl:copy-of select="$subtoc.list"/>
167    </xsl:if>
168  </xsl:element>
169  <xsl:if test="$toc.listitem.type != 'li'
170                and $toc.section.depth>0 and count($nodes)&gt;0">
171    <xsl:copy-of select="$subtoc.list"/>
172  </xsl:if>
173</xsl:template>
174
175<xsl:template match="part|reference" mode="toc">
176  <xsl:variable name="nodes" select="appendix|chapter|article
177                                     |index|glossary|bibliography
178                                     |preface|reference|refentry
179                                     |bridgehead"/>
180
181  <xsl:variable name="subtoc">
182    <xsl:element name="{$toc.list.type}">
183      <xsl:apply-templates select="$nodes" mode="toc"/>
184    </xsl:element>
185  </xsl:variable>
186
187  <xsl:variable name="subtoc.list">
188    <xsl:choose>
189      <xsl:when test="$toc.dd.type = ''">
190        <xsl:copy-of select="$subtoc"/>
191      </xsl:when>
192      <xsl:otherwise>
193        <xsl:element name="{$toc.dd.type}">
194          <xsl:copy-of select="$subtoc"/>
195        </xsl:element>
196      </xsl:otherwise>
197    </xsl:choose>
198  </xsl:variable>
199
200  <xsl:element name="{$toc.listitem.type}">
201    <xsl:apply-templates select="." mode="label.markup"/>
202    <xsl:value-of select="$autotoc.label.separator"/>
203    <a>
204      <xsl:attribute name="href">
205        <xsl:call-template name="href.target"/>
206      </xsl:attribute>
207      <xsl:apply-templates select="." mode="title.markup"/>
208    </a>
209    <xsl:if test="$toc.listitem.type = 'li'
210                  and $toc.section.depth>0 and count($nodes) &gt; 0">
211      <xsl:copy-of select="$subtoc.list"/>
212    </xsl:if>
213  </xsl:element>
214  <xsl:if test="$toc.listitem.type != 'li'
215                and $toc.section.depth>0 and count($nodes) &gt; 0">
216    <xsl:copy-of select="$subtoc.list"/>
217  </xsl:if>
218</xsl:template>
219
220<xsl:template match="preface|chapter|appendix|article" mode="toc">
221  <xsl:variable name="subtoc">
222    <xsl:element name="{$toc.list.type}">
223      <xsl:apply-templates select="section|sect1|bridgehead" mode="toc"/>
224    </xsl:element>
225  </xsl:variable>
226
227  <xsl:variable name="subtoc.list">
228    <xsl:choose>
229      <xsl:when test="$toc.dd.type = ''">
230        <xsl:copy-of select="$subtoc"/>
231      </xsl:when>
232      <xsl:otherwise>
233        <xsl:element name="{$toc.dd.type}">
234          <xsl:copy-of select="$subtoc"/>
235        </xsl:element>
236      </xsl:otherwise>
237    </xsl:choose>
238  </xsl:variable>
239
240  <xsl:element name="{$toc.listitem.type}">
241    <xsl:apply-templates select="." mode="label.markup"/>
242    <xsl:value-of select="$autotoc.label.separator"/>
243    <a>
244      <xsl:attribute name="href">
245        <xsl:call-template name="href.target"/>
246      </xsl:attribute>
247      <xsl:apply-templates select="." mode="title.markup"/>
248    </a>
249    <xsl:if test="$toc.listitem.type = 'li'
250                  and $toc.section.depth>0 and section|sect1">
251      <xsl:copy-of select="$subtoc.list"/>
252    </xsl:if>
253  </xsl:element>
254  <xsl:if test="$toc.listitem.type != 'li'
255                and $toc.section.depth>0 and section|sect1">
256    <xsl:copy-of select="$subtoc.list"/>
257  </xsl:if>
258</xsl:template>
259
260<xsl:template match="sect1" mode="toc">
261  <xsl:variable name="subtoc">
262    <xsl:element name="{$toc.list.type}">
263      <xsl:apply-templates select="sect2|bridgehead" mode="toc"/>
264    </xsl:element>
265  </xsl:variable>
266
267  <xsl:variable name="subtoc.list">
268    <xsl:choose>
269      <xsl:when test="$toc.dd.type = ''">
270        <xsl:copy-of select="$subtoc"/>
271      </xsl:when>
272      <xsl:otherwise>
273        <xsl:element name="{$toc.dd.type}">
274          <xsl:copy-of select="$subtoc"/>
275        </xsl:element>
276      </xsl:otherwise>
277    </xsl:choose>
278  </xsl:variable>
279
280  <xsl:element name="{$toc.listitem.type}">
281    <xsl:apply-templates select="." mode="label.markup"/>
282    <xsl:value-of select="$autotoc.label.separator"/>
283    <a>
284      <xsl:attribute name="href">
285        <xsl:call-template name="href.target"/>
286      </xsl:attribute>
287      <xsl:apply-templates select="." mode="title.markup"/>
288    </a>
289    <xsl:if test="$toc.listitem.type = 'li'
290                  and $toc.section.depth>1 and sect2">
291      <xsl:copy-of select="$subtoc.list"/>
292    </xsl:if>
293  </xsl:element>
294  <xsl:if test="$toc.listitem.type != 'li'
295                and $toc.section.depth>1 and sect2">
296    <xsl:copy-of select="$subtoc.list"/>
297  </xsl:if>
298</xsl:template>
299
300<xsl:template match="sect2" mode="toc">
301  <xsl:variable name="subtoc">
302    <xsl:element name="{$toc.list.type}">
303      <xsl:apply-templates select="sect3|bridgehead" mode="toc"/>
304    </xsl:element>
305  </xsl:variable>
306
307  <xsl:variable name="subtoc.list">
308    <xsl:choose>
309      <xsl:when test="$toc.dd.type = ''">
310        <xsl:copy-of select="$subtoc"/>
311      </xsl:when>
312      <xsl:otherwise>
313        <xsl:element name="{$toc.dd.type}">
314          <xsl:copy-of select="$subtoc"/>
315        </xsl:element>
316      </xsl:otherwise>
317    </xsl:choose>
318  </xsl:variable>
319
320  <xsl:element name="{$toc.listitem.type}">
321    <xsl:apply-templates select="." mode="label.markup"/>
322    <xsl:value-of select="$autotoc.label.separator"/>
323    <a>
324      <xsl:attribute name="href">
325        <xsl:call-template name="href.target"/>
326      </xsl:attribute>
327      <xsl:apply-templates select="." mode="title.markup"/>
328    </a>
329    <xsl:if test="$toc.listitem.type = 'li'
330                  and $toc.section.depth>2 and sect3">
331      <xsl:copy-of select="$subtoc.list"/>
332    </xsl:if>
333  </xsl:element>
334  <xsl:if test="$toc.listitem.type != 'li'
335                and $toc.section.depth>2 and sect3">
336    <xsl:copy-of select="$subtoc.list"/>
337  </xsl:if>
338</xsl:template>
339
340<xsl:template match="sect3" mode="toc">
341  <xsl:variable name="subtoc">
342    <xsl:element name="{$toc.list.type}">
343      <xsl:apply-templates select="sect4|bridgehead" mode="toc"/>
344    </xsl:element>
345  </xsl:variable>
346
347  <xsl:variable name="subtoc.list">
348    <xsl:choose>
349      <xsl:when test="$toc.dd.type = ''">
350        <xsl:copy-of select="$subtoc"/>
351      </xsl:when>
352      <xsl:otherwise>
353        <xsl:element name="{$toc.dd.type}">
354          <xsl:copy-of select="$subtoc"/>
355        </xsl:element>
356      </xsl:otherwise>
357    </xsl:choose>
358  </xsl:variable>
359
360  <xsl:element name="{$toc.listitem.type}">
361    <xsl:apply-templates select="." mode="label.markup"/>
362    <xsl:value-of select="$autotoc.label.separator"/>
363    <a>
364      <xsl:attribute name="href">
365        <xsl:call-template name="href.target"/>
366      </xsl:attribute>
367      <xsl:apply-templates select="." mode="title.markup"/>
368    </a>
369    <xsl:if test="$toc.listitem.type = 'li'
370                  and $toc.section.depth>3 and sect4">
371      <xsl:copy-of select="$subtoc.list"/>
372    </xsl:if>
373  </xsl:element>
374  <xsl:if test="$toc.listitem.type != 'li'
375                and $toc.section.depth>3 and sect4">
376    <xsl:copy-of select="$subtoc.list"/>
377  </xsl:if>
378</xsl:template>
379
380<xsl:template match="sect4" mode="toc">
381  <xsl:variable name="subtoc">
382    <xsl:element name="{$toc.list.type}">
383      <xsl:apply-templates select="sect5|bridgehead" mode="toc"/>
384    </xsl:element>
385  </xsl:variable>
386
387  <xsl:variable name="subtoc.list">
388    <xsl:choose>
389      <xsl:when test="$toc.dd.type = ''">
390        <xsl:copy-of select="$subtoc"/>
391      </xsl:when>
392      <xsl:otherwise>
393        <xsl:element name="{$toc.dd.type}">
394          <xsl:copy-of select="$subtoc"/>
395        </xsl:element>
396      </xsl:otherwise>
397    </xsl:choose>
398  </xsl:variable>
399
400  <xsl:element name="{$toc.listitem.type}">
401    <xsl:apply-templates select="." mode="label.markup"/>
402    <xsl:value-of select="$autotoc.label.separator"/>
403    <a>
404      <xsl:attribute name="href">
405        <xsl:call-template name="href.target"/>
406      </xsl:attribute>
407      <xsl:apply-templates select="." mode="title.markup"/>
408    </a>
409    <xsl:if test="$toc.listitem.type = 'li'
410                  and $toc.section.depth>4 and sect5">
411      <xsl:copy-of select="$subtoc.list"/>
412    </xsl:if>
413  </xsl:element>
414  <xsl:if test="$toc.listitem.type != 'li'
415                and $toc.section.depth>4 and sect5">
416    <xsl:copy-of select="$subtoc.list"/>
417  </xsl:if>
418</xsl:template>
419
420<xsl:template match="sect5" mode="toc">
421  <xsl:element name="{$toc.listitem.type}">
422    <xsl:apply-templates select="." mode="label.markup"/>
423    <xsl:value-of select="$autotoc.label.separator"/>
424    <a>
425      <xsl:attribute name="href">
426        <xsl:call-template name="href.target"/>
427      </xsl:attribute>
428      <xsl:apply-templates select="." mode="title.markup"/>
429    </a>
430  </xsl:element>
431</xsl:template>
432
433<xsl:template match="section" mode="toc">
434  <xsl:variable name="subtoc">
435    <xsl:element name="{$toc.list.type}">
436      <xsl:apply-templates select="section|bridgehead" mode="toc"/>
437    </xsl:element>
438  </xsl:variable>
439
440  <xsl:variable name="subtoc.list">
441    <xsl:choose>
442      <xsl:when test="$toc.dd.type = ''">
443        <xsl:copy-of select="$subtoc"/>
444      </xsl:when>
445      <xsl:otherwise>
446        <xsl:element name="{$toc.dd.type}">
447          <xsl:copy-of select="$subtoc"/>
448        </xsl:element>
449      </xsl:otherwise>
450    </xsl:choose>
451  </xsl:variable>
452
453  <xsl:variable name="toodeep">
454    <xsl:choose>
455      <!-- if the depth is less than 2, we're already deep enough -->
456      <xsl:when test="$toc.section.depth &lt; 2">yes</xsl:when>
457      <!-- if the current section has n-1 section ancestors -->
458      <!-- then we've already reached depth n -->
459      <xsl:when test="ancestor::section[position()=$toc.section.depth - 1]">
460        <xsl:text>yes</xsl:text>
461      </xsl:when>
462      <!-- otherwise, keep going -->
463      <xsl:otherwise>no</xsl:otherwise>
464    </xsl:choose>
465  </xsl:variable>
466
467  <xsl:element name="{$toc.listitem.type}">
468    <xsl:apply-templates select="." mode="label.markup"/>
469    <xsl:value-of select="$autotoc.label.separator"/>
470    <a>
471      <xsl:attribute name="href">
472        <xsl:call-template name="href.target"/>
473      </xsl:attribute>
474      <xsl:apply-templates select="." mode="title.markup"/>
475    </a>
476    <xsl:if test="$toc.listitem.type = 'li'
477                  and $toodeep='no' and section">
478      <xsl:copy-of select="$subtoc.list"/>
479    </xsl:if>
480  </xsl:element>
481  <xsl:if test="$toc.listitem.type != 'li'
482                and $toodeep='no' and section">
483    <xsl:copy-of select="$subtoc.list"/>
484  </xsl:if>
485</xsl:template>
486
487<xsl:template match="bridgehead" mode="toc">
488  <xsl:if test="$bridgehead.in.toc != 0">
489    <xsl:element name="{$toc.listitem.type}">
490      <xsl:apply-templates select="." mode="label.markup"/>
491      <xsl:value-of select="$autotoc.label.separator"/>
492      <a>
493        <xsl:attribute name="href">
494          <xsl:call-template name="href.target"/>
495        </xsl:attribute>
496        <xsl:apply-templates/>
497      </a>
498    </xsl:element>
499  </xsl:if>
500</xsl:template>
501
502<xsl:template match="bibliography|glossary" mode="toc">
503  <xsl:element name="{$toc.listitem.type}">
504    <a>
505      <xsl:attribute name="href">
506        <xsl:call-template name="href.target"/>
507      </xsl:attribute>
508      <xsl:apply-templates select="." mode="title.markup"/>
509    </a>
510  </xsl:element>
511</xsl:template>
512
513<xsl:template match="index" mode="toc">
514  <!-- If the index tag is empty, don't point at it from the TOC -->
515  <xsl:if test="* or $generate.index">
516    <xsl:element name="{$toc.listitem.type}">
517      <a>
518        <xsl:attribute name="href">
519          <xsl:call-template name="href.target"/>
520        </xsl:attribute>
521        <xsl:apply-templates select="." mode="title.markup"/>
522      </a>
523    </xsl:element>
524  </xsl:if>
525</xsl:template>
526
527<xsl:template match="refentry" mode="toc">
528  <xsl:variable name="refmeta" select=".//refmeta"/>
529  <xsl:variable name="refentrytitle" select="$refmeta//refentrytitle"/>
530  <xsl:variable name="refnamediv" select=".//refnamediv"/>
531  <xsl:variable name="refname" select="$refnamediv//refname"/>
532  <xsl:variable name="title">
533    <xsl:choose>
534      <xsl:when test="$refentrytitle">
535        <xsl:apply-templates select="$refentrytitle[1]" mode="title"/>
536      </xsl:when>
537      <xsl:when test="$refname">
538        <xsl:apply-templates select="$refname[1]" mode="title"/>
539      </xsl:when>
540      <xsl:otherwise></xsl:otherwise>
541    </xsl:choose>
542  </xsl:variable>
543
544  <xsl:element name="{$toc.listitem.type}">
545    <a>
546      <xsl:attribute name="href">
547        <xsl:call-template name="href.target"/>
548      </xsl:attribute>
549      <xsl:copy-of select="$title"/>
550    </a>
551  </xsl:element>
552</xsl:template>
553
554<xsl:template match="title" mode="toc">
555  <a>
556    <xsl:attribute name="href">
557      <xsl:call-template name="href.target">
558        <xsl:with-param name="object" select=".."/>
559      </xsl:call-template>
560    </xsl:attribute>
561    <xsl:apply-templates/>
562  </a>
563</xsl:template>
564
565</xsl:stylesheet>
566
567