2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
5 <!-- ********************************************************************
6 $Id: autotoc.xsl 8315 2009-03-11 21:33:31Z bobstayton $
7 ********************************************************************
9 This file is part of the XSL DocBook Stylesheet distribution.
10 See ../README or http://docbook.sf.net/release/xsl/current/ for
11 copyright and other information.
13 ******************************************************************** -->
15 <xsl:variable name="toc.listitem.type">
17 <xsl:when test="$toc.list.type = 'dl'">dt</xsl:when>
18 <xsl:otherwise>li</xsl:otherwise>
22 <!-- this is just hack because dl and ul aren't completely isomorphic -->
23 <xsl:variable name="toc.dd.type">
25 <xsl:when test="$toc.list.type = 'dl'">dd</xsl:when>
26 <xsl:otherwise></xsl:otherwise>
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"/>
35 <xsl:variable name="nodes.plus" select="$nodes | qandaset"/>
37 <xsl:variable name="toc.title">
38 <xsl:if test="$toc.title.p">
41 <xsl:call-template name="gentext">
42 <xsl:with-param name="key">TableofContents</xsl:with-param>
50 <xsl:when test="$manual.toc != ''">
51 <xsl:variable name="id">
52 <xsl:call-template name="object.id"/>
54 <xsl:variable name="toc" select="document($manual.toc, .)"/>
55 <xsl:variable name="tocentry" select="$toc//tocentry[@linkend=$id]"/>
56 <xsl:if test="$tocentry and $tocentry/*">
58 <xsl:copy-of select="$toc.title"/>
59 <xsl:element name="{$toc.list.type}">
60 <xsl:call-template name="manual-toc">
61 <xsl:with-param name="tocentry" select="$tocentry/*[1]"/>
69 <xsl:when test="$qanda.in.toc != 0">
70 <xsl:if test="$nodes.plus">
72 <xsl:copy-of select="$toc.title"/>
73 <xsl:element name="{$toc.list.type}">
74 <xsl:apply-templates select="$nodes.plus" mode="toc">
75 <xsl:with-param name="toc-context" select="$toc-context"/>
76 </xsl:apply-templates>
82 <xsl:if test="$nodes">
84 <xsl:copy-of select="$toc.title"/>
85 <xsl:element name="{$toc.list.type}">
86 <xsl:apply-templates select="$nodes" mode="toc">
87 <xsl:with-param name="toc-context" select="$toc-context"/>
88 </xsl:apply-templates>
99 <xsl:template name="make.lots">
100 <xsl:param name="toc.params" select="''"/>
101 <xsl:param name="toc"/>
103 <xsl:if test="contains($toc.params, 'toc')">
104 <xsl:copy-of select="$toc"/>
107 <xsl:if test="contains($toc.params, 'figure')">
108 <xsl:call-template name="list.of.titles">
109 <xsl:with-param name="titles" select="'figure'"/>
110 <xsl:with-param name="nodes" select=".//figure"/>
114 <xsl:if test="contains($toc.params, 'table')">
115 <xsl:call-template name="list.of.titles">
116 <xsl:with-param name="titles" select="'table'"/>
117 <xsl:with-param name="nodes" select=".//table"/>
121 <xsl:if test="contains($toc.params, 'example')">
122 <xsl:call-template name="list.of.titles">
123 <xsl:with-param name="titles" select="'example'"/>
124 <xsl:with-param name="nodes" select=".//example"/>
128 <xsl:if test="contains($toc.params, 'equation')">
129 <xsl:call-template name="list.of.titles">
130 <xsl:with-param name="titles" select="'equation'"/>
131 <xsl:with-param name="nodes" select=".//equation[title or info/title]"/>
135 <xsl:if test="contains($toc.params, 'procedure')">
136 <xsl:call-template name="list.of.titles">
137 <xsl:with-param name="titles" select="'procedure'"/>
138 <xsl:with-param name="nodes" select=".//procedure[title]"/>
143 <!-- ====================================================================== -->
145 <xsl:template name="set.toc">
146 <xsl:param name="toc-context" select="."/>
147 <xsl:param name="toc.title.p" select="true()"/>
149 <xsl:call-template name="make.toc">
150 <xsl:with-param name="toc-context" select="$toc-context"/>
151 <xsl:with-param name="toc.title.p" select="$toc.title.p"/>
152 <xsl:with-param name="nodes" select="book|setindex"/>
156 <xsl:template name="division.toc">
157 <xsl:param name="toc-context" select="."/>
158 <xsl:param name="toc.title.p" select="true()"/>
160 <xsl:call-template name="make.toc">
161 <xsl:with-param name="toc-context" select="$toc-context"/>
162 <xsl:with-param name="toc.title.p" select="$toc.title.p"/>
163 <xsl:with-param name="nodes" select="part|reference
164 |preface|chapter|appendix
166 |bibliography|glossary|index
168 |bridgehead[$bridgehead.in.toc != 0]"/>
173 <xsl:template name="component.toc">
174 <xsl:param name="toc-context" select="."/>
175 <xsl:param name="toc.title.p" select="true()"/>
177 <xsl:call-template name="make.toc">
178 <xsl:with-param name="toc-context" select="$toc-context"/>
179 <xsl:with-param name="toc.title.p" select="$toc.title.p"/>
180 <xsl:with-param name="nodes" select="section|sect1
181 |simplesect[$simplesect.in.toc != 0]
183 |article|bibliography|glossary
185 |bridgehead[not(@renderas)
186 and $bridgehead.in.toc != 0]
187 |.//bridgehead[@renderas='sect1'
188 and $bridgehead.in.toc != 0]"/>
192 <xsl:template name="component.toc.separator">
193 <!-- Customize to output something between
194 component.toc and first output -->
197 <xsl:template name="section.toc">
198 <xsl:param name="toc-context" select="."/>
199 <xsl:param name="toc.title.p" select="true()"/>
201 <xsl:call-template name="make.toc">
202 <xsl:with-param name="toc-context" select="$toc-context"/>
203 <xsl:with-param name="toc.title.p" select="$toc.title.p"/>
204 <xsl:with-param name="nodes"
205 select="section|sect1|sect2|sect3|sect4|sect5|refentry
206 |bridgehead[$bridgehead.in.toc != 0]"/>
211 <xsl:template name="section.toc.separator">
212 <!-- Customize to output something between
213 section.toc and first output -->
215 <!-- ==================================================================== -->
217 <xsl:template name="subtoc">
218 <xsl:param name="toc-context" select="."/>
219 <xsl:param name="nodes" select="NOT-AN-ELEMENT"/>
221 <xsl:variable name="nodes.plus" select="$nodes | qandaset"/>
223 <xsl:variable name="subtoc">
224 <xsl:element name="{$toc.list.type}">
226 <xsl:when test="$qanda.in.toc != 0">
227 <xsl:apply-templates mode="toc" select="$nodes.plus">
228 <xsl:with-param name="toc-context" select="$toc-context"/>
229 </xsl:apply-templates>
232 <xsl:apply-templates mode="toc" select="$nodes">
233 <xsl:with-param name="toc-context" select="$toc-context"/>
234 </xsl:apply-templates>
240 <xsl:variable name="depth">
242 <xsl:when test="local-name(.) = 'section'">
243 <xsl:value-of select="count(ancestor::section) + 1"/>
245 <xsl:when test="local-name(.) = 'sect1'">1</xsl:when>
246 <xsl:when test="local-name(.) = 'sect2'">2</xsl:when>
247 <xsl:when test="local-name(.) = 'sect3'">3</xsl:when>
248 <xsl:when test="local-name(.) = 'sect4'">4</xsl:when>
249 <xsl:when test="local-name(.) = 'sect5'">5</xsl:when>
250 <xsl:when test="local-name(.) = 'refsect1'">1</xsl:when>
251 <xsl:when test="local-name(.) = 'refsect2'">2</xsl:when>
252 <xsl:when test="local-name(.) = 'refsect3'">3</xsl:when>
253 <xsl:when test="local-name(.) = 'simplesect'">
256 <xsl:when test="local-name(..) = 'section'">
257 <xsl:value-of select="count(ancestor::section)"/>
259 <xsl:when test="local-name(..) = 'sect1'">2</xsl:when>
260 <xsl:when test="local-name(..) = 'sect2'">3</xsl:when>
261 <xsl:when test="local-name(..) = 'sect3'">4</xsl:when>
262 <xsl:when test="local-name(..) = 'sect4'">5</xsl:when>
263 <xsl:when test="local-name(..) = 'sect5'">6</xsl:when>
264 <xsl:when test="local-name(..) = 'refsect1'">2</xsl:when>
265 <xsl:when test="local-name(..) = 'refsect2'">3</xsl:when>
266 <xsl:when test="local-name(..) = 'refsect3'">4</xsl:when>
267 <xsl:otherwise>1</xsl:otherwise>
270 <xsl:otherwise>0</xsl:otherwise>
274 <xsl:variable name="depth.from.context" select="count(ancestor::*)-count($toc-context/ancestor::*)"/>
276 <xsl:variable name="subtoc.list">
278 <xsl:when test="$toc.dd.type = ''">
279 <xsl:copy-of select="$subtoc"/>
282 <xsl:element name="{$toc.dd.type}">
283 <xsl:copy-of select="$subtoc"/>
289 <xsl:element name="{$toc.listitem.type}">
290 <xsl:call-template name="toc.line">
291 <xsl:with-param name="toc-context" select="$toc-context"/>
293 <xsl:if test="$toc.listitem.type = 'li'
294 and $toc.section.depth > $depth and
295 ( ($qanda.in.toc = 0 and count($nodes)>0) or
296 ($qanda.in.toc != 0 and count($nodes.plus)>0) )
297 and $toc.max.depth > $depth.from.context">
298 <xsl:copy-of select="$subtoc.list"/>
301 <xsl:if test="$toc.listitem.type != 'li'
302 and $toc.section.depth > $depth and
303 ( ($qanda.in.toc = 0 and count($nodes)>0) or
304 ($qanda.in.toc != 0 and count($nodes.plus)>0) )
305 and $toc.max.depth > $depth.from.context">
306 <xsl:copy-of select="$subtoc.list"/>
310 <xsl:template name="toc.line">
311 <xsl:param name="toc-context" select="."/>
312 <xsl:param name="depth" select="1"/>
313 <xsl:param name="depth.from.context" select="8"/>
316 <xsl:attribute name="class"><xsl:value-of select="local-name(.)"/></xsl:attribute>
318 <!-- * if $autotoc.label.in.hyperlink is zero, then output the label -->
319 <!-- * before the hyperlinked title (as the DSSSL stylesheet does) -->
320 <xsl:if test="$autotoc.label.in.hyperlink = 0">
321 <xsl:variable name="label">
322 <xsl:apply-templates select="." mode="label.markup"/>
324 <xsl:copy-of select="$label"/>
325 <xsl:if test="$label != ''">
326 <xsl:value-of select="$autotoc.label.separator"/>
331 <xsl:attribute name="href">
332 <xsl:call-template name="href.target">
333 <xsl:with-param name="context" select="$toc-context"/>
334 <xsl:with-param name="toc-context" select="$toc-context"/>
338 <!-- * if $autotoc.label.in.hyperlink is non-zero, then output the label -->
339 <!-- * as part of the hyperlinked title -->
340 <xsl:if test="not($autotoc.label.in.hyperlink = 0)">
341 <xsl:variable name="label">
342 <xsl:apply-templates select="." mode="label.markup"/>
344 <xsl:copy-of select="$label"/>
345 <xsl:if test="$label != ''">
346 <xsl:value-of select="$autotoc.label.separator"/>
350 <xsl:apply-templates select="." mode="titleabbrev.markup"/>
355 <xsl:template match="book" mode="toc">
356 <xsl:param name="toc-context" select="."/>
358 <xsl:call-template name="subtoc">
359 <xsl:with-param name="toc-context" select="$toc-context"/>
360 <xsl:with-param name="nodes" select="part|reference
361 |preface|chapter|appendix
363 |bibliography|glossary|index
365 |bridgehead[$bridgehead.in.toc != 0]"/>
369 <xsl:template match="setindex" mode="toc">
370 <xsl:param name="toc-context" select="."/>
372 <!-- If the setindex tag is not empty, it should be it in the TOC -->
373 <xsl:if test="* or $generate.index != 0">
374 <xsl:call-template name="subtoc">
375 <xsl:with-param name="toc-context" select="$toc-context"/>
380 <xsl:template match="part|reference" mode="toc">
381 <xsl:param name="toc-context" select="."/>
383 <xsl:call-template name="subtoc">
384 <xsl:with-param name="toc-context" select="$toc-context"/>
385 <xsl:with-param name="nodes" select="appendix|chapter|article
386 |index|glossary|bibliography
387 |preface|reference|refentry
388 |bridgehead[$bridgehead.in.toc != 0]"/>
392 <xsl:template match="preface|chapter|appendix|article" mode="toc">
393 <xsl:param name="toc-context" select="."/>
395 <xsl:call-template name="subtoc">
396 <xsl:with-param name="toc-context" select="$toc-context"/>
397 <xsl:with-param name="nodes" select="section|sect1
398 |simplesect[$simplesect.in.toc != 0]
400 |glossary|bibliography|index
401 |bridgehead[$bridgehead.in.toc != 0]"/>
405 <xsl:template match="sect1" mode="toc">
406 <xsl:param name="toc-context" select="."/>
407 <xsl:call-template name="subtoc">
408 <xsl:with-param name="toc-context" select="$toc-context"/>
409 <xsl:with-param name="nodes" select="sect2
410 |bridgehead[$bridgehead.in.toc != 0]"/>
414 <xsl:template match="sect2" mode="toc">
415 <xsl:param name="toc-context" select="."/>
417 <xsl:call-template name="subtoc">
418 <xsl:with-param name="toc-context" select="$toc-context"/>
419 <xsl:with-param name="nodes" select="sect3
420 |bridgehead[$bridgehead.in.toc != 0]"/>
424 <xsl:template match="sect3" mode="toc">
425 <xsl:param name="toc-context" select="."/>
427 <xsl:call-template name="subtoc">
428 <xsl:with-param name="toc-context" select="$toc-context"/>
429 <xsl:with-param name="nodes" select="sect4
430 |bridgehead[$bridgehead.in.toc != 0]"/>
434 <xsl:template match="sect4" mode="toc">
435 <xsl:param name="toc-context" select="."/>
437 <xsl:call-template name="subtoc">
438 <xsl:with-param name="toc-context" select="$toc-context"/>
439 <xsl:with-param name="nodes" select="sect5
440 |bridgehead[$bridgehead.in.toc != 0]"/>
444 <xsl:template match="sect5" mode="toc">
445 <xsl:param name="toc-context" select="."/>
447 <xsl:call-template name="subtoc">
448 <xsl:with-param name="toc-context" select="$toc-context"/>
452 <xsl:template match="simplesect" mode="toc">
453 <xsl:param name="toc-context" select="."/>
455 <xsl:call-template name="subtoc">
456 <xsl:with-param name="toc-context" select="$toc-context"/>
460 <xsl:template match="section" mode="toc">
461 <xsl:param name="toc-context" select="."/>
463 <xsl:call-template name="subtoc">
464 <xsl:with-param name="toc-context" select="$toc-context"/>
465 <xsl:with-param name="nodes" select="section|refentry
466 |simplesect[$simplesect.in.toc != 0]
467 |bridgehead[$bridgehead.in.toc != 0]"/>
471 <xsl:template match="bridgehead" mode="toc">
472 <xsl:param name="toc-context" select="."/>
474 <xsl:if test="$bridgehead.in.toc != 0">
475 <xsl:call-template name="subtoc">
476 <xsl:with-param name="toc-context" select="$toc-context"/>
481 <xsl:template match="bibliography|glossary" mode="toc">
482 <xsl:param name="toc-context" select="."/>
484 <xsl:call-template name="subtoc">
485 <xsl:with-param name="toc-context" select="$toc-context"/>
489 <xsl:template match="index" mode="toc">
490 <xsl:param name="toc-context" select="."/>
492 <!-- If the index tag is not empty, it should be it in the TOC -->
493 <xsl:if test="* or $generate.index != 0">
494 <xsl:call-template name="subtoc">
495 <xsl:with-param name="toc-context" select="$toc-context"/>
500 <xsl:template match="refentry" mode="toc">
501 <xsl:param name="toc-context" select="."/>
503 <xsl:variable name="refmeta" select=".//refmeta"/>
504 <xsl:variable name="refentrytitle" select="$refmeta//refentrytitle"/>
505 <xsl:variable name="refnamediv" select=".//refnamediv"/>
506 <xsl:variable name="refname" select="$refnamediv//refname"/>
507 <xsl:variable name="refdesc" select="$refnamediv//refdescriptor"/>
508 <xsl:variable name="title">
510 <xsl:when test="$refentrytitle">
511 <xsl:apply-templates select="$refentrytitle[1]"
512 mode="titleabbrev.markup"/>
514 <xsl:when test="$refdesc">
515 <xsl:apply-templates select="$refdesc"
516 mode="titleabbrev.markup"/>
518 <xsl:when test="$refname">
519 <xsl:apply-templates select="$refname[1]"
520 mode="titleabbrev.markup"/>
525 <xsl:element name="{$toc.listitem.type}">
526 <span class='refentrytitle'>
528 <xsl:attribute name="href">
529 <xsl:call-template name="href.target">
530 <xsl:with-param name="toc-context" select="$toc-context"/>
533 <xsl:copy-of select="$title"/>
536 <span class='refpurpose'>
537 <xsl:if test="$annotate.toc != 0">
538 <!-- * DocBook 5 says inlinemediaobject (among other things) -->
539 <!-- * is allowed in refpurpose; so we need to run -->
540 <!-- * apply-templates on refpurpose here, instead of value-of -->
541 <xsl:apply-templates select="refnamediv/refpurpose"/>
547 <xsl:template match="title" mode="toc">
548 <xsl:param name="toc-context" select="."/>
551 <xsl:attribute name="href">
552 <xsl:call-template name="href.target">
553 <xsl:with-param name="object" select=".."/>
554 <xsl:with-param name="toc-context" select="$toc-context"/>
557 <xsl:apply-templates/>
561 <xsl:template name="manual-toc">
562 <xsl:param name="toc-context" select="."/>
563 <xsl:param name="tocentry"/>
565 <!-- be careful, we don't want to change the current document to the other tree! -->
567 <xsl:if test="$tocentry">
568 <xsl:variable name="node" select="key('id', $tocentry/@linkend)"/>
570 <xsl:element name="{$toc.listitem.type}">
571 <xsl:variable name="label">
572 <xsl:apply-templates select="$node" mode="label.markup"/>
574 <xsl:copy-of select="$label"/>
575 <xsl:if test="$label != ''">
576 <xsl:value-of select="$autotoc.label.separator"/>
579 <xsl:attribute name="href">
580 <xsl:call-template name="href.target">
581 <xsl:with-param name="object" select="$node"/>
582 <xsl:with-param name="toc-context" select="$toc-context"/>
585 <xsl:apply-templates select="$node" mode="titleabbrev.markup"/>
589 <xsl:if test="$tocentry/*">
590 <xsl:element name="{$toc.list.type}">
591 <xsl:call-template name="manual-toc">
592 <xsl:with-param name="tocentry" select="$tocentry/*[1]"/>
597 <xsl:if test="$tocentry/following-sibling::*">
598 <xsl:call-template name="manual-toc">
599 <xsl:with-param name="tocentry" select="$tocentry/following-sibling::*[1]"/>
605 <!-- ==================================================================== -->
607 <xsl:template name="list.of.titles">
608 <xsl:param name="toc-context" select="."/>
609 <xsl:param name="titles" select="'table'"/>
610 <xsl:param name="nodes" select=".//table"/>
612 <xsl:if test="$nodes">
613 <div class="list-of-{$titles}s">
616 <xsl:call-template name="gentext">
617 <xsl:with-param name="key">
619 <xsl:when test="$titles='table'">ListofTables</xsl:when>
620 <xsl:when test="$titles='figure'">ListofFigures</xsl:when>
621 <xsl:when test="$titles='equation'">ListofEquations</xsl:when>
622 <xsl:when test="$titles='example'">ListofExamples</xsl:when>
623 <xsl:when test="$titles='procedure'">ListofProcedures</xsl:when>
624 <xsl:otherwise>ListofUnknown</xsl:otherwise>
631 <xsl:element name="{$toc.list.type}">
632 <xsl:apply-templates select="$nodes" mode="toc">
633 <xsl:with-param name="toc-context" select="$toc-context"/>
634 </xsl:apply-templates>
640 <xsl:template match="figure|table|example|equation|procedure" mode="toc">
641 <xsl:param name="toc-context" select="."/>
643 <xsl:element name="{$toc.listitem.type}">
644 <xsl:variable name="label">
645 <xsl:apply-templates select="." mode="label.markup"/>
647 <xsl:copy-of select="$label"/>
648 <xsl:if test="$label != ''">
649 <xsl:value-of select="$autotoc.label.separator"/>
652 <xsl:attribute name="href">
653 <xsl:call-template name="href.target">
654 <xsl:with-param name="toc-context" select="$toc-context"/>
657 <xsl:apply-templates select="." mode="titleabbrev.markup"/>
662 <!-- Used only if qanda.in.toc parameter is non-zero -->
663 <xsl:template match="qandaset" mode="toc">
664 <xsl:param name="toc-context" select="."/>
665 <xsl:call-template name="subtoc">
666 <xsl:with-param name="toc-context" select="$toc-context"/>
667 <xsl:with-param name="nodes" select="qandadiv | qandaentry"/>
671 <xsl:template match="qandadiv|qandaentry" mode="toc">
672 <xsl:apply-templates select="." mode="qandatoc.mode"/>