Imported Upstream version 1.79.2
[platform/upstream/docbook-xsl-stylesheets.git] / fo / toc.xsl
1 <?xml version='1.0'?>
2 <xsl:stylesheet exclude-result-prefixes="d"
3                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
4                 xmlns:d="http://docbook.org/ns/docbook"
5                 xmlns:fo="http://www.w3.org/1999/XSL/Format"
6                 version='1.0'>
7
8 <!-- ********************************************************************
9
10      This file is part of the XSL DocBook Stylesheet distribution.
11      See ../README or http://cdn.docbook.org/release/xsl/current/ for
12      copyright and other information.
13
14      ******************************************************************** -->
15
16 <!-- ==================================================================== -->
17
18 <!-- only set, book and part puts toc in its own page sequence -->
19
20 <xsl:template match="d:set/d:toc | d:book/d:toc | d:part/d:toc">
21   <xsl:variable name="toc.params">
22     <xsl:call-template name="find.path.params">
23       <xsl:with-param name="node" select="parent::*"/>
24       <xsl:with-param name="table" select="normalize-space($generate.toc)"/>
25     </xsl:call-template>
26   </xsl:variable>
27
28   <!-- Do not output the toc element if one is already generated
29        by the use of $generate.toc parameter, or if
30        generating a source toc is turned off -->
31   <xsl:if test="not(contains($toc.params, 'toc')) and
32                 ($process.source.toc != 0 or $process.empty.source.toc != 0)">
33     <!-- Don't generate a page sequence unless there is content -->
34     <xsl:variable name="content">
35       <xsl:choose>
36         <xsl:when test="* and $process.source.toc != 0">
37           <xsl:apply-templates />
38         </xsl:when>
39         <xsl:when test="count(*) = 0 and $process.empty.source.toc != 0">
40           <!-- trick to switch context node to parent element -->
41           <xsl:for-each select="parent::*">
42             <xsl:choose>
43               <xsl:when test="self::d:set">
44                 <xsl:call-template name="set.toc">
45                   <xsl:with-param name="toc.title.p" 
46                                   select="contains($toc.params, 'title')"/>
47                 </xsl:call-template>
48               </xsl:when>
49               <xsl:when test="self::d:book">
50                 <xsl:call-template name="division.toc">
51                   <xsl:with-param name="toc.title.p" 
52                                   select="contains($toc.params, 'title')"/>
53                 </xsl:call-template>
54               </xsl:when>
55               <xsl:when test="self::d:part">
56                 <xsl:call-template name="division.toc">
57                   <xsl:with-param name="toc.title.p" 
58                                   select="contains($toc.params, 'title')"/>
59                 </xsl:call-template>
60               </xsl:when>
61             </xsl:choose>
62           </xsl:for-each>
63         </xsl:when>
64       </xsl:choose>
65     </xsl:variable>
66
67     <xsl:if test="string-length(normalize-space($content)) != 0">
68       <xsl:variable name="lot-master-reference">
69         <xsl:call-template name="select.pagemaster">
70           <xsl:with-param name="pageclass" select="'lot'"/>
71         </xsl:call-template>
72       </xsl:variable>
73     
74       <xsl:call-template name="page.sequence">
75         <xsl:with-param name="master-reference"
76                         select="$lot-master-reference"/>
77         <xsl:with-param name="element" select="'toc'"/>
78         <xsl:with-param name="gentext-key" select="'TableofContents'"/>
79         <xsl:with-param name="content" select="$content"/>
80       </xsl:call-template>
81     </xsl:if>
82   </xsl:if>
83 </xsl:template>
84   
85 <xsl:template match="d:chapter/d:toc | d:appendix/d:toc | d:preface/d:toc | d:article/d:toc">
86   <xsl:variable name="toc.params">
87     <xsl:call-template name="find.path.params">
88       <xsl:with-param name="node" select="parent::*"/>
89       <xsl:with-param name="table" select="normalize-space($generate.toc)"/>
90     </xsl:call-template>
91   </xsl:variable>
92
93   <!-- Do not output the toc element if one is already generated
94        by the use of $generate.toc parameter, or if
95        generating a source toc is turned off -->
96   <xsl:if test="not(contains($toc.params, 'toc')) and
97                 ($process.source.toc != 0 or $process.empty.source.toc != 0)">
98     <xsl:choose>
99       <xsl:when test="* and $process.source.toc != 0">
100         <fo:block>
101           <xsl:apply-templates/> 
102         </fo:block>
103       </xsl:when>
104       <xsl:when test="count(*) = 0 and $process.empty.source.toc != 0">
105         <!-- trick to switch context node to section element -->
106         <xsl:for-each select="parent::*">
107           <xsl:call-template name="component.toc">
108             <xsl:with-param name="toc.title.p" 
109                             select="contains($toc.params, 'title')"/>
110           </xsl:call-template>
111         </xsl:for-each>
112       </xsl:when>
113     </xsl:choose>
114     <xsl:call-template name="component.toc.separator"/>
115   </xsl:if>
116 </xsl:template>
117
118 <xsl:template match="d:section/d:toc
119                     |d:sect1/d:toc
120                     |d:sect2/d:toc
121                     |d:sect3/d:toc
122                     |d:sect4/d:toc
123                     |d:sect5/d:toc">
124
125   <xsl:variable name="toc.params">
126     <xsl:call-template name="find.path.params">
127       <xsl:with-param name="node" select="parent::*"/>
128       <xsl:with-param name="table" select="normalize-space($generate.toc)"/>
129     </xsl:call-template>
130   </xsl:variable>
131
132   <!-- Do not output the toc element if one is already generated
133        by the use of $generate.toc parameter, or if
134        generating a source toc is turned off -->
135   <xsl:if test="not(contains($toc.params, 'toc')) and
136                 ($process.source.toc != 0 or $process.empty.source.toc != 0)">
137     <xsl:choose>
138       <xsl:when test="* and $process.source.toc != 0">
139         <fo:block>
140           <xsl:apply-templates/> 
141         </fo:block>
142       </xsl:when>
143       <xsl:when test="count(*) = 0 and $process.empty.source.toc != 0">
144         <!-- trick to switch context node to section element -->
145         <xsl:for-each select="parent::*">
146           <xsl:call-template name="section.toc">
147             <xsl:with-param name="toc.title.p" 
148                             select="contains($toc.params, 'title')"/>
149           </xsl:call-template>
150         </xsl:for-each>
151       </xsl:when>
152     </xsl:choose>
153     <xsl:call-template name="section.toc.separator"/>
154   </xsl:if>
155 </xsl:template>
156
157 <!-- ==================================================================== -->
158
159 <xsl:template match="d:tocpart|d:tocchap
160                      |d:toclevel1|d:toclevel2|d:toclevel3|d:toclevel4|d:toclevel5">
161   <xsl:apply-templates select="d:tocentry"/>
162   <xsl:if test="d:tocchap|d:toclevel1|d:toclevel2|d:toclevel3|d:toclevel4|d:toclevel5">
163     <fo:block start-indent="{count(ancestor::*)*2}pc">
164       <xsl:apply-templates select="d:tocchap|d:toclevel1|d:toclevel2|d:toclevel3|d:toclevel4|d:toclevel5"/>
165     </fo:block>
166   </xsl:if>
167 </xsl:template>
168
169 <xsl:template match="d:tocentry|d:lotentry|d:tocdiv|d:tocfront|d:tocback">
170   <fo:block end-indent="2pc"
171             last-line-end-indent="-2pc">
172     <xsl:if test="@linkend or @pagenum">
173       <xsl:attribute name="text-align-last">justify</xsl:attribute>
174     </xsl:if>
175     <fo:inline keep-with-next.within-line="always">
176       <xsl:choose>
177         <xsl:when test="@linkend">
178           <fo:basic-link internal-destination="{@linkend}">
179             <xsl:apply-templates/>
180           </fo:basic-link>
181         </xsl:when>
182         <xsl:otherwise>
183           <xsl:apply-templates/>
184         </xsl:otherwise>
185       </xsl:choose>
186     </fo:inline>
187
188     <xsl:choose>
189       <xsl:when test="@linkend">
190         <fo:inline keep-together.within-line="always">
191           <fo:leader xsl:use-attribute-sets="toc.leader.properties"/>
192           <fo:basic-link internal-destination="{@linkend}">
193             <xsl:choose>
194               <xsl:when test="@pagenum">
195                 <xsl:value-of select="@pagenum"/>
196               </xsl:when>
197               <xsl:otherwise>
198                 <fo:page-number-citation ref-id="{@linkend}"/>
199               </xsl:otherwise>
200             </xsl:choose>
201           </fo:basic-link>
202         </fo:inline>
203       </xsl:when>
204       <xsl:when test="@pagenum">
205         <fo:inline keep-together.within-line="always">
206           <fo:leader xsl:use-attribute-sets="toc.leader.properties"/>
207           <xsl:value-of select="@pagenum"/>
208         </fo:inline>
209       </xsl:when>
210     </xsl:choose>
211   </fo:block>
212 </xsl:template>
213
214 <xsl:template match="d:toc/d:title | d:tocdiv/d:title">
215   <fo:block font-weight="bold">
216     <xsl:apply-templates/>
217   </fo:block>
218 </xsl:template>
219
220 <xsl:template match="d:toc/d:subtitle | d:tocdiv/d:subtitle">
221   <fo:block font-weight="bold">
222     <xsl:apply-templates/>
223   </fo:block>
224 </xsl:template>
225
226 <xsl:template match="d:toc/d:titleabbrev |d:tocdiv/d:titleabbrev">
227 </xsl:template>
228
229 <!-- ==================================================================== -->
230
231 <!-- A lot element must have content, because there is no attribute
232      to select what kind of list should be generated -->
233 <xsl:template match="d:book/d:lot | d:part/d:lot">
234   <!-- Don't generate a page sequence unless there is content -->
235   <xsl:variable name="content">
236     <xsl:choose>
237       <xsl:when test="* and $process.source.toc != 0">
238         <xsl:apply-templates />
239       </xsl:when>
240       <xsl:when test="not(child::*) and $process.empty.source.toc != 0">
241         <xsl:call-template name="process.empty.lot"/>
242       </xsl:when>
243     </xsl:choose>
244   </xsl:variable>
245
246   <xsl:if test="string-length(normalize-space($content)) != 0">
247     <xsl:variable name="lot-master-reference">
248       <xsl:call-template name="select.pagemaster">
249         <xsl:with-param name="pageclass" select="'lot'"/>
250       </xsl:call-template>
251     </xsl:variable>
252   
253     <xsl:call-template name="page.sequence">
254       <xsl:with-param name="master-reference"
255                       select="$lot-master-reference"/>
256       <xsl:with-param name="element" select="'toc'"/>
257       <xsl:with-param name="content" select="$content"/>
258     </xsl:call-template>
259   </xsl:if>
260 </xsl:template>
261   
262 <xsl:template match="d:chapter/d:lot | d:appendix/d:lot | d:preface/d:lot | d:article/d:lot">
263   <xsl:choose>
264     <xsl:when test="* and $process.source.toc != 0">
265       <fo:block>
266         <xsl:apply-templates/> 
267       </fo:block>
268       <xsl:call-template name="component.toc.separator"/>
269     </xsl:when>
270     <xsl:when test="not(child::*) and $process.empty.source.toc != 0">
271       <xsl:call-template name="process.empty.lot"/>
272     </xsl:when>
273   </xsl:choose>
274 </xsl:template>
275
276 <xsl:template match="d:section/d:lot
277                     |d:sect1/d:lot
278                     |d:sect2/d:lot
279                     |d:sect3/d:lot
280                     |d:sect4/d:lot
281                     |d:sect5/d:lot">
282   <xsl:choose>
283     <xsl:when test="* and $process.source.toc != 0">
284       <fo:block>
285         <xsl:apply-templates/> 
286       </fo:block>
287       <xsl:call-template name="section.toc.separator"/>
288     </xsl:when>
289     <xsl:when test="not(child::*) and $process.empty.source.toc != 0">
290       <xsl:call-template name="process.empty.lot"/>
291     </xsl:when>
292   </xsl:choose>
293 </xsl:template>
294
295 <xsl:template name="process.empty.lot">
296   <!-- An empty lot element does not provide any information to indicate
297        what should be included in it.  You can customize this
298        template to generate a lot based on @role or something -->
299   <xsl:message>
300     <xsl:text>Warning: don't know what to generate for </xsl:text>
301     <xsl:text>lot that has no children.</xsl:text>
302   </xsl:message>
303 </xsl:template>
304
305 <xsl:template match="d:lot/d:title">
306   <fo:block font-weight="bold">
307     <xsl:apply-templates/>
308   </fo:block>
309 </xsl:template>
310
311 <xsl:template match="d:lot/d:subtitle">
312   <fo:block font-weight="bold">
313     <xsl:apply-templates/>
314   </fo:block>
315 </xsl:template>
316
317 <xsl:template match="d:lot/d:titleabbrev">
318 </xsl:template>
319
320 </xsl:stylesheet>