1<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
2                xmlns:exsl="http://exslt.org/common"
3                xmlns:cf="http://docbook.sourceforge.net/xmlns/chunkfast/1.0"
4		xmlns:ng="http://docbook.org/docbook-ng"
5		xmlns:db="http://docbook.org/ns/docbook"
6                version="1.0"
7                exclude-result-prefixes="exsl cf ng db">
8
9<!-- ********************************************************************
10     $Id: chunk-code.xsl,v 1.15 2006/05/04 12:46:45 kosek Exp $
11     ********************************************************************
12
13     This file is part of the XSL DocBook Stylesheet distribution.
14     See ../README or http://nwalsh.com/docbook/xsl/ for copyright
15     and other information.
16
17     ******************************************************************** -->
18
19<!-- ==================================================================== -->
20
21<xsl:param name="onechunk" select="0"/>
22<xsl:param name="refentry.separator" select="0"/>
23<xsl:param name="chunk.fast" select="0"/>
24
25<xsl:key name="genid" match="*" use="generate-id()"/>
26
27<!-- ==================================================================== -->
28
29<xsl:variable name="chunk.hierarchy">
30  <xsl:if test="$chunk.fast != 0">
31    <xsl:choose>
32      <xsl:when test="function-available('exsl:node-set')">
33        <xsl:message>Computing chunks...</xsl:message>
34        <xsl:apply-templates select="/*" mode="find.chunks"/>
35      </xsl:when>
36      <xsl:otherwise>
37        <xsl:message>
38          <xsl:text>Fast chunking requires exsl:node-set(). </xsl:text>
39          <xsl:text>Using "slow" chunking.</xsl:text>
40        </xsl:message>
41      </xsl:otherwise>
42    </xsl:choose>
43  </xsl:if>
44</xsl:variable>
45
46<xsl:template match="*" mode="find.chunks">
47  <xsl:variable name="chunk">
48    <xsl:call-template name="chunk"/>
49  </xsl:variable>
50
51  <xsl:choose>
52    <xsl:when test="$chunk != 0">
53      <cf:div class="{local-name(.)}" id="{generate-id()}">
54        <xsl:apply-templates select="*" mode="find.chunks"/>
55      </cf:div>
56    </xsl:when>
57    <xsl:otherwise>
58      <xsl:apply-templates select="*" mode="find.chunks"/>
59    </xsl:otherwise>
60  </xsl:choose>
61</xsl:template>
62
63<!-- ==================================================================== -->
64
65<xsl:template name="process-chunk-element">
66  <xsl:param name="content">
67    <xsl:apply-imports/>
68  </xsl:param>
69
70  <xsl:choose>
71    <xsl:when test="$chunk.fast != 0 and function-available('exsl:node-set')">
72      <xsl:variable name="chunks" select="exsl:node-set($chunk.hierarchy)//cf:div"/>
73      <xsl:variable name="genid" select="generate-id()"/>
74
75      <xsl:variable name="div" select="$chunks[@id=$genid]"/>
76
77      <xsl:variable name="prevdiv"
78                    select="($div/preceding-sibling::cf:div|$div/preceding::cf:div|$div/parent::cf:div)[last()]"/>
79      <xsl:variable name="prev" select="key('genid', $prevdiv/@id)"/>
80
81      <xsl:variable name="nextdiv"
82                    select="($div/following-sibling::cf:div|$div/following::cf:div|$div/cf:div)[1]"/>
83      <xsl:variable name="next" select="key('genid', $nextdiv/@id)"/>
84
85      <xsl:choose>
86        <xsl:when test="$onechunk != 0 and parent::*">
87          <xsl:copy-of select="$content"/>
88        </xsl:when>
89        <xsl:otherwise>
90          <xsl:call-template name="process-chunk">
91            <xsl:with-param name="prev" select="$prev"/>
92            <xsl:with-param name="next" select="$next"/>
93            <xsl:with-param name="content" select="$content"/>
94          </xsl:call-template>
95        </xsl:otherwise>
96      </xsl:choose>
97    </xsl:when>
98    <xsl:otherwise>
99      <xsl:choose>
100        <xsl:when test="$onechunk != 0 and not(parent::*)">
101          <xsl:call-template name="chunk-all-sections">
102            <xsl:with-param name="content" select="$content"/>
103          </xsl:call-template>
104        </xsl:when>
105        <xsl:when test="$onechunk != 0">
106          <xsl:copy-of select="$content"/>
107        </xsl:when>
108        <xsl:when test="$chunk.first.sections = 0">
109          <xsl:call-template name="chunk-first-section-with-parent">
110            <xsl:with-param name="content" select="$content"/>
111          </xsl:call-template>
112        </xsl:when>
113        <xsl:otherwise>
114          <xsl:call-template name="chunk-all-sections">
115            <xsl:with-param name="content" select="$content"/>
116          </xsl:call-template>
117        </xsl:otherwise>
118      </xsl:choose>
119    </xsl:otherwise>
120  </xsl:choose>
121</xsl:template>
122
123<xsl:template name="process-chunk">
124  <xsl:param name="prev" select="."/>
125  <xsl:param name="next" select="."/>
126  <xsl:param name="content">
127    <xsl:apply-imports/>
128  </xsl:param>
129
130  <xsl:variable name="ischunk">
131    <xsl:call-template name="chunk"/>
132  </xsl:variable>
133
134  <xsl:variable name="chunkfn">
135    <xsl:if test="$ischunk='1'">
136      <xsl:apply-templates mode="chunk-filename" select="."/>
137    </xsl:if>
138  </xsl:variable>
139
140  <xsl:if test="$ischunk='0'">
141    <xsl:message>
142      <xsl:text>Error </xsl:text>
143      <xsl:value-of select="name(.)"/>
144      <xsl:text> is not a chunk!</xsl:text>
145    </xsl:message>
146  </xsl:if>
147
148  <xsl:variable name="filename">
149    <xsl:call-template name="make-relative-filename">
150      <xsl:with-param name="base.dir" select="$base.dir"/>
151      <xsl:with-param name="base.name" select="$chunkfn"/>
152    </xsl:call-template>
153  </xsl:variable>
154
155  <xsl:call-template name="write.chunk">
156    <xsl:with-param name="filename" select="$filename"/>
157    <xsl:with-param name="content">
158      <xsl:call-template name="chunk-element-content">
159        <xsl:with-param name="prev" select="$prev"/>
160        <xsl:with-param name="next" select="$next"/>
161        <xsl:with-param name="content" select="$content"/>
162      </xsl:call-template>
163    </xsl:with-param>
164    <xsl:with-param name="quiet" select="$chunk.quietly"/>
165  </xsl:call-template>
166</xsl:template>
167
168<xsl:template name="chunk-first-section-with-parent">
169  <xsl:param name="content">
170    <xsl:apply-imports/>
171  </xsl:param>
172
173  <!-- These xpath expressions are really hairy. The trick is to pick sections -->
174  <!-- that are not first children and are not the children of first children -->
175
176  <!-- Break these variables into pieces to work around
177       http://nagoya.apache.org/bugzilla/show_bug.cgi?id=6063 -->
178
179  <xsl:variable name="prev-v1"
180     select="(ancestor::sect1[$chunk.section.depth &gt; 0
181                               and preceding-sibling::sect1][1]
182
183             |ancestor::sect2[$chunk.section.depth &gt; 1
184                               and preceding-sibling::sect2
185                               and parent::sect1[preceding-sibling::sect1]][1]
186
187             |ancestor::sect3[$chunk.section.depth &gt; 2
188                               and preceding-sibling::sect3
189                               and parent::sect2[preceding-sibling::sect2]
190                               and ancestor::sect1[preceding-sibling::sect1]][1]
191
192             |ancestor::sect4[$chunk.section.depth &gt; 3
193                               and preceding-sibling::sect4
194                               and parent::sect3[preceding-sibling::sect2]
195                               and ancestor::sect2[preceding-sibling::sect2]
196                               and ancestor::sect1[preceding-sibling::sect1]][1]
197
198             |ancestor::sect5[$chunk.section.depth &gt; 4
199                               and preceding-sibling::sect5
200                               and parent::sect4[preceding-sibling::sect4]
201                               and ancestor::sect3[preceding-sibling::sect3]
202                               and ancestor::sect2[preceding-sibling::sect2]
203                               and ancestor::sect1[preceding-sibling::sect1]][1]
204
205             |ancestor::section[$chunk.section.depth &gt; count(ancestor::section)
206                                and not(ancestor::section[not(preceding-sibling::section)])][1])[last()]"/>
207
208  <xsl:variable name="prev-v2"
209     select="(preceding::sect1[$chunk.section.depth &gt; 0
210                               and preceding-sibling::sect1][1]
211
212             |preceding::sect2[$chunk.section.depth &gt; 1
213                               and preceding-sibling::sect2
214                               and parent::sect1[preceding-sibling::sect1]][1]
215
216             |preceding::sect3[$chunk.section.depth &gt; 2
217                               and preceding-sibling::sect3
218                               and parent::sect2[preceding-sibling::sect2]
219                               and ancestor::sect1[preceding-sibling::sect1]][1]
220
221             |preceding::sect4[$chunk.section.depth &gt; 3
222                               and preceding-sibling::sect4
223                               and parent::sect3[preceding-sibling::sect2]
224                               and ancestor::sect2[preceding-sibling::sect2]
225                               and ancestor::sect1[preceding-sibling::sect1]][1]
226
227             |preceding::sect5[$chunk.section.depth &gt; 4
228                               and preceding-sibling::sect5
229                               and parent::sect4[preceding-sibling::sect4]
230                               and ancestor::sect3[preceding-sibling::sect3]
231                               and ancestor::sect2[preceding-sibling::sect2]
232                               and ancestor::sect1[preceding-sibling::sect1]][1]
233
234             |preceding::section[$chunk.section.depth &gt; count(ancestor::section)
235                                 and preceding-sibling::section
236                                 and not(ancestor::section[not(preceding-sibling::section)])][1])[last()]"/>
237
238  <xsl:variable name="prev"
239    select="(preceding::book[1]
240             |preceding::preface[1]
241             |preceding::chapter[1]
242             |preceding::appendix[1]
243             |preceding::part[1]
244             |preceding::reference[1]
245             |preceding::refentry[1]
246             |preceding::colophon[1]
247             |preceding::article[1]
248             |preceding::bibliography[parent::article or parent::book or parent::part][1]
249             |preceding::glossary[parent::article or parent::book or parent::part][1]
250             |preceding::index[$generate.index != 0]
251	                       [parent::article or parent::book or parent::part][1]
252             |preceding::setindex[$generate.index != 0][1]
253             |ancestor::set
254             |ancestor::book[1]
255             |ancestor::preface[1]
256             |ancestor::chapter[1]
257             |ancestor::appendix[1]
258             |ancestor::part[1]
259             |ancestor::reference[1]
260             |ancestor::article[1]
261             |$prev-v1
262             |$prev-v2)[last()]"/>
263
264  <xsl:variable name="next-v1"
265    select="(following::sect1[$chunk.section.depth &gt; 0
266                               and preceding-sibling::sect1][1]
267
268             |following::sect2[$chunk.section.depth &gt; 1
269                               and preceding-sibling::sect2
270                               and parent::sect1[preceding-sibling::sect1]][1]
271
272             |following::sect3[$chunk.section.depth &gt; 2
273                               and preceding-sibling::sect3
274                               and parent::sect2[preceding-sibling::sect2]
275                               and ancestor::sect1[preceding-sibling::sect1]][1]
276
277             |following::sect4[$chunk.section.depth &gt; 3
278                               and preceding-sibling::sect4
279                               and parent::sect3[preceding-sibling::sect2]
280                               and ancestor::sect2[preceding-sibling::sect2]
281                               and ancestor::sect1[preceding-sibling::sect1]][1]
282
283             |following::sect5[$chunk.section.depth &gt; 4
284                               and preceding-sibling::sect5
285                               and parent::sect4[preceding-sibling::sect4]
286                               and ancestor::sect3[preceding-sibling::sect3]
287                               and ancestor::sect2[preceding-sibling::sect2]
288                               and ancestor::sect1[preceding-sibling::sect1]][1]
289
290             |following::section[$chunk.section.depth &gt; count(ancestor::section)
291                                 and preceding-sibling::section
292                                 and not(ancestor::section[not(preceding-sibling::section)])][1])[1]"/>
293
294  <xsl:variable name="next-v2"
295    select="(descendant::sect1[$chunk.section.depth &gt; 0
296                               and preceding-sibling::sect1][1]
297
298             |descendant::sect2[$chunk.section.depth &gt; 1
299                               and preceding-sibling::sect2
300                               and parent::sect1[preceding-sibling::sect1]][1]
301
302             |descendant::sect3[$chunk.section.depth &gt; 2
303                               and preceding-sibling::sect3
304                               and parent::sect2[preceding-sibling::sect2]
305                               and ancestor::sect1[preceding-sibling::sect1]][1]
306
307             |descendant::sect4[$chunk.section.depth &gt; 3
308                               and preceding-sibling::sect4
309                               and parent::sect3[preceding-sibling::sect2]
310                               and ancestor::sect2[preceding-sibling::sect2]
311                               and ancestor::sect1[preceding-sibling::sect1]][1]
312
313             |descendant::sect5[$chunk.section.depth &gt; 4
314                               and preceding-sibling::sect5
315                               and parent::sect4[preceding-sibling::sect4]
316                               and ancestor::sect3[preceding-sibling::sect3]
317                               and ancestor::sect2[preceding-sibling::sect2]
318                               and ancestor::sect1[preceding-sibling::sect1]][1]
319
320             |descendant::section[$chunk.section.depth &gt; count(ancestor::section)
321                                 and preceding-sibling::section
322                                 and not(ancestor::section[not(preceding-sibling::section)])])[1]"/>
323
324  <xsl:variable name="next"
325    select="(following::book[1]
326             |following::preface[1]
327             |following::chapter[1]
328             |following::appendix[1]
329             |following::part[1]
330             |following::reference[1]
331             |following::refentry[1]
332             |following::colophon[1]
333             |following::bibliography[parent::article or parent::book or parent::part][1]
334             |following::glossary[parent::article or parent::book or parent::part][1]
335             |following::index[$generate.index != 0]
336	                       [parent::article or parent::book or parent::part][1]
337             |following::article[1]
338             |following::setindex[$generate.index != 0][1]
339             |descendant::book[1]
340             |descendant::preface[1]
341             |descendant::chapter[1]
342             |descendant::appendix[1]
343             |descendant::article[1]
344             |descendant::bibliography[parent::article or parent::book or parent::part][1]
345             |descendant::glossary[parent::article or parent::book or parent::part][1]
346             |descendant::index[$generate.index != 0]
347	                       [parent::article or parent::book or parent::part][1]
348             |descendant::colophon[1]
349             |descendant::setindex[$generate.index != 0][1]
350             |descendant::part[1]
351             |descendant::reference[1]
352             |descendant::refentry[1]
353             |$next-v1
354             |$next-v2)[1]"/>
355
356  <xsl:call-template name="process-chunk">
357    <xsl:with-param name="prev" select="$prev"/>
358    <xsl:with-param name="next" select="$next"/>
359    <xsl:with-param name="content" select="$content"/>
360  </xsl:call-template>
361</xsl:template>
362
363<xsl:template name="chunk-all-sections">
364  <xsl:param name="content">
365    <xsl:apply-imports/>
366  </xsl:param>
367
368  <xsl:variable name="prev-v1"
369    select="(preceding::sect1[$chunk.section.depth &gt; 0][1]
370             |preceding::sect2[$chunk.section.depth &gt; 1][1]
371             |preceding::sect3[$chunk.section.depth &gt; 2][1]
372             |preceding::sect4[$chunk.section.depth &gt; 3][1]
373             |preceding::sect5[$chunk.section.depth &gt; 4][1]
374             |preceding::section[$chunk.section.depth &gt; count(ancestor::section)][1])[last()]"/>
375
376  <xsl:variable name="prev-v2"
377    select="(ancestor::sect1[$chunk.section.depth &gt; 0][1]
378             |ancestor::sect2[$chunk.section.depth &gt; 1][1]
379             |ancestor::sect3[$chunk.section.depth &gt; 2][1]
380             |ancestor::sect4[$chunk.section.depth &gt; 3][1]
381             |ancestor::sect5[$chunk.section.depth &gt; 4][1]
382             |ancestor::section[$chunk.section.depth &gt; count(ancestor::section)][1])[last()]"/>
383
384  <xsl:variable name="prev"
385    select="(preceding::book[1]
386             |preceding::preface[1]
387             |preceding::chapter[1]
388             |preceding::appendix[1]
389             |preceding::part[1]
390             |preceding::reference[1]
391             |preceding::refentry[1]
392             |preceding::colophon[1]
393             |preceding::article[1]
394             |preceding::bibliography[parent::article or parent::book or parent::part][1]
395             |preceding::glossary[parent::article or parent::book or parent::part][1]
396             |preceding::index[$generate.index != 0]
397	                       [parent::article or parent::book or parent::part][1]
398             |preceding::setindex[$generate.index != 0][1]
399             |ancestor::set
400             |ancestor::book[1]
401             |ancestor::preface[1]
402             |ancestor::chapter[1]
403             |ancestor::appendix[1]
404             |ancestor::part[1]
405             |ancestor::reference[1]
406             |ancestor::article[1]
407             |$prev-v1
408             |$prev-v2)[last()]"/>
409
410  <xsl:variable name="next-v1"
411    select="(following::sect1[$chunk.section.depth &gt; 0][1]
412             |following::sect2[$chunk.section.depth &gt; 1][1]
413             |following::sect3[$chunk.section.depth &gt; 2][1]
414             |following::sect4[$chunk.section.depth &gt; 3][1]
415             |following::sect5[$chunk.section.depth &gt; 4][1]
416             |following::section[$chunk.section.depth &gt; count(ancestor::section)][1])[1]"/>
417
418  <xsl:variable name="next-v2"
419    select="(descendant::sect1[$chunk.section.depth &gt; 0][1]
420             |descendant::sect2[$chunk.section.depth &gt; 1][1]
421             |descendant::sect3[$chunk.section.depth &gt; 2][1]
422             |descendant::sect4[$chunk.section.depth &gt; 3][1]
423             |descendant::sect5[$chunk.section.depth &gt; 4][1]
424             |descendant::section[$chunk.section.depth 
425                                  &gt; count(ancestor::section)][1])[1]"/>
426
427  <xsl:variable name="next"
428    select="(following::book[1]
429             |following::preface[1]
430             |following::chapter[1]
431             |following::appendix[1]
432             |following::part[1]
433             |following::reference[1]
434             |following::refentry[1]
435             |following::colophon[1]
436             |following::bibliography[parent::article or parent::book or parent::part][1]
437             |following::glossary[parent::article or parent::book or parent::part][1]
438             |following::index[$generate.index != 0]
439	                       [parent::article or parent::book][1]
440             |following::article[1]
441             |following::setindex[$generate.index != 0][1]
442             |descendant::book[1]
443             |descendant::preface[1]
444             |descendant::chapter[1]
445             |descendant::appendix[1]
446             |descendant::article[1]
447             |descendant::bibliography[parent::article or parent::book][1]
448             |descendant::glossary[parent::article or parent::book or parent::part][1]
449             |descendant::index[$generate.index != 0]
450	                       [parent::article or parent::book][1]
451             |descendant::colophon[1]
452             |descendant::setindex[$generate.index != 0][1]
453             |descendant::part[1]
454             |descendant::reference[1]
455             |descendant::refentry[1]
456             |$next-v1
457             |$next-v2)[1]"/>
458
459  <xsl:call-template name="process-chunk">
460    <xsl:with-param name="prev" select="$prev"/>
461    <xsl:with-param name="next" select="$next"/>
462    <xsl:with-param name="content" select="$content"/>
463  </xsl:call-template>
464</xsl:template>
465
466<!-- ==================================================================== -->
467
468<xsl:template match="/">
469  <xsl:choose>
470    <xsl:when test="function-available('exsl:node-set')
471		    and (*/self::ng:* or */self::db:*)">
472      <!-- Hack! If someone hands us a DocBook V5.x or DocBook NG document,
473	   toss the namespace and continue. Someday we'll reverse this logic
474	   and add the namespace to documents that don't have one.
475	   But not before the whole stylesheet has been converted to use
476	   namespaces. i.e., don't hold your breath -->
477      <xsl:message>Stripping NS from DocBook 5/NG document.</xsl:message>
478      <xsl:variable name="nons">
479	<xsl:apply-templates mode="stripNS"/>
480      </xsl:variable>
481      <xsl:message>Processing stripped document.</xsl:message>
482      <xsl:apply-templates select="exsl:node-set($nons)"/>
483    </xsl:when>
484    <xsl:otherwise>
485      <xsl:choose>
486	<xsl:when test="$rootid != ''">
487	  <xsl:choose>
488	    <xsl:when test="count(key('id',$rootid)) = 0">
489	      <xsl:message terminate="yes">
490		<xsl:text>ID '</xsl:text>
491		<xsl:value-of select="$rootid"/>
492		<xsl:text>' not found in document.</xsl:text>
493	      </xsl:message>
494	    </xsl:when>
495	    <xsl:otherwise>
496	      <xsl:if test="$collect.xref.targets = 'yes' or
497			    $collect.xref.targets = 'only'">
498		<xsl:apply-templates select="key('id', $rootid)"
499				     mode="collect.targets"/>
500	      </xsl:if>
501	      <xsl:if test="$collect.xref.targets != 'only'">
502		<xsl:apply-templates select="key('id',$rootid)"
503				     mode="process.root"/>
504		<xsl:if test="$tex.math.in.alt != ''">
505		  <xsl:apply-templates select="key('id',$rootid)"
506				       mode="collect.tex.math"/>
507		</xsl:if>
508		<xsl:if test="$generate.manifest != 0">
509		  <xsl:call-template name="generate.manifest">
510		    <xsl:with-param name="node" select="key('id',$rootid)"/>
511		  </xsl:call-template>
512		</xsl:if>
513	      </xsl:if>
514	    </xsl:otherwise>
515	  </xsl:choose>
516	</xsl:when>
517	<xsl:otherwise>
518	  <xsl:if test="$collect.xref.targets = 'yes' or
519			$collect.xref.targets = 'only'">
520	    <xsl:apply-templates select="/" mode="collect.targets"/>
521	  </xsl:if>
522	  <xsl:if test="$collect.xref.targets != 'only'">
523	    <xsl:apply-templates select="/" mode="process.root"/>
524	    <xsl:if test="$tex.math.in.alt != ''">
525	      <xsl:apply-templates select="/" mode="collect.tex.math"/>
526	    </xsl:if>
527	    <xsl:if test="$generate.manifest != 0">
528	      <xsl:call-template name="generate.manifest">
529		<xsl:with-param name="node" select="/"/>
530	      </xsl:call-template>
531	    </xsl:if>
532	  </xsl:if>
533	</xsl:otherwise>
534      </xsl:choose>
535    </xsl:otherwise>
536  </xsl:choose>
537</xsl:template>
538
539<xsl:template match="*" mode="process.root">
540  <xsl:apply-templates select="."/>
541</xsl:template>
542
543<!-- ====================================================================== -->
544
545<xsl:template match="set|book|part|preface|chapter|appendix
546                     |article
547                     |reference|refentry
548                     |book/glossary|article/glossary|part/glossary
549                     |book/bibliography|article/bibliography|part/bibliography
550                     |colophon">
551  <xsl:choose>
552    <xsl:when test="$onechunk != 0 and parent::*">
553      <xsl:apply-imports/>
554    </xsl:when>
555    <xsl:otherwise>
556      <xsl:call-template name="process-chunk-element"/>
557    </xsl:otherwise>
558  </xsl:choose>
559</xsl:template>
560
561<xsl:template match="sect1|sect2|sect3|sect4|sect5|section">
562  <xsl:variable name="ischunk">
563    <xsl:call-template name="chunk"/>
564  </xsl:variable>
565
566  <xsl:choose>
567    <xsl:when test="not(parent::*)">
568      <xsl:call-template name="process-chunk-element"/>
569    </xsl:when>
570    <xsl:when test="$ischunk = 0">
571      <xsl:apply-imports/>
572    </xsl:when>
573    <xsl:otherwise>
574      <xsl:call-template name="process-chunk-element"/>
575    </xsl:otherwise>
576  </xsl:choose>
577</xsl:template>
578
579<xsl:template match="setindex
580                     |book/index
581                     |article/index
582                     |part/index">
583  <!-- some implementations use completely empty index tags to indicate -->
584  <!-- where an automatically generated index should be inserted. so -->
585  <!-- if the index is completely empty, skip it. -->
586  <xsl:if test="count(*)>0 or $generate.index != '0'">
587    <xsl:call-template name="process-chunk-element"/>
588  </xsl:if>
589</xsl:template>
590
591<!-- ==================================================================== -->
592
593<xsl:template name="make.lots">
594  <xsl:param name="toc.params" select="''"/>
595  <xsl:param name="toc"/>
596
597  <xsl:variable name="lots">
598    <xsl:if test="contains($toc.params, 'toc')">
599      <xsl:copy-of select="$toc"/>
600    </xsl:if>
601
602    <xsl:if test="contains($toc.params, 'figure')">
603      <xsl:choose>
604        <xsl:when test="$chunk.separate.lots != '0'">
605          <xsl:call-template name="make.lot.chunk">
606            <xsl:with-param name="type" select="'figure'"/>
607            <xsl:with-param name="lot">
608              <xsl:call-template name="list.of.titles">
609                <xsl:with-param name="titles" select="'figure'"/>
610                <xsl:with-param name="nodes" select=".//figure"/>
611              </xsl:call-template>
612            </xsl:with-param>
613          </xsl:call-template>
614        </xsl:when>
615        <xsl:otherwise>
616          <xsl:call-template name="list.of.titles">
617            <xsl:with-param name="titles" select="'figure'"/>
618            <xsl:with-param name="nodes" select=".//figure"/>
619          </xsl:call-template>
620        </xsl:otherwise>
621      </xsl:choose>
622    </xsl:if>
623
624    <xsl:if test="contains($toc.params, 'table')">
625      <xsl:choose>
626        <xsl:when test="$chunk.separate.lots != '0'">
627          <xsl:call-template name="make.lot.chunk">
628            <xsl:with-param name="type" select="'table'"/>
629            <xsl:with-param name="lot">
630              <xsl:call-template name="list.of.titles">
631                <xsl:with-param name="titles" select="'table'"/>
632                <xsl:with-param name="nodes" select=".//table"/>
633              </xsl:call-template>
634            </xsl:with-param>
635          </xsl:call-template>
636        </xsl:when>
637        <xsl:otherwise>
638          <xsl:call-template name="list.of.titles">
639            <xsl:with-param name="titles" select="'table'"/>
640            <xsl:with-param name="nodes" select=".//table"/>
641          </xsl:call-template>
642        </xsl:otherwise>
643      </xsl:choose>
644    </xsl:if>
645
646    <xsl:if test="contains($toc.params, 'example')">
647      <xsl:choose>
648        <xsl:when test="$chunk.separate.lots != '0'">
649          <xsl:call-template name="make.lot.chunk">
650            <xsl:with-param name="type" select="'example'"/>
651            <xsl:with-param name="lot">
652              <xsl:call-template name="list.of.titles">
653                <xsl:with-param name="titles" select="'example'"/>
654                <xsl:with-param name="nodes" select=".//example"/>
655              </xsl:call-template>
656            </xsl:with-param>
657          </xsl:call-template>
658        </xsl:when>
659        <xsl:otherwise>
660          <xsl:call-template name="list.of.titles">
661            <xsl:with-param name="titles" select="'example'"/>
662            <xsl:with-param name="nodes" select=".//example"/>
663          </xsl:call-template>
664        </xsl:otherwise>
665      </xsl:choose>
666    </xsl:if>
667
668    <xsl:if test="contains($toc.params, 'equation')">
669      <xsl:choose>
670        <xsl:when test="$chunk.separate.lots != '0'">
671          <xsl:call-template name="make.lot.chunk">
672            <xsl:with-param name="type" select="'equation'"/>
673            <xsl:with-param name="lot">
674              <xsl:call-template name="list.of.titles">
675                <xsl:with-param name="titles" select="'equation'"/>
676                <xsl:with-param name="nodes" select=".//equation"/>
677              </xsl:call-template>
678            </xsl:with-param>
679          </xsl:call-template>
680        </xsl:when>
681        <xsl:otherwise>
682          <xsl:call-template name="list.of.titles">
683            <xsl:with-param name="titles" select="'equation'"/>
684            <xsl:with-param name="nodes" select=".//equation"/>
685          </xsl:call-template>
686        </xsl:otherwise>
687      </xsl:choose>
688    </xsl:if>
689
690    <xsl:if test="contains($toc.params, 'procedure')">
691      <xsl:choose>
692        <xsl:when test="$chunk.separate.lots != '0'">
693          <xsl:call-template name="make.lot.chunk">
694            <xsl:with-param name="type" select="'procedure'"/>
695            <xsl:with-param name="lot">
696              <xsl:call-template name="list.of.titles">
697                <xsl:with-param name="titles" select="'procedure'"/>
698                <xsl:with-param name="nodes" select=".//procedure[title]"/>
699              </xsl:call-template>
700            </xsl:with-param>
701          </xsl:call-template>
702        </xsl:when>
703        <xsl:otherwise>
704          <xsl:call-template name="list.of.titles">
705            <xsl:with-param name="titles" select="'procedure'"/>
706            <xsl:with-param name="nodes" select=".//procedure[title]"/>
707          </xsl:call-template>
708        </xsl:otherwise>
709      </xsl:choose>
710    </xsl:if>
711  </xsl:variable>
712
713  <xsl:if test="string($lots) != ''">
714    <xsl:choose>
715      <xsl:when test="$chunk.tocs.and.lots != 0 and not(parent::*)">
716        <xsl:call-template name="write.chunk">
717          <xsl:with-param name="filename">
718            <xsl:call-template name="make-relative-filename">
719              <xsl:with-param name="base.dir" select="$base.dir"/>
720              <xsl:with-param name="base.name">
721                <xsl:call-template name="dbhtml-dir"/>
722                <xsl:apply-templates select="." mode="recursive-chunk-filename">
723                  <xsl:with-param name="recursive" select="true()"/>
724                </xsl:apply-templates>
725                <xsl:text>-toc</xsl:text>
726                <xsl:value-of select="$html.ext"/>
727              </xsl:with-param>
728            </xsl:call-template>
729          </xsl:with-param>
730          <xsl:with-param name="content">
731            <xsl:call-template name="chunk-element-content">
732              <xsl:with-param name="prev" select="/foo"/>
733              <xsl:with-param name="next" select="/foo"/>
734              <xsl:with-param name="nav.context" select="'toc'"/>
735              <xsl:with-param name="content">
736		<xsl:if test="$chunk.tocs.and.lots.has.title != 0">
737		  <h1>
738		    <xsl:apply-templates select="." mode="object.title.markup"/>
739		  </h1>
740		</xsl:if>
741                <xsl:copy-of select="$lots"/>
742              </xsl:with-param>
743            </xsl:call-template>
744          </xsl:with-param>
745          <xsl:with-param name="quiet" select="$chunk.quietly"/>
746        </xsl:call-template>
747      </xsl:when>
748      <xsl:otherwise>
749        <xsl:copy-of select="$lots"/>
750      </xsl:otherwise>
751    </xsl:choose>
752  </xsl:if>
753</xsl:template>
754
755<xsl:template name="make.lot.chunk">
756  <xsl:param name="type" select="''"/>
757  <xsl:param name="lot"/>
758
759  <xsl:if test="string($lot) != ''">
760    <xsl:variable name="filename">
761      <xsl:call-template name="make-relative-filename">
762        <xsl:with-param name="base.dir" select="$base.dir"/>
763        <xsl:with-param name="base.name">
764          <xsl:call-template name="dbhtml-dir"/>
765          <xsl:value-of select="$type"/>
766          <xsl:text>-toc</xsl:text>
767          <xsl:value-of select="$html.ext"/>
768        </xsl:with-param>
769      </xsl:call-template>
770    </xsl:variable>
771
772    <xsl:variable name="href">
773      <xsl:call-template name="make-relative-filename">
774        <xsl:with-param name="base.name">
775          <xsl:call-template name="dbhtml-dir"/>
776          <xsl:value-of select="$type"/>
777          <xsl:text>-toc</xsl:text>
778          <xsl:value-of select="$html.ext"/>
779        </xsl:with-param>
780      </xsl:call-template>
781    </xsl:variable>
782
783    <xsl:call-template name="write.chunk">
784      <xsl:with-param name="filename" select="$filename"/>
785      <xsl:with-param name="content">
786        <xsl:call-template name="chunk-element-content">
787          <xsl:with-param name="prev" select="/foo"/>
788          <xsl:with-param name="next" select="/foo"/>
789          <xsl:with-param name="nav.context" select="'toc'"/>
790          <xsl:with-param name="content">
791            <xsl:copy-of select="$lot"/>
792          </xsl:with-param>
793        </xsl:call-template>
794      </xsl:with-param>
795      <xsl:with-param name="quiet" select="$chunk.quietly"/>
796    </xsl:call-template>
797    <!-- And output a link to this file -->
798    <div>
799      <xsl:attribute name="class">
800        <xsl:text>ListofTitles</xsl:text>
801      </xsl:attribute>
802      <a href="{$href}">
803        <xsl:call-template name="gentext">
804          <xsl:with-param name="key">
805            <xsl:choose>
806              <xsl:when test="$type='table'">ListofTables</xsl:when>
807              <xsl:when test="$type='figure'">ListofFigures</xsl:when>
808              <xsl:when test="$type='equation'">ListofEquations</xsl:when>
809              <xsl:when test="$type='example'">ListofExamples</xsl:when>
810              <xsl:when test="$type='procedure'">ListofProcedures</xsl:when>
811              <xsl:otherwise>ListofUnknown</xsl:otherwise>
812            </xsl:choose>
813          </xsl:with-param>
814        </xsl:call-template>
815      </a>
816    </div>
817  </xsl:if>
818</xsl:template>
819
820<!-- ==================================================================== -->
821
822<xsl:template name="in.other.chunk">
823  <xsl:param name="chunk" select="."/>
824  <xsl:param name="node" select="."/>
825
826  <xsl:variable name="is.chunk">
827    <xsl:call-template name="chunk">
828      <xsl:with-param name="node" select="$node"/>
829    </xsl:call-template>
830  </xsl:variable>
831
832<!--
833  <xsl:message>
834    <xsl:text>in.other.chunk: </xsl:text>
835    <xsl:value-of select="name($chunk)"/>
836    <xsl:text> </xsl:text>
837    <xsl:value-of select="name($node)"/>
838    <xsl:text> </xsl:text>
839    <xsl:value-of select="$chunk = $node"/>
840    <xsl:text> </xsl:text>
841    <xsl:value-of select="$is.chunk"/>
842  </xsl:message>
843-->
844
845  <xsl:choose>
846    <xsl:when test="$chunk = $node">0</xsl:when>
847    <xsl:when test="$is.chunk = 1">1</xsl:when>
848    <xsl:when test="count($node) = 0">0</xsl:when>
849    <xsl:otherwise>
850      <xsl:call-template name="in.other.chunk">
851        <xsl:with-param name="chunk" select="$chunk"/>
852        <xsl:with-param name="node" select="$node/parent::*"/>
853      </xsl:call-template>
854    </xsl:otherwise>
855  </xsl:choose>
856</xsl:template>
857
858<xsl:template name="count.footnotes.in.this.chunk">
859  <xsl:param name="node" select="."/>
860  <xsl:param name="footnotes" select="$node//footnote"/>
861  <xsl:param name="count" select="0"/>
862
863<!--
864  <xsl:message>
865    <xsl:text>count.footnotes.in.this.chunk: </xsl:text>
866    <xsl:value-of select="name($node)"/>
867  </xsl:message>
868-->
869
870  <xsl:variable name="in.other.chunk">
871    <xsl:call-template name="in.other.chunk">
872      <xsl:with-param name="chunk" select="$node"/>
873      <xsl:with-param name="node" select="$footnotes[1]"/>
874    </xsl:call-template>
875  </xsl:variable>
876
877  <xsl:choose>
878    <xsl:when test="count($footnotes) = 0">
879      <xsl:value-of select="$count"/>
880    </xsl:when>
881    <xsl:otherwise>
882      <xsl:choose>
883        <xsl:when test="$in.other.chunk != 0">
884          <xsl:call-template name="count.footnotes.in.this.chunk">
885            <xsl:with-param name="node" select="$node"/>
886            <xsl:with-param name="footnotes"
887                            select="$footnotes[position() &gt; 1]"/>
888            <xsl:with-param name="count" select="$count"/>
889          </xsl:call-template>
890        </xsl:when>
891        <xsl:when test="$footnotes[1]/ancestor::table
892                        |$footnotes[1]/ancestor::informaltable">
893          <xsl:call-template name="count.footnotes.in.this.chunk">
894            <xsl:with-param name="node" select="$node"/>
895            <xsl:with-param name="footnotes"
896                            select="$footnotes[position() &gt; 1]"/>
897            <xsl:with-param name="count" select="$count"/>
898          </xsl:call-template>
899        </xsl:when>
900        <xsl:otherwise>
901          <xsl:call-template name="count.footnotes.in.this.chunk">
902            <xsl:with-param name="node" select="$node"/>
903            <xsl:with-param name="footnotes"
904                            select="$footnotes[position() &gt; 1]"/>
905            <xsl:with-param name="count" select="$count + 1"/>
906          </xsl:call-template>
907        </xsl:otherwise>
908      </xsl:choose>
909    </xsl:otherwise>
910  </xsl:choose>
911</xsl:template>
912
913<xsl:template name="process.footnotes.in.this.chunk">
914  <xsl:param name="node" select="."/>
915  <xsl:param name="footnotes" select="$node//footnote"/>
916
917<!--
918  <xsl:message>process.footnotes.in.this.chunk</xsl:message>
919-->
920
921  <xsl:variable name="in.other.chunk">
922    <xsl:call-template name="in.other.chunk">
923      <xsl:with-param name="chunk" select="$node"/>
924      <xsl:with-param name="node" select="$footnotes[1]"/>
925    </xsl:call-template>
926  </xsl:variable>
927
928  <xsl:choose>
929    <xsl:when test="count($footnotes) = 0">
930      <!-- nop -->
931    </xsl:when>
932    <xsl:otherwise>
933      <xsl:choose>
934        <xsl:when test="$in.other.chunk != 0">
935          <xsl:call-template name="process.footnotes.in.this.chunk">
936            <xsl:with-param name="node" select="$node"/>
937            <xsl:with-param name="footnotes"
938                            select="$footnotes[position() &gt; 1]"/>
939          </xsl:call-template>
940        </xsl:when>
941        <xsl:when test="$footnotes[1]/ancestor::table
942                        |$footnotes[1]/ancestor::informaltable">
943          <xsl:call-template name="process.footnotes.in.this.chunk">
944            <xsl:with-param name="node" select="$node"/>
945            <xsl:with-param name="footnotes"
946                            select="$footnotes[position() &gt; 1]"/>
947          </xsl:call-template>
948        </xsl:when>
949        <xsl:otherwise>
950          <xsl:apply-templates select="$footnotes[1]"
951                               mode="process.footnote.mode"/>
952          <xsl:call-template name="process.footnotes.in.this.chunk">
953            <xsl:with-param name="node" select="$node"/>
954            <xsl:with-param name="footnotes"
955                            select="$footnotes[position() &gt; 1]"/>
956          </xsl:call-template>
957        </xsl:otherwise>
958      </xsl:choose>
959    </xsl:otherwise>
960  </xsl:choose>
961</xsl:template>
962
963<xsl:template name="process.footnotes">
964  <xsl:variable name="footnotes" select=".//footnote"/>
965  <xsl:variable name="fcount">
966    <xsl:call-template name="count.footnotes.in.this.chunk">
967      <xsl:with-param name="node" select="."/>
968      <xsl:with-param name="footnotes" select="$footnotes"/>
969    </xsl:call-template>
970  </xsl:variable>
971
972<!--
973  <xsl:message>
974    <xsl:value-of select="name(.)"/>
975    <xsl:text> fcount: </xsl:text>
976    <xsl:value-of select="$fcount"/>
977  </xsl:message>
978-->
979
980  <!-- Only bother to do this if there's at least one non-table footnote -->
981  <xsl:if test="$fcount &gt; 0">
982    <div class="footnotes">
983      <br/>
984      <hr width="100" align="left"/>
985      <xsl:call-template name="process.footnotes.in.this.chunk">
986        <xsl:with-param name="node" select="."/>
987        <xsl:with-param name="footnotes" select="$footnotes"/>
988      </xsl:call-template>
989    </div>
990  </xsl:if>
991
992  <!-- FIXME: When chunking, only the annotations actually used
993              in this chunk should be referenced. I don't think it
994	      does any harm to reference them all, but it adds
995	      unnecessary bloat to each chunk. -->
996  <xsl:if test="$annotation.support != 0 and //annotation">
997    <div class="annotation-list">
998      <div class="annotation-nocss">
999	<p>The following annotations are from this essay. You are seeing
1000	them here because your browser doesn’t support the user-interface
1001	techniques used to make them appear as ‘popups’ on modern browsers.</p>
1002      </div>
1003
1004      <xsl:apply-templates select="//annotation"
1005			   mode="annotation-popup"/>
1006    </div>
1007  </xsl:if>
1008</xsl:template>
1009
1010<xsl:template name="process.chunk.footnotes">
1011  <xsl:variable name="is.chunk">
1012    <xsl:call-template name="chunk"/>
1013  </xsl:variable>
1014  <xsl:if test="$is.chunk = 1">
1015    <xsl:call-template name="process.footnotes"/>
1016  </xsl:if>
1017</xsl:template>
1018
1019<!-- ====================================================================== -->
1020
1021<!-- Resolve xml:base attributes -->
1022<xsl:template match="@fileref">
1023  <!-- need a check for absolute urls -->
1024  <xsl:choose>
1025    <xsl:when test="contains(., ':')">
1026      <!-- it has a uri scheme so it is an absolute uri -->
1027      <xsl:value-of select="."/>
1028    </xsl:when>
1029    <xsl:otherwise>
1030      <!-- its a relative uri -->
1031      <xsl:call-template name="relative-uri">
1032        <xsl:with-param name="destdir">
1033          <xsl:call-template name="dbhtml-dir">
1034            <xsl:with-param name="context" select=".."/>
1035          </xsl:call-template>
1036        </xsl:with-param>
1037      </xsl:call-template>
1038    </xsl:otherwise>
1039  </xsl:choose>
1040</xsl:template>
1041
1042</xsl:stylesheet>
1043