Imported Upstream version 1.77.1
[platform/upstream/docbook-xsl.git] / html / chunktoc.xsl
1 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
2                 xmlns:doc="http://nwalsh.com/xsl/documentation/1.0"
3                 version="1.0"
4                 exclude-result-prefixes="doc">
5
6 <!-- ********************************************************************
7      $Id: chunktoc.xsl 9286 2012-04-19 10:10:58Z bobstayton $
8      ********************************************************************
9
10      This file is part of the XSL DocBook Stylesheet distribution.
11      See ../README or http://docbook.sf.net/release/xsl/current/ for
12      copyright and other information.
13
14      ******************************************************************** -->
15
16 <!-- ==================================================================== -->
17
18 <xsl:import href="docbook.xsl"/>
19 <xsl:import href="chunk-common.xsl"/>
20
21 <xsl:template name="chunk">
22   <xsl:param name="node" select="."/>
23   <!-- returns 1 if $node is a chunk -->
24
25   <xsl:variable name="id">
26     <xsl:call-template name="object.id">
27       <xsl:with-param name="object" select="$node"/>
28     </xsl:call-template>
29   </xsl:variable>
30
31   <xsl:variable name="chunks" select="document($chunk.toc,/)"/>
32
33   <xsl:choose>
34     <xsl:when test="$chunks//tocentry[@linkend=$id]">1</xsl:when>
35     <xsl:otherwise>0</xsl:otherwise>
36   </xsl:choose>
37 </xsl:template>
38
39 <!-- ==================================================================== -->
40
41 <xsl:template match="*" mode="chunk-filename">
42   <!-- returns the filename of a chunk -->
43
44   <xsl:variable name="id">
45     <xsl:call-template name="object.id"/>
46   </xsl:variable>
47
48   <xsl:variable name="chunks" select="document($chunk.toc,/)"/>
49
50   <xsl:variable name="chunk" select="$chunks//tocentry[@linkend=$id]"/>
51   <xsl:variable name="filename">
52     <xsl:call-template name="pi.dbhtml_filename">
53       <xsl:with-param name="node" select="$chunk"/>
54     </xsl:call-template>
55   </xsl:variable>
56
57   <xsl:choose>
58     <xsl:when test="$chunk">
59       <xsl:value-of select="$filename"/>
60     </xsl:when>
61     <xsl:otherwise>
62       <xsl:apply-templates select="parent::*" mode="chunk-filename"/>
63     </xsl:otherwise>
64   </xsl:choose>
65 </xsl:template>
66
67 <!-- ==================================================================== -->
68
69 <xsl:template name="process-chunk">
70   <xsl:variable name="id">
71     <xsl:call-template name="object.id"/>
72   </xsl:variable>
73
74   <xsl:variable name="chunks" select="document($chunk.toc,/)"/>
75
76   <xsl:variable name="chunk" select="$chunks//tocentry[@linkend=$id]"/>
77   <xsl:variable name="prev-id"
78                 select="($chunk/preceding::tocentry
79                          |$chunk/ancestor::tocentry)[last()]/@linkend"/>
80   <xsl:variable name="next-id"
81                 select="($chunk/following::tocentry
82                          |$chunk/child::tocentry)[1]/@linkend"/>
83
84   <xsl:variable name="prev" select="key('id',$prev-id)"/>
85   <xsl:variable name="next" select="key('id',$next-id)"/>
86
87   <xsl:variable name="ischunk">
88     <xsl:call-template name="chunk"/>
89   </xsl:variable>
90
91   <xsl:variable name="chunkfn">
92     <xsl:if test="$ischunk='1'">
93       <xsl:apply-templates mode="chunk-filename" select="."/>
94     </xsl:if>
95   </xsl:variable>
96
97   <xsl:variable name="filename">
98     <xsl:call-template name="make-relative-filename">
99       <xsl:with-param name="base.dir" select="$chunk.base.dir"/>
100       <xsl:with-param name="base.name" select="$chunkfn"/>
101     </xsl:call-template>
102   </xsl:variable>
103
104   <xsl:choose>
105     <xsl:when test="$ischunk = 0">
106       <xsl:apply-imports/>
107     </xsl:when>
108
109     <xsl:otherwise>
110       <xsl:call-template name="write.chunk">
111         <xsl:with-param name="filename" select="$filename"/>
112         <xsl:with-param name="content">
113           <xsl:call-template name="chunk-element-content">
114             <xsl:with-param name="prev" select="$prev"/>
115             <xsl:with-param name="next" select="$next"/>
116           </xsl:call-template>
117         </xsl:with-param>
118         <xsl:with-param name="quiet" select="$chunk.quietly"/>
119       </xsl:call-template>
120     </xsl:otherwise>
121   </xsl:choose>
122 </xsl:template>
123
124 <!-- ==================================================================== -->
125
126 <xsl:template match="set">
127   <xsl:call-template name="process-chunk"/>
128 </xsl:template>
129
130 <xsl:template match="book">
131   <xsl:call-template name="process-chunk"/>
132 </xsl:template>
133
134 <xsl:template match="book/appendix">
135   <xsl:call-template name="process-chunk"/>
136 </xsl:template>
137
138 <xsl:template match="book/glossary">
139   <xsl:call-template name="process-chunk"/>
140 </xsl:template>
141
142 <xsl:template match="book/bibliography">
143   <xsl:call-template name="process-chunk"/>
144 </xsl:template>
145
146 <xsl:template match="dedication" mode="dedication">
147   <xsl:call-template name="process-chunk"/>
148 </xsl:template>
149
150 <xsl:template match="preface|chapter">
151   <xsl:call-template name="process-chunk"/>
152 </xsl:template>
153
154 <xsl:template match="part|reference">
155   <xsl:call-template name="process-chunk"/>
156 </xsl:template>
157
158 <xsl:template match="refentry">
159   <xsl:call-template name="process-chunk"/>
160 </xsl:template>
161
162 <xsl:template match="colophon">
163   <xsl:call-template name="process-chunk"/>
164 </xsl:template>
165
166 <xsl:template match="article">
167   <xsl:call-template name="process-chunk"/>
168 </xsl:template>
169
170 <xsl:template match="topic">
171   <xsl:call-template name="process-chunk"/>
172 </xsl:template>
173
174 <xsl:template match="article/appendix">
175   <xsl:call-template name="process-chunk"/>
176 </xsl:template>
177
178 <xsl:template match="article/glossary">
179   <xsl:call-template name="process-chunk"/>
180 </xsl:template>
181
182 <xsl:template match="article/bibliography">
183   <xsl:call-template name="process-chunk"/>
184 </xsl:template>
185
186 <xsl:template match="sect1|sect2|sect3|sect4|sect5|section">
187   <xsl:variable name="ischunk">
188     <xsl:call-template name="chunk"/>
189   </xsl:variable>
190
191   <xsl:choose>
192     <xsl:when test="$ischunk != 0">
193       <xsl:call-template name="process-chunk"/>
194     </xsl:when>
195     <xsl:otherwise>
196       <xsl:apply-imports/>
197     </xsl:otherwise>
198   </xsl:choose>
199 </xsl:template>
200
201 <xsl:template match="setindex
202                      |book/index
203                      |article/index">
204   <!-- some implementations use completely empty index tags to indicate -->
205   <!-- where an automatically generated index should be inserted. so -->
206   <!-- if the index is completely empty, skip it. -->
207   <xsl:if test="count(*)>0 or $generate.index != '0'">
208     <xsl:call-template name="process-chunk"/>
209   </xsl:if>
210 </xsl:template>
211
212 <!-- ==================================================================== -->
213
214 <xsl:template match="/">
215   <!-- * Get a title for current doc so that we let the user -->
216   <!-- * know what document we are processing at this point. -->
217   <xsl:variable name="doc.title">
218     <xsl:call-template name="get.doc.title"/>
219   </xsl:variable>
220   <xsl:choose>
221     <xsl:when test="$chunk.toc = ''">
222       <xsl:message terminate="yes">
223         <xsl:text>The chunk.toc file is not set.</xsl:text>
224       </xsl:message>
225     </xsl:when>
226     <!-- Hack! If someone hands us a DocBook V5.x or DocBook NG document,
227          toss the namespace and continue.  Use the docbook5 namespaced
228          stylesheets for DocBook5 if you don't want to use this feature.-->
229     <!-- include extra test for Xalan quirk -->
230     <xsl:when test="$exsl.node.set.available != 0
231                     and (*/self::ng:* or */self::db:*)">
232       <xsl:call-template name="log.message">
233         <xsl:with-param name="level">Note</xsl:with-param>
234         <xsl:with-param name="source" select="$doc.title"/>
235         <xsl:with-param name="context-desc">
236           <xsl:text>namesp. cut</xsl:text>
237         </xsl:with-param>
238         <xsl:with-param name="message">
239           <xsl:text>stripped namespace before processing</xsl:text>
240         </xsl:with-param>
241       </xsl:call-template>
242       <xsl:variable name="nons">
243         <xsl:apply-templates mode="stripNS"/>
244       </xsl:variable>
245       <xsl:call-template name="log.message">
246         <xsl:with-param name="level">Note</xsl:with-param>
247         <xsl:with-param name="source" select="$doc.title"/>
248         <xsl:with-param name="context-desc">
249           <xsl:text>namesp. cut</xsl:text>
250         </xsl:with-param>
251         <xsl:with-param name="message">
252           <xsl:text>processing stripped document</xsl:text>
253         </xsl:with-param>
254       </xsl:call-template>
255       <xsl:apply-templates select="exsl:node-set($nons)"/>
256     </xsl:when>
257     <!-- Can't process unless namespace removed -->
258     <xsl:when test="*/self::ng:* or */self::db:*">
259       <xsl:message terminate="yes">
260         <xsl:text>Unable to strip the namespace from DB5 document,</xsl:text>
261         <xsl:text> cannot proceed.</xsl:text>
262       </xsl:message>
263     </xsl:when>
264     <xsl:otherwise>
265       <xsl:choose>
266         <xsl:when test="$rootid != ''">
267           <xsl:choose>
268             <xsl:when test="count(key('id',$rootid)) = 0">
269               <xsl:message terminate="yes">
270                 <xsl:text>ID '</xsl:text>
271                 <xsl:value-of select="$rootid"/>
272                 <xsl:text>' not found in document.</xsl:text>
273               </xsl:message>
274             </xsl:when>
275             <xsl:otherwise>
276               <xsl:if test="$collect.xref.targets = 'yes' or
277                             $collect.xref.targets = 'only'">
278                 <xsl:apply-templates select="key('id', $rootid)"
279                                      mode="collect.targets"/>
280               </xsl:if>
281               <xsl:if test="$collect.xref.targets != 'only'">
282                 <xsl:apply-templates select="key('id',$rootid)"
283                                      mode="process.root"/>
284                 <xsl:if test="$tex.math.in.alt != ''">
285                   <xsl:apply-templates select="key('id',$rootid)"
286                                        mode="collect.tex.math"/>
287                 </xsl:if>
288                 <xsl:if test="$generate.manifest != 0">
289                   <xsl:call-template name="generate.manifest">
290                     <xsl:with-param name="node" select="key('id',$rootid)"/>
291                   </xsl:call-template>
292                 </xsl:if>
293               </xsl:if>
294             </xsl:otherwise>
295           </xsl:choose>
296         </xsl:when>
297         <xsl:otherwise>
298           <xsl:if test="$collect.xref.targets = 'yes' or
299                         $collect.xref.targets = 'only'">
300             <xsl:apply-templates select="/" mode="collect.targets"/>
301           </xsl:if>
302           <xsl:if test="$collect.xref.targets != 'only'">
303             <xsl:apply-templates select="/" mode="process.root"/>
304             <xsl:if test="$tex.math.in.alt != ''">
305               <xsl:apply-templates select="/" mode="collect.tex.math"/>
306             </xsl:if>
307             <xsl:if test="$generate.manifest != 0">
308               <xsl:call-template name="generate.manifest">
309                 <xsl:with-param name="node" select="/"/>
310               </xsl:call-template>
311             </xsl:if>
312           </xsl:if>
313         </xsl:otherwise>
314       </xsl:choose>
315     </xsl:otherwise>
316   </xsl:choose>
317 </xsl:template>
318
319 <xsl:template match="*" mode="process.root">
320   <xsl:apply-templates select="."/>
321   <xsl:call-template name="generate.css"/>
322 </xsl:template>
323
324 <xsl:template name="make.lots">
325   <xsl:param name="toc.params" select="''"/>
326   <xsl:param name="toc"/>
327
328   <xsl:variable name="lots">
329     <xsl:if test="contains($toc.params, 'toc')">
330       <xsl:copy-of select="$toc"/>
331     </xsl:if>
332
333     <xsl:if test="contains($toc.params, 'figure')">
334       <xsl:choose>
335         <xsl:when test="$chunk.separate.lots != '0'">
336           <xsl:call-template name="make.lot.chunk">
337             <xsl:with-param name="type" select="'figure'"/>
338             <xsl:with-param name="lot">
339               <xsl:call-template name="list.of.titles">
340                 <xsl:with-param name="titles" select="'figure'"/>
341                 <xsl:with-param name="nodes" select=".//figure"/>
342               </xsl:call-template>
343             </xsl:with-param>
344           </xsl:call-template>
345         </xsl:when>
346         <xsl:otherwise>
347           <xsl:call-template name="list.of.titles">
348             <xsl:with-param name="titles" select="'figure'"/>
349             <xsl:with-param name="nodes" select=".//figure"/>
350           </xsl:call-template>
351         </xsl:otherwise>
352       </xsl:choose>
353     </xsl:if>
354
355     <xsl:if test="contains($toc.params, 'table')">
356       <xsl:choose>
357         <xsl:when test="$chunk.separate.lots != '0'">
358           <xsl:call-template name="make.lot.chunk">
359             <xsl:with-param name="type" select="'table'"/>
360             <xsl:with-param name="lot">
361               <xsl:call-template name="list.of.titles">
362                 <xsl:with-param name="titles" select="'table'"/>
363                 <xsl:with-param name="nodes" select=".//table"/>
364               </xsl:call-template>
365             </xsl:with-param>
366           </xsl:call-template>
367         </xsl:when>
368         <xsl:otherwise>
369           <xsl:call-template name="list.of.titles">
370             <xsl:with-param name="titles" select="'table'"/>
371             <xsl:with-param name="nodes" select=".//table"/>
372           </xsl:call-template>
373         </xsl:otherwise>
374       </xsl:choose>
375     </xsl:if>
376
377     <xsl:if test="contains($toc.params, 'example')">
378       <xsl:choose>
379         <xsl:when test="$chunk.separate.lots != '0'">
380           <xsl:call-template name="make.lot.chunk">
381             <xsl:with-param name="type" select="'example'"/>
382             <xsl:with-param name="lot">
383               <xsl:call-template name="list.of.titles">
384                 <xsl:with-param name="titles" select="'example'"/>
385                 <xsl:with-param name="nodes" select=".//example"/>
386               </xsl:call-template>
387             </xsl:with-param>
388           </xsl:call-template>
389         </xsl:when>
390         <xsl:otherwise>
391           <xsl:call-template name="list.of.titles">
392             <xsl:with-param name="titles" select="'example'"/>
393             <xsl:with-param name="nodes" select=".//example"/>
394           </xsl:call-template>
395         </xsl:otherwise>
396       </xsl:choose>
397     </xsl:if>
398
399     <xsl:if test="contains($toc.params, 'equation')">
400       <xsl:choose>
401         <xsl:when test="$chunk.separate.lots != '0'">
402           <xsl:call-template name="make.lot.chunk">
403             <xsl:with-param name="type" select="'equation'"/>
404             <xsl:with-param name="lot">
405               <xsl:call-template name="list.of.titles">
406                 <xsl:with-param name="titles" select="'equation'"/>
407                 <xsl:with-param name="nodes" select=".//equation"/>
408               </xsl:call-template>
409             </xsl:with-param>
410           </xsl:call-template>
411         </xsl:when>
412         <xsl:otherwise>
413           <xsl:call-template name="list.of.titles">
414             <xsl:with-param name="titles" select="'equation'"/>
415             <xsl:with-param name="nodes" select=".//equation"/>
416           </xsl:call-template>
417         </xsl:otherwise>
418       </xsl:choose>
419     </xsl:if>
420
421     <xsl:if test="contains($toc.params, 'procedure')">
422       <xsl:choose>
423         <xsl:when test="$chunk.separate.lots != '0'">
424           <xsl:call-template name="make.lot.chunk">
425             <xsl:with-param name="type" select="'procedure'"/>
426             <xsl:with-param name="lot">
427               <xsl:call-template name="list.of.titles">
428                 <xsl:with-param name="titles" select="'procedure'"/>
429                 <xsl:with-param name="nodes" select=".//procedure[title]"/>
430               </xsl:call-template>
431             </xsl:with-param>
432           </xsl:call-template>
433         </xsl:when>
434         <xsl:otherwise>
435           <xsl:call-template name="list.of.titles">
436             <xsl:with-param name="titles" select="'procedure'"/>
437             <xsl:with-param name="nodes" select=".//procedure[title]"/>
438           </xsl:call-template>
439         </xsl:otherwise>
440       </xsl:choose>
441     </xsl:if>
442   </xsl:variable>
443
444   <xsl:if test="string($lots) != ''">
445     <xsl:choose>
446       <xsl:when test="$chunk.tocs.and.lots != 0 and not(parent::*)">
447         <xsl:call-template name="write.chunk">
448           <xsl:with-param name="filename">
449             <xsl:call-template name="make-relative-filename">
450               <xsl:with-param name="base.dir" select="$chunk.base.dir"/>
451               <xsl:with-param name="base.name">
452                 <xsl:call-template name="dbhtml-dir"/>
453                 <xsl:apply-templates select="." mode="recursive-chunk-filename">
454                   <xsl:with-param name="recursive" select="true()"/>
455                 </xsl:apply-templates>
456                 <xsl:text>-toc</xsl:text>
457                 <xsl:value-of select="$html.ext"/>
458               </xsl:with-param>
459             </xsl:call-template>
460           </xsl:with-param>
461           <xsl:with-param name="content">
462             <xsl:call-template name="chunk-element-content">
463               <xsl:with-param name="prev" select="/foo"/>
464               <xsl:with-param name="next" select="/foo"/>
465               <xsl:with-param name="nav.context" select="'toc'"/>
466               <xsl:with-param name="content">
467                 <h1>
468                   <xsl:apply-templates select="." mode="object.title.markup"/>
469                 </h1>
470                 <xsl:copy-of select="$lots"/>
471               </xsl:with-param>
472             </xsl:call-template>
473           </xsl:with-param>
474           <xsl:with-param name="quiet" select="$chunk.quietly"/>
475         </xsl:call-template>
476       </xsl:when>
477       <xsl:otherwise>
478         <xsl:copy-of select="$lots"/>
479       </xsl:otherwise>
480     </xsl:choose>
481   </xsl:if>
482 </xsl:template>
483
484 <xsl:template name="make.lot.chunk">
485   <xsl:param name="type" select="''"/>
486   <xsl:param name="lot"/>
487
488   <xsl:if test="string($lot) != ''">
489     <xsl:variable name="filename">
490       <xsl:call-template name="make-relative-filename">
491         <xsl:with-param name="base.dir" select="$chunk.base.dir"/>
492         <xsl:with-param name="base.name">
493           <xsl:call-template name="dbhtml-dir"/>
494           <xsl:value-of select="$type"/>
495           <xsl:text>-toc</xsl:text>
496           <xsl:value-of select="$html.ext"/>
497         </xsl:with-param>
498       </xsl:call-template>
499     </xsl:variable>
500
501     <xsl:variable name="href">
502       <xsl:call-template name="make-relative-filename">
503         <xsl:with-param name="base.dir" select="''"/>
504         <xsl:with-param name="base.name">
505           <xsl:call-template name="dbhtml-dir"/>
506           <xsl:value-of select="$type"/>
507           <xsl:text>-toc</xsl:text>
508           <xsl:value-of select="$html.ext"/>
509         </xsl:with-param>
510       </xsl:call-template>
511     </xsl:variable>
512
513     <xsl:call-template name="write.chunk">
514       <xsl:with-param name="filename" select="$filename"/>
515       <xsl:with-param name="content">
516         <xsl:call-template name="chunk-element-content">
517           <xsl:with-param name="prev" select="/foo"/>
518           <xsl:with-param name="next" select="/foo"/>
519           <xsl:with-param name="nav.context" select="'toc'"/>
520           <xsl:with-param name="content">
521             <xsl:copy-of select="$lot"/>
522           </xsl:with-param>
523         </xsl:call-template>
524       </xsl:with-param>
525       <xsl:with-param name="quiet" select="$chunk.quietly"/>
526     </xsl:call-template>
527     <!-- And output a link to this file -->
528     <div>
529       <xsl:attribute name="class">
530         <xsl:text>ListofTitles</xsl:text>
531       </xsl:attribute>
532       <a href="{$href}">
533         <xsl:call-template name="gentext">
534           <xsl:with-param name="key">
535             <xsl:choose>
536               <xsl:when test="$type='table'">ListofTables</xsl:when>
537               <xsl:when test="$type='figure'">ListofFigures</xsl:when>
538               <xsl:when test="$type='equation'">ListofEquations</xsl:when>
539               <xsl:when test="$type='example'">ListofExamples</xsl:when>
540               <xsl:when test="$type='procedure'">ListofProcedures</xsl:when>
541               <xsl:otherwise>ListofUnknown</xsl:otherwise>
542             </xsl:choose>
543           </xsl:with-param>
544         </xsl:call-template>
545       </a>
546     </div>
547   </xsl:if>
548 </xsl:template>
549
550 </xsl:stylesheet>