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: autotoc.xsl,v 1.37 2006/05/04 12:21:28 kosek Exp $
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:variable name="toc.listitem.type">
16  <xsl:choose>
17    <xsl:when test="$toc.list.type = 'dl'">dt</xsl:when>
18    <xsl:otherwise>li</xsl:otherwise>
19  </xsl:choose>
20</xsl:variable>
21
22<!-- this is just hack because dl and ul aren't completely isomorphic -->
23<xsl:variable name="toc.dd.type">
24  <xsl:choose>
25    <xsl:when test="$toc.list.type = 'dl'">dd</xsl:when>
26    <xsl:otherwise></xsl:otherwise>
27  </xsl:choose>
28</xsl:variable>
29
30<xsl:template name="make.toc">
31  <xsl:param name="toc-context" select="."/>
32  <xsl:param name="toc.title.p" select="true()"/>
33  <xsl:param name="nodes" select="/NOT-AN-ELEMENT"/>
34
35  <xsl:variable name="toc.title">
36    <xsl:if test="$toc.title.p">
37      <p>
38        <b>
39          <xsl:call-template name="gentext">
40            <xsl:with-param name="key">TableofContents</xsl:with-param>
41          </xsl:call-template>
42        </b>
43      </p>
44    </xsl:if>
45  </xsl:variable>
46
47  <xsl:choose>
48    <xsl:when test="$manual.toc != ''">
49      <xsl:variable name="id">
50        <xsl:call-template name="object.id"/>
51      </xsl:variable>
52      <xsl:variable name="toc" select="document($manual.toc, .)"/>
53      <xsl:variable name="tocentry" select="$toc//tocentry[@linkend=$id]"/>
54      <xsl:if test="$tocentry and $tocentry/*">
55        <div class="toc">
56          <xsl:copy-of select="$toc.title"/>
57          <xsl:element name="{$toc.list.type}">
58            <xsl:call-template name="manual-toc">
59              <xsl:with-param name="tocentry" select="$tocentry/*[1]"/>
60            </xsl:call-template>
61          </xsl:element>
62        </div>
63      </xsl:if>
64    </xsl:when>
65    <xsl:otherwise>
66      <xsl:if test="$nodes">
67        <div class="toc">
68          <xsl:copy-of select="$toc.title"/>
69          <xsl:element name="{$toc.list.type}">
70            <xsl:apply-templates select="$nodes" mode="toc">
71              <xsl:with-param name="toc-context" select="$toc-context"/>
72            </xsl:apply-templates>
73          </xsl:element>
74        </div>
75      </xsl:if>
76    </xsl:otherwise>
77  </xsl:choose>
78</xsl:template>
79
80<xsl:template name="make.lots">
81  <xsl:param name="toc.params" select="''"/>
82  <xsl:param name="toc"/>
83
84  <xsl:if test="contains($toc.params, 'toc')">
85    <xsl:copy-of select="$toc"/>
86  </xsl:if>
87
88  <xsl:if test="contains($toc.params, 'figure')">
89    <xsl:call-template name="list.of.titles">
90      <xsl:with-param name="titles" select="'figure'"/>
91      <xsl:with-param name="nodes" select=".//figure"/>
92    </xsl:call-template>
93  </xsl:if>
94
95  <xsl:if test="contains($toc.params, 'table')">
96    <xsl:call-template name="list.of.titles">
97      <xsl:with-param name="titles" select="'table'"/>
98      <xsl:with-param name="nodes" select=".//table"/>
99    </xsl:call-template>
100  </xsl:if>
101
102  <xsl:if test="contains($toc.params, 'example')">
103    <xsl:call-template name="list.of.titles">
104      <xsl:with-param name="titles" select="'example'"/>
105      <xsl:with-param name="nodes" select=".//example"/>
106    </xsl:call-template>
107  </xsl:if>
108
109  <xsl:if test="contains($toc.params, 'equation')">
110    <xsl:call-template name="list.of.titles">
111      <xsl:with-param name="titles" select="'equation'"/>
112      <xsl:with-param name="nodes" select=".//equation[title]"/>
113    </xsl:call-template>
114  </xsl:if>
115
116  <xsl:if test="contains($toc.params, 'procedure')">
117    <xsl:call-template name="list.of.titles">
118      <xsl:with-param name="titles" select="'procedure'"/>
119      <xsl:with-param name="nodes" select=".//procedure[title]"/>
120    </xsl:call-template>
121  </xsl:if>
122</xsl:template>
123
124<!-- ====================================================================== -->
125
126<xsl:template name="set.toc">
127  <xsl:param name="toc-context" select="."/>
128  <xsl:param name="toc.title.p" select="true()"/>
129
130  <xsl:call-template name="make.toc">
131    <xsl:with-param name="toc-context" select="$toc-context"/>
132    <xsl:with-param name="toc.title.p" select="$toc.title.p"/>
133    <xsl:with-param name="nodes" select="book|setindex"/>
134  </xsl:call-template>
135</xsl:template>
136
137<xsl:template name="division.toc">
138  <xsl:param name="toc-context" select="."/>
139  <xsl:param name="toc.title.p" select="true()"/>
140
141  <xsl:call-template name="make.toc">
142    <xsl:with-param name="toc-context" select="$toc-context"/>
143    <xsl:with-param name="toc.title.p" select="$toc.title.p"/>
144    <xsl:with-param name="nodes" select="part|reference
145                                         |preface|chapter|appendix
146                                         |article
147                                         |bibliography|glossary|index
148                                         |refentry
149                                         |bridgehead[$bridgehead.in.toc != 0]"/>
150
151  </xsl:call-template>
152</xsl:template>
153
154<xsl:template name="component.toc">
155  <xsl:param name="toc-context" select="."/>
156  <xsl:param name="toc.title.p" select="true()"/>
157
158  <xsl:call-template name="make.toc">
159    <xsl:with-param name="toc-context" select="$toc-context"/>
160    <xsl:with-param name="toc.title.p" select="$toc.title.p"/>
161    <xsl:with-param name="nodes" select="section|sect1
162                                         |simplesect[$simplesect.in.toc != 0]
163                                         |refentry
164                                         |article|bibliography|glossary
165                                         |appendix|index
166                                         |bridgehead[not(@renderas)
167                                                     and $bridgehead.in.toc != 0]
168                                         |.//bridgehead[@renderas='sect1'
169                                                        and $bridgehead.in.toc != 0]"/>
170  </xsl:call-template>
171</xsl:template>
172
173<xsl:template name="component.toc.separator">
174  <!-- Customize to output something between
175       component.toc and first output -->
176</xsl:template>
177
178<xsl:template name="section.toc">
179  <xsl:param name="toc-context" select="."/>
180  <xsl:param name="toc.title.p" select="true()"/>
181
182  <xsl:call-template name="make.toc">
183    <xsl:with-param name="toc-context" select="$toc-context"/>
184    <xsl:with-param name="toc.title.p" select="$toc.title.p"/>
185    <xsl:with-param name="nodes"
186                    select="section|sect1|sect2|sect3|sect4|sect5|refentry
187                           |bridgehead[$bridgehead.in.toc != 0]"/>
188
189  </xsl:call-template>
190</xsl:template>
191
192<xsl:template name="section.toc.separator">
193  <!-- Customize to output something between
194       section.toc and first output -->
195</xsl:template>
196<!-- ==================================================================== -->
197
198<xsl:template name="subtoc">
199  <xsl:param name="toc-context" select="."/>
200  <xsl:param name="nodes" select="NOT-AN-ELEMENT"/>
201
202  <xsl:variable name="subtoc">
203    <xsl:element name="{$toc.list.type}">
204      <xsl:apply-templates mode="toc" select="$nodes">
205        <xsl:with-param name="toc-context" select="$toc-context"/>
206      </xsl:apply-templates>
207    </xsl:element>
208  </xsl:variable>
209
210  <xsl:variable name="depth">
211    <xsl:choose>
212      <xsl:when test="local-name(.) = 'section'">
213        <xsl:value-of select="count(ancestor::section) + 1"/>
214      </xsl:when>
215      <xsl:when test="local-name(.) = 'sect1'">1</xsl:when>
216      <xsl:when test="local-name(.) = 'sect2'">2</xsl:when>
217      <xsl:when test="local-name(.) = 'sect3'">3</xsl:when>
218      <xsl:when test="local-name(.) = 'sect4'">4</xsl:when>
219      <xsl:when test="local-name(.) = 'sect5'">5</xsl:when>
220      <xsl:when test="local-name(.) = 'refsect1'">1</xsl:when>
221      <xsl:when test="local-name(.) = 'refsect2'">2</xsl:when>
222      <xsl:when test="local-name(.) = 'refsect3'">3</xsl:when>
223      <xsl:when test="local-name(.) = 'simplesect'">
224        <!-- sigh... -->
225        <xsl:choose>
226          <xsl:when test="local-name(..) = 'section'">
227            <xsl:value-of select="count(ancestor::section)"/>
228          </xsl:when>
229          <xsl:when test="local-name(..) = 'sect1'">2</xsl:when>
230          <xsl:when test="local-name(..) = 'sect2'">3</xsl:when>
231          <xsl:when test="local-name(..) = 'sect3'">4</xsl:when>
232          <xsl:when test="local-name(..) = 'sect4'">5</xsl:when>
233          <xsl:when test="local-name(..) = 'sect5'">6</xsl:when>
234          <xsl:when test="local-name(..) = 'refsect1'">2</xsl:when>
235          <xsl:when test="local-name(..) = 'refsect2'">3</xsl:when>
236          <xsl:when test="local-name(..) = 'refsect3'">4</xsl:when>
237          <xsl:otherwise>1</xsl:otherwise>
238        </xsl:choose>
239      </xsl:when>
240      <xsl:otherwise>0</xsl:otherwise>
241    </xsl:choose>
242  </xsl:variable>
243
244  <xsl:variable name="depth.from.context" select="count(ancestor::*)-count($toc-context/ancestor::*)"/>
245
246  <xsl:variable name="subtoc.list">
247    <xsl:choose>
248      <xsl:when test="$toc.dd.type = ''">
249        <xsl:copy-of select="$subtoc"/>
250      </xsl:when>
251      <xsl:otherwise>
252        <xsl:element name="{$toc.dd.type}">
253          <xsl:copy-of select="$subtoc"/>
254        </xsl:element>
255      </xsl:otherwise>
256    </xsl:choose>
257  </xsl:variable>
258
259  <xsl:element name="{$toc.listitem.type}">
260    <xsl:call-template name="toc.line">
261      <xsl:with-param name="toc-context" select="$toc-context"/>
262    </xsl:call-template>
263    <xsl:if test="$toc.listitem.type = 'li'
264                  and $toc.section.depth > $depth and count($nodes)&gt;0
265                  and $toc.max.depth > $depth.from.context">
266      <xsl:copy-of select="$subtoc.list"/>
267    </xsl:if>
268  </xsl:element>
269  <xsl:if test="$toc.listitem.type != 'li'
270                and $toc.section.depth > $depth and count($nodes)&gt;0
271                and $toc.max.depth > $depth.from.context">
272    <xsl:copy-of select="$subtoc.list"/>
273  </xsl:if>
274</xsl:template>
275
276<xsl:template name="toc.line">
277  <xsl:param name="toc-context" select="."/>
278  <xsl:param name="depth" select="1"/>
279  <xsl:param name="depth.from.context" select="8"/>
280
281 <span>
282  <xsl:attribute name="class"><xsl:value-of select="local-name(.)"/></xsl:attribute>
283
284  <!-- * if $autotoc.label.in.hyperlink is zero, then output the label -->
285  <!-- * before the hyperlinked title (as the DSSSL stylesheet does) -->
286  <xsl:if test="$autotoc.label.in.hyperlink = 0">
287    <xsl:variable name="label">
288      <xsl:apply-templates select="." mode="label.markup"/>
289    </xsl:variable>
290    <xsl:copy-of select="$label"/>
291    <xsl:if test="$label != ''">
292      <xsl:value-of select="$autotoc.label.separator"/>
293    </xsl:if>
294  </xsl:if>
295
296  <a>
297    <xsl:attribute name="href">
298      <xsl:call-template name="href.target">
299        <xsl:with-param name="context" select="$toc-context"/>
300      </xsl:call-template>
301    </xsl:attribute>
302    
303  <!-- * if $autotoc.label.in.hyperlink is non-zero, then output the label -->
304  <!-- * as part of the hyperlinked title -->
305  <xsl:if test="not($autotoc.label.in.hyperlink = 0)">
306    <xsl:variable name="label">
307      <xsl:apply-templates select="." mode="label.markup"/>
308    </xsl:variable>
309    <xsl:copy-of select="$label"/>
310    <xsl:if test="$label != ''">
311      <xsl:value-of select="$autotoc.label.separator"/>
312    </xsl:if>
313  </xsl:if>
314
315    <xsl:apply-templates select="." mode="titleabbrev.markup"/>
316  </a>
317  </span>
318</xsl:template>
319
320<xsl:template match="book" mode="toc">
321  <xsl:param name="toc-context" select="."/>
322
323  <xsl:call-template name="subtoc">
324    <xsl:with-param name="toc-context" select="$toc-context"/>
325    <xsl:with-param name="nodes" select="part|reference
326                                         |preface|chapter|appendix
327                                         |article
328                                         |bibliography|glossary|index
329                                         |refentry
330                                         |bridgehead[$bridgehead.in.toc != 0]"/>
331  </xsl:call-template>
332</xsl:template>
333
334<xsl:template match="setindex" mode="toc">
335  <xsl:param name="toc-context" select="."/>
336
337  <!-- If the setindex tag is not empty, it should be it in the TOC -->
338  <xsl:if test="* or $generate.index != 0">
339    <xsl:call-template name="subtoc">
340      <xsl:with-param name="toc-context" select="$toc-context"/>
341    </xsl:call-template>
342  </xsl:if>
343</xsl:template>
344
345<xsl:template match="part|reference" mode="toc">
346  <xsl:param name="toc-context" select="."/>
347
348  <xsl:call-template name="subtoc">
349    <xsl:with-param name="toc-context" select="$toc-context"/>
350    <xsl:with-param name="nodes" select="appendix|chapter|article
351                                         |index|glossary|bibliography
352                                         |preface|reference|refentry
353                                         |bridgehead[$bridgehead.in.toc != 0]"/>
354  </xsl:call-template>
355</xsl:template>
356
357<xsl:template match="preface|chapter|appendix|article" mode="toc">
358  <xsl:param name="toc-context" select="."/>
359
360  <xsl:call-template name="subtoc">
361    <xsl:with-param name="toc-context" select="$toc-context"/>
362    <xsl:with-param name="nodes" select="section|sect1
363                                         |simplesect[$simplesect.in.toc != 0]
364                                         |refentry
365                                         |glossary|bibliography|index
366                                         |bridgehead[$bridgehead.in.toc != 0]"/>
367  </xsl:call-template>
368</xsl:template>
369
370<xsl:template match="sect1" mode="toc">
371  <xsl:param name="toc-context" select="."/>
372  <xsl:call-template name="subtoc">
373    <xsl:with-param name="toc-context" select="$toc-context"/>
374    <xsl:with-param name="nodes" select="sect2
375                                         |bridgehead[$bridgehead.in.toc != 0]"/>
376  </xsl:call-template>
377</xsl:template>
378
379<xsl:template match="sect2" mode="toc">
380  <xsl:param name="toc-context" select="."/>
381
382  <xsl:call-template name="subtoc">
383    <xsl:with-param name="toc-context" select="$toc-context"/>
384    <xsl:with-param name="nodes" select="sect3
385                                         |bridgehead[$bridgehead.in.toc != 0]"/>
386  </xsl:call-template>
387</xsl:template>
388
389<xsl:template match="sect3" mode="toc">
390  <xsl:param name="toc-context" select="."/>
391
392  <xsl:call-template name="subtoc">
393    <xsl:with-param name="toc-context" select="$toc-context"/>
394    <xsl:with-param name="nodes" select="sect4
395                                         |bridgehead[$bridgehead.in.toc != 0]"/>
396  </xsl:call-template>
397</xsl:template>
398
399<xsl:template match="sect4" mode="toc">
400  <xsl:param name="toc-context" select="."/>
401
402  <xsl:call-template name="subtoc">
403    <xsl:with-param name="toc-context" select="$toc-context"/>
404    <xsl:with-param name="nodes" select="sect5
405                                         |bridgehead[$bridgehead.in.toc != 0]"/>
406  </xsl:call-template>
407</xsl:template>
408
409<xsl:template match="sect5" mode="toc">
410  <xsl:param name="toc-context" select="."/>
411
412  <xsl:call-template name="subtoc">
413    <xsl:with-param name="toc-context" select="$toc-context"/>
414  </xsl:call-template>
415</xsl:template>
416
417<xsl:template match="simplesect" mode="toc">
418  <xsl:param name="toc-context" select="."/>
419
420  <xsl:call-template name="subtoc">
421    <xsl:with-param name="toc-context" select="$toc-context"/>
422  </xsl:call-template>
423</xsl:template>
424
425<xsl:template match="section" mode="toc">
426  <xsl:param name="toc-context" select="."/>
427
428  <xsl:call-template name="subtoc">
429    <xsl:with-param name="toc-context" select="$toc-context"/>
430    <xsl:with-param name="nodes" select="section
431                                         |bridgehead[$bridgehead.in.toc != 0]"/>
432  </xsl:call-template>
433</xsl:template>
434
435<xsl:template match="bridgehead" mode="toc">
436  <xsl:param name="toc-context" select="."/>
437
438  <xsl:if test="$bridgehead.in.toc != 0">
439    <xsl:call-template name="subtoc">
440      <xsl:with-param name="toc-context" select="$toc-context"/>
441    </xsl:call-template>
442  </xsl:if>
443</xsl:template>
444
445<xsl:template match="bibliography|glossary" mode="toc">
446  <xsl:param name="toc-context" select="."/>
447
448  <xsl:call-template name="subtoc">
449    <xsl:with-param name="toc-context" select="$toc-context"/>
450  </xsl:call-template>
451</xsl:template>
452
453<xsl:template match="index" mode="toc">
454  <xsl:param name="toc-context" select="."/>
455
456  <!-- If the index tag is not empty, it should be it in the TOC -->
457  <xsl:if test="* or $generate.index != 0">
458    <xsl:call-template name="subtoc">
459      <xsl:with-param name="toc-context" select="$toc-context"/>
460    </xsl:call-template>
461  </xsl:if>
462</xsl:template>
463
464<xsl:template match="refentry" mode="toc">
465  <xsl:param name="toc-context" select="."/>
466
467  <xsl:variable name="refmeta" select=".//refmeta"/>
468  <xsl:variable name="refentrytitle" select="$refmeta//refentrytitle"/>
469  <xsl:variable name="refnamediv" select=".//refnamediv"/>
470  <xsl:variable name="refname" select="$refnamediv//refname"/>
471  <xsl:variable name="refdesc" select="$refnamediv//refdescriptor"/>
472  <xsl:variable name="title">
473    <xsl:choose>
474      <xsl:when test="$refentrytitle">
475        <xsl:apply-templates select="$refentrytitle[1]"
476			     mode="titleabbrev.markup"/>
477      </xsl:when>
478      <xsl:when test="$refdesc">
479        <xsl:apply-templates select="$refdesc"
480			     mode="titleabbrev.markup"/>
481      </xsl:when>
482      <xsl:when test="$refname">
483        <xsl:apply-templates select="$refname[1]"
484			     mode="titleabbrev.markup"/>
485      </xsl:when>
486    </xsl:choose>
487  </xsl:variable>
488
489  <xsl:element name="{$toc.listitem.type}">
490    <span class='refentrytitle'>
491      <a>
492        <xsl:attribute name="href">
493          <xsl:call-template name="href.target"/>
494        </xsl:attribute>
495        <xsl:copy-of select="$title"/>
496      </a>
497    </span>
498    <span class='refpurpose'>
499      <xsl:if test="$annotate.toc != 0">
500        <!-- * DocBook 5 says inlinemediaobject (among other things) -->
501        <!-- * is allowed in refpurpose; so we need to run -->
502        <!-- * apply-templates on refpurpose here, instead of value-of  -->
503        <xsl:apply-templates select="refnamediv/refpurpose"/>
504      </xsl:if>
505    </span>
506  </xsl:element>
507</xsl:template>
508
509<xsl:template match="title" mode="toc">
510  <xsl:param name="toc-context" select="."/>
511
512  <a>
513    <xsl:attribute name="href">
514      <xsl:call-template name="href.target">
515        <xsl:with-param name="object" select=".."/>
516      </xsl:call-template>
517    </xsl:attribute>
518    <xsl:apply-templates/>
519  </a>
520</xsl:template>
521
522<xsl:template name="manual-toc">
523  <xsl:param name="toc-context" select="."/>
524  <xsl:param name="tocentry"/>
525
526  <!-- be careful, we don't want to change the current document to the other tree! -->
527
528  <xsl:if test="$tocentry">
529    <xsl:variable name="node" select="key('id', $tocentry/@linkend)"/>
530
531    <xsl:element name="{$toc.listitem.type}">
532      <xsl:variable name="label">
533        <xsl:apply-templates select="$node" mode="label.markup"/>
534      </xsl:variable>
535      <xsl:copy-of select="$label"/>
536      <xsl:if test="$label != ''">
537        <xsl:value-of select="$autotoc.label.separator"/>
538      </xsl:if>
539      <a>
540        <xsl:attribute name="href">
541          <xsl:call-template name="href.target">
542            <xsl:with-param name="object" select="$node"/>
543          </xsl:call-template>
544        </xsl:attribute>
545        <xsl:apply-templates select="$node" mode="titleabbrev.markup"/>
546      </a>
547    </xsl:element>
548
549    <xsl:if test="$tocentry/*">
550      <xsl:element name="{$toc.list.type}">
551        <xsl:call-template name="manual-toc">
552          <xsl:with-param name="tocentry" select="$tocentry/*[1]"/>
553        </xsl:call-template>
554      </xsl:element>
555    </xsl:if>
556
557    <xsl:if test="$tocentry/following-sibling::*">
558      <xsl:call-template name="manual-toc">
559        <xsl:with-param name="tocentry" select="$tocentry/following-sibling::*[1]"/>
560      </xsl:call-template>
561    </xsl:if>
562  </xsl:if>
563</xsl:template>
564
565<!-- ==================================================================== -->
566
567<xsl:template name="list.of.titles">
568  <xsl:param name="toc-context" select="."/>
569  <xsl:param name="titles" select="'table'"/>
570  <xsl:param name="nodes" select=".//table"/>
571
572  <xsl:if test="$nodes">
573    <div class="list-of-{$titles}s">
574      <p>
575        <b>
576          <xsl:call-template name="gentext">
577            <xsl:with-param name="key">
578              <xsl:choose>
579                <xsl:when test="$titles='table'">ListofTables</xsl:when>
580                <xsl:when test="$titles='figure'">ListofFigures</xsl:when>
581                <xsl:when test="$titles='equation'">ListofEquations</xsl:when>
582                <xsl:when test="$titles='example'">ListofExamples</xsl:when>
583                <xsl:when test="$titles='procedure'">ListofProcedures</xsl:when>
584                <xsl:otherwise>ListofUnknown</xsl:otherwise>
585              </xsl:choose>
586            </xsl:with-param>
587          </xsl:call-template>
588        </b>
589      </p>
590
591      <xsl:element name="{$toc.list.type}">
592        <xsl:apply-templates select="$nodes" mode="toc">
593          <xsl:with-param name="toc-context" select="$toc-context"/>
594        </xsl:apply-templates>
595      </xsl:element>
596    </div>
597  </xsl:if>
598</xsl:template>
599
600<xsl:template match="figure|table|example|equation|procedure" mode="toc">
601  <xsl:param name="toc-context" select="."/>
602
603  <xsl:element name="{$toc.listitem.type}">
604    <xsl:variable name="label">
605      <xsl:apply-templates select="." mode="label.markup"/>
606    </xsl:variable>
607    <xsl:copy-of select="$label"/>
608    <xsl:if test="$label != ''">
609      <xsl:value-of select="$autotoc.label.separator"/>
610    </xsl:if>
611    <a>
612      <xsl:attribute name="href">
613        <xsl:call-template name="href.target"/>
614      </xsl:attribute>
615      <xsl:apply-templates select="." mode="titleabbrev.markup"/>
616    </a>
617  </xsl:element>
618</xsl:template>
619
620</xsl:stylesheet>
621
622