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