]> git.stg.codes - stg.git/blob - doc/xslt/html/footnote.xsl
Set output encoding to utf-8.
[stg.git] / doc / xslt / html / footnote.xsl
1 <?xml version='1.0'?>
2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3                 xmlns:exsl="http://exslt.org/common"
4                 exclude-result-prefixes="exsl"
5                 version='1.0'>
6
7 <!-- ********************************************************************
8      $Id: footnote.xsl 8812 2010-08-09 20:51:51Z bobstayton $
9      ********************************************************************
10
11      This file is part of the XSL DocBook Stylesheet distribution.
12      See ../README or http://docbook.sf.net/release/xsl/current/ for
13      copyright and other information.
14
15      ******************************************************************** -->
16
17 <xsl:template match="footnote">
18   <xsl:variable name="name">
19     <xsl:call-template name="object.id"/>
20   </xsl:variable>
21   <xsl:variable name="href">
22     <xsl:text>#ftn.</xsl:text>
23     <xsl:call-template name="object.id"/>
24   </xsl:variable>
25
26   <xsl:choose>
27     <xsl:when test="ancestor::table or ancestor::informaltable">
28       <sup>
29         <xsl:text>[</xsl:text>
30         <a name="{$name}" href="{$href}">
31           <xsl:apply-templates select="." mode="class.attribute"/>
32           <xsl:apply-templates select="." mode="footnote.number"/>
33         </a>
34         <xsl:text>]</xsl:text>
35       </sup>
36     </xsl:when>
37     <xsl:otherwise>
38       <sup>
39         <xsl:text>[</xsl:text>
40         <a name="{$name}" href="{$href}">
41           <xsl:apply-templates select="." mode="class.attribute"/>
42           <xsl:apply-templates select="." mode="footnote.number"/>
43         </a>
44         <xsl:text>]</xsl:text>
45       </sup>
46     </xsl:otherwise>
47   </xsl:choose>
48 </xsl:template>
49
50 <xsl:template match="footnoteref">
51   <xsl:variable name="targets" select="key('id',@linkend)"/>
52   <xsl:variable name="footnote" select="$targets[1]"/>
53
54   <xsl:if test="not(local-name($footnote) = 'footnote')">
55    <xsl:message terminate="yes">
56 ERROR: A footnoteref element has a linkend that points to an element that is not a footnote. 
57 Typically this happens when an id attribute is accidentally applied to the child of a footnote element. 
58 target element: <xsl:value-of select="local-name($footnote)"/>
59 linkend/id: <xsl:value-of select="@linkend"/>
60    </xsl:message>
61   </xsl:if>
62
63   <xsl:variable name="target.href">
64     <xsl:call-template name="href.target">
65       <xsl:with-param name="object" select="$footnote"/>
66     </xsl:call-template>
67   </xsl:variable>
68
69   <xsl:variable name="href">
70     <xsl:value-of select="substring-before($target.href, '#')"/>
71     <xsl:text>#ftn.</xsl:text>
72     <xsl:value-of select="substring-after($target.href, '#')"/>
73   </xsl:variable>
74
75   <sup>
76     <xsl:text>[</xsl:text>
77     <a href="{$href}">
78       <xsl:apply-templates select="." mode="class.attribute"/>
79       <xsl:apply-templates select="$footnote" mode="footnote.number"/>
80     </a>
81     <xsl:text>]</xsl:text>
82   </sup>
83 </xsl:template>
84
85 <xsl:template match="footnote" mode="footnote.number">
86   <xsl:choose>
87     <xsl:when test="string-length(@label) != 0">
88       <xsl:value-of select="@label"/>
89     </xsl:when>
90     <xsl:when test="ancestor::table or ancestor::informaltable">
91       <xsl:variable name="tfnum">
92         <xsl:number level="any" from="table|informaltable" format="1"/>
93       </xsl:variable>
94
95       <xsl:choose>
96         <xsl:when test="string-length($table.footnote.number.symbols) &gt;= $tfnum">
97           <xsl:value-of select="substring($table.footnote.number.symbols, $tfnum, 1)"/>
98         </xsl:when>
99         <xsl:otherwise>
100           <xsl:number level="any" from="table | informaltable"
101                       format="{$table.footnote.number.format}"/>
102         </xsl:otherwise>
103       </xsl:choose>
104     </xsl:when>
105     <xsl:otherwise>
106       <xsl:variable name="pfoot" select="preceding::footnote[not(@label)]"/>
107       <xsl:variable name="ptfoot" select="preceding::table//footnote |
108                                           preceding::informaltable//footnote"/>
109       <xsl:variable name="fnum" select="count($pfoot) - count($ptfoot) + 1"/>
110
111       <xsl:choose>
112         <xsl:when test="string-length($footnote.number.symbols) &gt;= $fnum">
113           <xsl:value-of select="substring($footnote.number.symbols, $fnum, 1)"/>
114         </xsl:when>
115         <xsl:otherwise>
116           <xsl:number value="$fnum" format="{$footnote.number.format}"/>
117         </xsl:otherwise>
118       </xsl:choose>
119     </xsl:otherwise>
120   </xsl:choose>
121 </xsl:template>
122
123 <!-- ==================================================================== -->
124
125 <xsl:template match="footnote/para[1]|footnote/simpara[1]" priority="2">
126   <!-- this only works if the first thing in a footnote is a para, -->
127   <!-- which is ok, because it usually is. -->
128   <xsl:variable name="name">
129     <xsl:text>ftn.</xsl:text>
130     <xsl:call-template name="object.id">
131       <xsl:with-param name="object" select="ancestor::footnote"/>
132     </xsl:call-template>
133   </xsl:variable>
134   <xsl:variable name="href">
135     <xsl:text>#</xsl:text>
136     <xsl:call-template name="object.id">
137       <xsl:with-param name="object" select="ancestor::footnote"/>
138     </xsl:call-template>
139   </xsl:variable>
140
141   <xsl:call-template name="paragraph">
142     <xsl:with-param name="class">
143       <xsl:if test="@role and $para.propagates.style != 0">
144         <xsl:value-of select="@role"/>
145       </xsl:if>
146     </xsl:with-param>
147     <xsl:with-param name="content">
148       <sup>
149         <xsl:text>[</xsl:text>
150         <a id="{$name}" href="{$href}">
151           <xsl:apply-templates select="." mode="class.attribute"/>
152           <xsl:apply-templates select="ancestor::footnote" mode="footnote.number"/>
153         </a>
154         <xsl:text>] </xsl:text>
155       </sup>
156       <xsl:apply-templates/>
157     </xsl:with-param>
158   </xsl:call-template>
159
160 </xsl:template>
161
162 <!-- ==================================================================== -->
163
164 <xsl:template match="*" mode="footnote.body.number">
165   <xsl:variable name="name">
166     <xsl:text>ftn.</xsl:text>
167     <xsl:call-template name="object.id">
168       <xsl:with-param name="object" select="ancestor::footnote"/>
169     </xsl:call-template>
170   </xsl:variable>
171   <xsl:variable name="href">
172     <xsl:text>#</xsl:text>
173     <xsl:call-template name="object.id">
174       <xsl:with-param name="object" select="ancestor::footnote"/>
175     </xsl:call-template>
176   </xsl:variable>
177   <xsl:variable name="footnote.mark">
178     <sup>
179       <xsl:text>[</xsl:text>
180       <a name="{$name}" href="{$href}">
181         <xsl:apply-templates select="." mode="class.attribute"/>
182         <xsl:apply-templates select="ancestor::footnote"
183                              mode="footnote.number"/>
184       </a>
185       <xsl:text>] </xsl:text>
186     </sup>
187   </xsl:variable>
188
189   <xsl:variable name="html">
190     <xsl:apply-templates select="."/>
191   </xsl:variable>
192
193   <xsl:choose>
194     <xsl:when test="$exsl.node.set.available != 0">
195       <xsl:variable name="html-nodes" select="exsl:node-set($html)"/>
196       <xsl:choose>
197         <xsl:when test="$html-nodes//p">
198           <xsl:apply-templates select="$html-nodes" mode="insert.html.p">
199             <xsl:with-param name="mark" select="$footnote.mark"/>
200           </xsl:apply-templates>
201         </xsl:when>
202         <xsl:otherwise>
203           <xsl:apply-templates select="$html-nodes" mode="insert.html.text">
204             <xsl:with-param name="mark" select="$footnote.mark"/>
205           </xsl:apply-templates>
206         </xsl:otherwise>
207       </xsl:choose>
208     </xsl:when>
209     <xsl:otherwise>
210       <xsl:copy-of select="$html"/>
211     </xsl:otherwise>
212   </xsl:choose>
213 </xsl:template>
214
215 <!-- ==================================================================== -->
216
217 <!--
218 <xsl:template name="count-element-from">
219   <xsl:param name="from" select=".."/>
220   <xsl:param name="to" select="."/>
221   <xsl:param name="count" select="0"/>
222   <xsl:param name="list" select="$from/following::*[local-name(.)=local-name($to)]
223                                  |$from/descendant-or-self::*[local-name(.)=local-name($to)]"/>
224
225   <xsl:choose>
226     <xsl:when test="not($list)">
227       <xsl:text>-1</xsl:text>
228     </xsl:when>
229     <xsl:when test="$list[1] = $to">
230       <xsl:value-of select="$count + 1"/>
231     </xsl:when>
232     <xsl:otherwise>
233     </xsl:otherwise>
234   </xsl:choose>
235 </xsl:template>
236 -->
237
238 <!-- ==================================================================== -->
239
240 <xsl:template name="process.footnotes">
241   <xsl:variable name="footnotes" select=".//footnote"/>
242   <xsl:variable name="table.footnotes"
243                 select=".//table//footnote | .//informaltable//footnote"/>
244
245   <!-- Only bother to do this if there's at least one non-table footnote -->
246   <xsl:if test="count($footnotes)>count($table.footnotes)">
247     <div class="footnotes">
248       <br/>
249       <hr width="100" align="{$direction.align.start}"/>
250       <xsl:apply-templates select="$footnotes" mode="process.footnote.mode"/>
251     </div>
252   </xsl:if>
253
254   <xsl:if test="$annotation.support != 0 and //annotation">
255     <div class="annotation-list">
256       <div class="annotation-nocss">
257         <p>The following annotations are from this essay. You are seeing
258         them here because your browser doesn’t support the user-interface
259         techniques used to make them appear as ‘popups’ on modern browsers.</p>
260       </div>
261
262       <xsl:apply-templates select="//annotation"
263                            mode="annotation-popup"/>
264     </div>
265   </xsl:if>
266 </xsl:template>
267
268 <xsl:template name="process.chunk.footnotes">
269   <!-- nop -->
270 </xsl:template>
271
272 <xsl:template match="footnote" name="process.footnote" mode="process.footnote.mode">
273   <xsl:choose>
274     <xsl:when test="local-name(*[1]) = 'para' or local-name(*[1]) = 'simpara'">
275       <div>
276         <xsl:call-template name="common.html.attributes"/>
277         <xsl:apply-templates/>
278       </div>
279     </xsl:when>
280
281     <xsl:when test="$html.cleanup != 0 and 
282                     $exsl.node.set.available != 0">
283       <div>
284         <xsl:call-template name="common.html.attributes"/>
285         <xsl:apply-templates select="*[1]" mode="footnote.body.number"/>
286         <xsl:apply-templates select="*[position() &gt; 1]"/>
287       </div>
288     </xsl:when>
289
290     <xsl:otherwise>
291       <xsl:message>
292         <xsl:text>Warning: footnote number may not be generated </xsl:text>
293         <xsl:text>correctly; </xsl:text>
294         <xsl:value-of select="local-name(*[1])"/>
295         <xsl:text> unexpected as first child of footnote.</xsl:text>
296       </xsl:message>
297       <div>
298         <xsl:call-template name="common.html.attributes"/>
299         <xsl:apply-templates/>
300       </div>
301     </xsl:otherwise>
302   </xsl:choose>
303 </xsl:template>
304
305 <xsl:template match="table//footnote | informaltable//footnote"
306               mode="process.footnote.mode">
307 </xsl:template>
308
309 <xsl:template match="footnote" mode="table.footnote.mode">
310   <xsl:call-template name="process.footnote"/>
311 </xsl:template>
312
313 </xsl:stylesheet>