]> git.lyx.org Git - lyx.git/blob - lib/docbook/epub3/epub3-element-mods.xsl
Use same find-dialogs as other bind-files also for x?emacs
[lyx.git] / lib / docbook / epub3 / epub3-element-mods.xsl
1 <?xml version="1.0" encoding="utf-8"?>
2 <!DOCTYPE xsl:stylesheet [
3 <!ENTITY uppercase "'ABCDEFGHIJKLMNOPQRSTUVWXYZ'">
4 <!ENTITY lowercase "'abcdefghijklmnopqrstuvwxyz'">
5 ]>
6
7 <xsl:stylesheet 
8   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
9   xmlns:d="http://docbook.org/ns/docbook"
10   xmlns:exsl="http://exslt.org/common"
11   xmlns:set="http://exslt.org/sets"
12   xmlns="http://www.w3.org/1999/xhtml"
13   xmlns:epub="http://www.idpf.org/2007/ops"
14   xmlns:m="http://www.w3.org/1998/Math/MathML"
15   xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon"
16   xmlns:ssml="http://www.w3.org/2001/10/synthesis"
17   xmlns:svg="http://www.w3.org/2000/svg"
18   xmlns:opf="http://www.idpf.org/2007/opf"
19   xmlns:dc="http://purl.org/dc/elements/1.1/"  
20   xmlns:cf="http://docbook.sourceforge.net/xmlns/chunkfast/1.0"
21   xmlns:date="http://exslt.org/dates-and-times"
22   xmlns:dcterms="http://purl.org/dc/terms/"
23   xmlns:ncx="http://www.daisy.org/z3986/2005/ncx/"
24   xmlns:db="http://docbook.org/ns/docbook"
25   xmlns:stext="http://nwalsh.com/xslt/ext/com.nwalsh.saxon.TextFactory"
26   xmlns:str="http://exslt.org/strings"
27   xmlns:xtext="xalan://com.nwalsh.xalan.Text"
28
29   extension-element-prefixes="stext xtext"
30   exclude-result-prefixes="#default cf date db dc dcterms epub exsl m ncx opf pls set ssml stext str svg xtext d"
31   version="1.0">
32
33 <xsl:import href="titlepage.templates.xsl"/>
34
35 <!--==============================================================-->
36 <!--  DocBook XSL Parameter settings                              -->
37 <!--==============================================================-->
38 <!-- separate file for toc -->
39 <xsl:param name="chunk.tocs.and.lots" select="1"/>
40 <xsl:param name="toc.section.depth" select="2"/>
41 <xsl:param name="generate.toc">
42 book  toc,title,figure,table,example,equation
43 article  toc,title,figure,table,example,equation
44 </xsl:param>
45 <xsl:param name="generate.manifest" select="0"/>
46 <xsl:param name="manifest.in.base.dir" select="1"/>
47
48 <!-- HTML chunk output goes to $base.dir/OEPBS -->
49 <xsl:variable name="chunk.base.dir">
50   <xsl:choose>
51     <xsl:when test="$base.dir != '' and contains($base.dir, $epub.oebps.dir)">
52       <xsl:value-of select="substring-before($base.dir, $epub.oebps.dir)"/>
53     </xsl:when>
54     <!-- If epub.oebps.dir reset but base.dir still has OEBPS: -->
55     <xsl:when test="$base.dir != '' and contains($base.dir, 'OEBPS')">
56       <xsl:value-of select="substring-before($base.dir, 'OEBPS')"/>
57     </xsl:when>
58     <xsl:when test="string-length($base.dir) = 0"></xsl:when>
59     <!-- make sure to add trailing slash if omitted by user -->
60     <xsl:when test="substring($base.dir, string-length($base.dir), 1) = '/'">
61       <xsl:value-of select="$base.dir"/>
62     </xsl:when>
63     <xsl:otherwise>
64       <xsl:value-of select="concat($base.dir, '/')"/>
65     </xsl:otherwise>
66   </xsl:choose>
67   <xsl:value-of select="$epub.oebps.dir"/>
68   <xsl:if test="substring($epub.oebps.dir, string-length($epub.oebps.dir), 1) != '/'">
69     <xsl:text>/</xsl:text>
70   </xsl:if>
71 </xsl:variable>
72
73
74 <xsl:param name="index.links.to.section" select="0"/>
75
76 <!-- Epub does not yet support external links -->
77 <xsl:param name="activate.external.olinks" select="0"/>
78
79 <!-- Turning this on crashes ADE, which is unbelievably awesome -->
80 <xsl:param name="formal.object.break.after">0</xsl:param>
81
82 <xsl:param name="callout.graphics" select="1"/>
83 <xsl:param name="callout.graphics.extension">.png</xsl:param>
84 <xsl:param name="callout.graphics.number.limit" select="15"/>
85 <xsl:param name="callout.graphics.path" select="'images/callouts/'"/>
86 <xsl:param name="show.comments" select="0"/>
87
88 <!-- no navigation in .epub -->
89 <xsl:param name="suppress.navigation" select="'1'"/> 
90
91 <!-- EPUB3: use ol lists in table of contents -->
92 <xsl:param name="toc.list.type">ol</xsl:param>
93 <xsl:param name="autotoc.label.in.hyperlink" select="1"/>
94
95 <xsl:param name="css.decoration" select="1"/>
96 <!-- generate the css file from a source file -->
97 <xsl:param name="make.clean.html" select="1"/>
98 <!-- specify the default epub3 stylesheet -->
99 <xsl:param name="docbook.css.source">docbook-epub.css.xml</xsl:param>
100 <!-- for custom CSS, use the custom.css.source param -->
101 <xsl:param name="custom.css.source"></xsl:param>
102
103 <!--==============================================================-->
104 <!--  New EPUB3 Parameters                                        -->
105 <!--==============================================================-->
106 <xsl:param name="epub.version">3.0</xsl:param>
107 <!-- optional ncx for backwards compatibility -->
108 <xsl:param name="epub.include.ncx" select="1"/>
109 <xsl:param name="epub.ncx.depth">4</xsl:param> <!-- Not functional until http://code.google.com/p/epubcheck/issues/detail?id=70 is resolved -->
110 <!-- currently optional duplicate dcterms properties, may be required in future -->
111 <xsl:param name="epub.include.metadata.dcterms" select="1"/>
112 <!-- optional guide element for backwards compatibility -->
113 <xsl:param name="epub.include.guide" select="1"/>
114 <!-- some dc: currently required, to be replaced in future version -->
115 <xsl:param name="epub.include.metadata.dc.elements" select="1"/>
116 <!-- Some dc: elements will remain optional according to the spec -->
117 <xsl:param name="epub.include.optional.metadata.dc.elements" select="1"/>
118 <xsl:param name="epub.autolabel" select="0"/>
119 <xsl:param 
120   name="epub.vocabulary.profile.content">http://www.idpf.org/epub/30/profile/content/</xsl:param>
121 <xsl:param 
122   name="epub.vocabulary.profile.package">http://www.idpf.org/epub/30/profile/package/</xsl:param>
123 <xsl:param name="epub.output.epub.types" select="1"/>
124 <xsl:param name="epub.oebps.dir" select="'OEBPS'"/> 
125 <xsl:variable name="epub.metainf.dir" select="'META-INF/'"/> 
126 <xsl:param name="epub.ncx.filename" select="'toc.ncx'"/> 
127 <xsl:variable name="epub.mimetype.filename" select="'mimetype'"/> 
128 <xsl:variable name="epub.mimetype.value" select="'application/epub+zip'"/> 
129 <xsl:param name="epub.container.filename" select="'container.xml'"/> 
130 <xsl:param name="epub.package.filename" select="'package.opf'"/> 
131 <xsl:param name="epub.cover.filename" select="concat('cover', $html.ext)"/> 
132 <xsl:param name="epub.cover.linear" select="0" />
133
134 <!-- names of id attributes used in package files -->
135 <xsl:param name="epub.meta.identifier.id">meta-identifier</xsl:param> 
136 <xsl:param name="epub.dc.identifier.id">pub-identifier</xsl:param> 
137 <xsl:param name="epub.meta.title.id">meta-title</xsl:param> 
138 <xsl:param name="epub.dc.title.id">pub-title</xsl:param> 
139 <xsl:param name="epub.meta.language.id">meta-language</xsl:param> 
140 <xsl:param name="epub.dc.language.id">pub-language</xsl:param> 
141 <xsl:param name="epub.meta.creator.id">meta-creator</xsl:param> 
142 <xsl:param name="epub.dc.creator.id">pub-creator</xsl:param> 
143 <xsl:param name="epub.ncx.toc.id">ncxtoc</xsl:param>
144 <xsl:param name="epub.ncx.manifest.id">ncx</xsl:param>
145 <xsl:param name="epub.ncx.mediatype">application/x-dtbncx+xml</xsl:param>
146 <xsl:param name="epub.xhtml.mediatype">application/xhtml+xml</xsl:param>
147 <xsl:param name="epub.html.toc.id">htmltoc</xsl:param>
148 <xsl:param name="epub.cover.filename.id" select="'cover'"/> 
149 <xsl:param name="epub.cover.image.id" select="'cover-image'"/> 
150
151 <xsl:param name="epub.embedded.fonts"></xsl:param>
152 <xsl:param name="epub.namespace">http://www.idpf.org/2007/ops</xsl:param>
153 <xsl:param name="opf.namespace">http://www.idpf.org/2007/opf</xsl:param>
154 <xsl:param name="ncx.namespace">http://www.daisy.org/z3986/2005/ncx/</xsl:param>
155 <xsl:param name="dc.namespace">http://purl.org/dc/elements/1.1/</xsl:param>
156 <!-- prefix generated ids in package elements so they differ from content ids -->
157 <xsl:param name="epub.package.id.prefix">id-</xsl:param>
158 <!-- editor is either a creator or contributor -->
159 <xsl:param name="editor.property">contributor</xsl:param> 
160
161 <!-- Generate full output path -->
162 <xsl:param name="epub.package.dir" select="concat($chunk.base.dir, '../')"/>
163
164 <xsl:param name="epub.ncx.pathname" 
165            select="concat($chunk.base.dir, $epub.ncx.filename)"/>
166 <xsl:param name="epub.container.pathname"
167            select="concat($epub.package.dir, $epub.metainf.dir, 
168            $epub.container.filename)"/>
169 <xsl:param name="epub.package.pathname"
170            select="concat($chunk.base.dir, $epub.package.filename)"/>
171 <xsl:param name="epub.cover.pathname"
172            select="concat($chunk.base.dir, $epub.cover.filename)"/>
173 <xsl:param name="epub.mimetype.pathname"
174            select="concat($epub.package.dir, $epub.mimetype.filename)"/>
175
176 <xsl:param name="kindle.extensions" select="0"/>
177
178 <!--==============================================================-->
179 <!--  Internal variables used for computing certain metadata      -->
180 <!--==============================================================-->
181 <xsl:variable name="epub3.chunk.hierarchy">
182   <xsl:apply-templates select="/*" mode="find.chunks"/>
183 </xsl:variable>
184
185 <xsl:variable name="chunkset" select="exsl:node-set($epub3.chunk.hierarchy)//cf:div"/>
186
187 <!--==============================================================-->
188 <!--  Template customizations                                     -->
189 <!--==============================================================-->
190
191 <!-- This is used only by ncx piece -->
192 <xsl:variable name="root.is.a.chunk">
193   <xsl:choose>
194     <xsl:when test="/*[not(self::d:book)][not(d:sect1) or not(d:section)]">
195       <xsl:text>1</xsl:text>
196     </xsl:when>
197     <xsl:when test="/d:book[*[last()][self::d:bookinfo]]|d:book[d:bookinfo]">
198       <xsl:text>1</xsl:text>
199     </xsl:when>
200     <xsl:when test="/d:book[*[last()][self::d:info]]|d:book[d:info]">
201       <xsl:text>1</xsl:text>
202     </xsl:when>
203     <xsl:when test="/d:bibliography">
204       <xsl:text>1</xsl:text>
205     </xsl:when>
206     <xsl:otherwise>
207       <xsl:text>0</xsl:text>
208     </xsl:otherwise>
209   </xsl:choose>
210 </xsl:variable>
211
212 <!-- EPUB3: Add generation of epub publication files -->
213 <xsl:template match="*" mode="process.root">
214   <xsl:variable name="doc" select="self::*"/>
215
216   <xsl:call-template name="user.preroot"/>
217   <xsl:call-template name="root.messages"/>
218
219   <html>
220     <head>
221       <xsl:call-template name="system.head.content">
222         <xsl:with-param name="node" select="$doc"/>
223       </xsl:call-template>
224       <xsl:call-template name="head.content">
225         <xsl:with-param name="node" select="$doc"/>
226       </xsl:call-template>
227       <xsl:call-template name="user.head.content">
228         <xsl:with-param name="node" select="$doc"/>
229       </xsl:call-template>
230     </head>
231     <body>
232       <xsl:call-template name="body.attributes"/>
233       <xsl:call-template name="user.header.content">
234         <xsl:with-param name="node" select="$doc"/>
235       </xsl:call-template>
236       <xsl:apply-templates select="."/>
237       <xsl:call-template name="user.footer.content">
238         <xsl:with-param name="node" select="$doc"/>
239       </xsl:call-template>
240     </body>
241   </html>
242   <xsl:value-of select="$html.append"/>
243   
244   <!-- Generate any css files only once, not once per chunk -->
245   <xsl:call-template name="generate.css.files"/>
246
247   <xsl:call-template name="generate.epub.files"/>
248
249 </xsl:template>
250
251 <xsl:template name="generate.epub.files">
252   <!-- Generate epub3 files -->
253   <xsl:message>
254     <xsl:text>Generating EPUB package files.</xsl:text>
255   </xsl:message>
256
257   <xsl:apply-templates select="." mode="opf"/>
258   <xsl:apply-templates select="." mode="container"/>
259   <xsl:call-template name="mimetype"/>
260   <xsl:if test="$epub.include.ncx != 0">
261     <xsl:call-template name="ncx"/>
262   </xsl:if>
263 </xsl:template>
264
265 <!-- Generate the package file -->
266 <xsl:template match="*" mode="opf">
267
268   <xsl:variable name="lang">
269     <xsl:call-template name="l10n.language"/>
270   </xsl:variable>
271
272   <xsl:variable name="content">
273     <!-- use xsl:element so output does not have a namespace prefix -->
274     <xsl:element name="package" namespace="{$opf.namespace}">
275       <!-- Add the package namespaces at the top -->
276       <xsl:call-template name="add.package.namespaces"/>
277
278       <xsl:attribute name="version">
279         <xsl:value-of select="$epub.version"/>
280       </xsl:attribute>
281       <xsl:attribute name="xml:lang">
282         <xsl:value-of select="$lang"/>
283       </xsl:attribute>
284       <!-- No profile att in 2011-09-06 spec 
285       <xsl:attribute name="profile">
286         <xsl:value-of select="$epub.vocabulary.profile.package"/>
287       </xsl:attribute>
288       -->
289       <xsl:attribute name="unique-identifier">
290         <xsl:value-of select="$epub.dc.identifier.id"/>
291       </xsl:attribute>
292
293
294       <xsl:call-template name="package.metadata"/>
295       <xsl:call-template name="package.manifest"/>
296       <xsl:call-template name="package.spine"/>
297
298       <xsl:if test="$epub.include.guide != 0">
299         <xsl:call-template name="package.guide"/>
300       </xsl:if>
301
302     </xsl:element>
303   </xsl:variable>
304
305   <xsl:call-template name="write.chunk">
306     <xsl:with-param name="filename">
307       <xsl:value-of select="$epub.package.pathname" />
308     </xsl:with-param>
309     <xsl:with-param name="content" select="$content"/>
310     <xsl:with-param name="method" select="'xml'" />
311     <xsl:with-param name="encoding" select="'utf-8'" />
312     <xsl:with-param name="indent" select="'yes'" />
313     <xsl:with-param name="quiet" select="$chunk.quietly" />
314     <xsl:with-param name="doctype-public" select="''"/> <!-- intentionally blank -->
315     <xsl:with-param name="doctype-system" select="''"/> <!-- intentionally blank -->
316   </xsl:call-template>
317 </xsl:template>
318
319 <xsl:template name="package.metadata">
320
321   <xsl:element name="metadata" namespace="{$opf.namespace}">
322
323     <xsl:call-template name="metadata.identifier"/>
324     <xsl:call-template name="metadata.title"/>
325     <xsl:call-template name="metadata.language"/>
326     <xsl:call-template name="metadata.modified"/>
327     <xsl:call-template name="metadata.cover"/>
328     <xsl:call-template name="metadata.other.info"/>
329     
330   </xsl:element>
331 </xsl:template>
332
333 <xsl:template name="doc.title">
334   <xsl:apply-templates select="." mode="title.markup"/>
335 </xsl:template>
336
337 <xsl:template name="metadata.identifier">
338   <xsl:variable name="package.id.value">
339     <xsl:call-template name="package-identifier"/>
340   </xsl:variable>
341
342   <xsl:if test="$epub.include.metadata.dc.elements != 0">
343     <!-- dc:identifier element -->
344     <dc:identifier>
345       <xsl:attribute name="id">
346         <xsl:value-of select="$epub.dc.identifier.id"/>
347       </xsl:attribute>
348       <!--
349       <xsl:attribute name="prefer">
350         <xsl:value-of select="$epub.meta.identifier.id"/>
351       </xsl:attribute>
352       -->
353       <xsl:copy-of select="$package.id.value"/>
354     </dc:identifier>
355   </xsl:if>
356
357   <xsl:if test="$epub.include.metadata.dcterms != 0">
358     <!-- equivalent meta identifier element -->
359     <xsl:element name="meta" namespace="{$opf.namespace}">
360       <xsl:attribute name="id">
361         <xsl:value-of select="$epub.meta.identifier.id"/>
362       </xsl:attribute>
363       <xsl:attribute name="property">dcterms:identifier</xsl:attribute>
364       <xsl:copy-of select="$package.id.value"/>
365     </xsl:element>
366   </xsl:if>
367 </xsl:template>
368
369 <xsl:template name="metadata.title">
370   <xsl:variable name="doc.title">
371     <xsl:call-template name="doc.title"/>
372   </xsl:variable>
373
374   <xsl:if test="$epub.include.metadata.dc.elements != 0">
375     <dc:title>
376       <xsl:attribute name="id">
377         <xsl:value-of select="$epub.dc.title.id"/>
378       </xsl:attribute>
379       <!--
380       <xsl:attribute name="prefer">
381         <xsl:value-of select="$epub.meta.title.id"/>
382       </xsl:attribute>
383       -->
384       <xsl:value-of select="normalize-space($doc.title)"/>
385     </dc:title>
386   </xsl:if>
387
388   <xsl:if test="$epub.include.metadata.dcterms != 0">
389   <!-- equivalent meta title element -->
390     <xsl:element name="meta" namespace="{$opf.namespace}">
391       <xsl:attribute name="property">dcterms:title</xsl:attribute>
392       <xsl:attribute name="id">
393         <xsl:value-of select="$epub.meta.title.id"/>
394       </xsl:attribute>
395       <xsl:value-of select="normalize-space($doc.title)"/>
396     </xsl:element>
397   </xsl:if>
398 </xsl:template>
399
400 <xsl:template name="metadata.language">
401   <xsl:variable name="lang">
402     <xsl:call-template name="l10n.language"/>
403   </xsl:variable>
404
405   <xsl:if test="$epub.include.metadata.dc.elements != 0">
406     <dc:language>
407       <xsl:if test="$kindle.extensions = 0">
408         <xsl:attribute name="id">
409           <xsl:value-of select="$epub.dc.language.id"/>
410         </xsl:attribute>
411       </xsl:if>
412       <xsl:value-of select="$lang"/>
413     </dc:language>
414   </xsl:if>
415
416   <xsl:if test="$epub.include.metadata.dcterms != 0">
417   <!-- equivalent meta lang element -->
418     <xsl:element name="meta" namespace="{$opf.namespace}">
419       <xsl:attribute name="property">dcterms:language</xsl:attribute>
420       <xsl:attribute name="id">
421         <xsl:value-of select="$epub.meta.language.id"/>
422       </xsl:attribute>
423       <xsl:value-of select="$lang"/>
424     </xsl:element>
425   </xsl:if>
426 </xsl:template>
427   
428 <xsl:template name="metadata.modified">
429   <xsl:variable name="local.datetime" select="date:date-time()"/>
430   <xsl:variable name="utc.datetime">
431     <xsl:call-template name="convert.date.to.utc">
432       <xsl:with-param name="date" select="$local.datetime"/>
433     </xsl:call-template>
434   </xsl:variable>
435   <xsl:choose>
436     <xsl:when test="string-length($utc.datetime) != 0">
437       <xsl:element name="meta" namespace="{$opf.namespace}">
438         <xsl:attribute name="property">dcterms:modified</xsl:attribute>
439         <xsl:value-of select="$utc.datetime"/>
440       </xsl:element>
441       <xsl:comment>The preceding date value is actually local time (not UTC) in UTC format because there is no function in XSLT 1.0 to generate a correct UTC time</xsl:comment>
442     </xsl:when>
443     <xsl:otherwise>
444       <xsl:message terminate="yes">
445         <xsl:text>ERROR: no last-modified date value could be determined, </xsl:text>
446         <xsl:text>so cannot output required meta element with </xsl:text>
447         <xsl:text>dcterms:modified attribute. Exiting.</xsl:text>
448       </xsl:message>
449     </xsl:otherwise>
450   </xsl:choose>
451 </xsl:template>
452
453 <xsl:template name="convert.date.to.utc">
454   <xsl:param name="date" select="''"/>
455   <!-- input format is YYYY-MM-DDTHH:MM:SS-X:00
456      where -X:00 is the offset from UTC. -->
457
458   <!-- output format is YYYY-MM-DDTHH:MM:SSZ with no offset -->
459   <!-- FIX ME:  Not so easy without a proper UTC date function. -->
460   <!-- Currently it just converts the local time to this format, which is
461        not the correct UTC time. -->
462   <xsl:value-of select="concat(substring($date,1,19), 'Z')"/>
463 </xsl:template>
464
465 <!-- This cover meta element used by kindlegen, at least -->
466 <xsl:template name="metadata.cover">
467   <xsl:variable name="info" select="./*[contains(local-name(.), 'info')][1]"/>
468   <xsl:variable name="cover.image" 
469                 select="$info//d:mediaobject[@role='cover' or ancestor::d:cover]"/>
470
471   <xsl:if test="$cover.image">
472     <xsl:element name="meta" namespace="{$opf.namespace}">
473       <xsl:attribute name="content">
474         <xsl:value-of select="$epub.cover.image.id"/>
475       </xsl:attribute>
476       <xsl:attribute name="name">cover</xsl:attribute>
477     </xsl:element>
478   </xsl:if>
479 </xsl:template>
480
481 <xsl:template name="metadata.other.info">
482   <!-- Take info relative to selected root element -->
483   <xsl:variable name="info" select="./*[contains(local-name(.), 'info')][1]"/>
484
485   <xsl:apply-templates select="$info/*" mode="opf.metadata"/>        
486 </xsl:template>
487
488 <xsl:template match="*" mode="opf.metadata">
489   <!-- default is no output -->
490 </xsl:template>
491
492 <xsl:template match="d:authorgroup" mode="opf.metadata">
493   <xsl:apply-templates select="*" mode="opf.metadata"/>
494 </xsl:template>
495
496 <xsl:template match="d:author|d:corpauthor" mode="opf.metadata">
497   <xsl:variable name="n">
498     <xsl:choose>
499       <xsl:when test="self::d:corpauthor">
500         <xsl:apply-templates/>
501       </xsl:when>
502       <xsl:when test="d:org/d:orgname">
503         <xsl:apply-templates select="d:org/d:orgname"/>
504       </xsl:when>
505       <xsl:when test="d:orgname">
506         <xsl:apply-templates select="d:orgname"/>
507       </xsl:when>
508       <xsl:otherwise>
509         <xsl:call-template name="person.name">
510           <xsl:with-param name="node" select="."/>
511         </xsl:call-template>
512       </xsl:otherwise>
513     </xsl:choose>
514   </xsl:variable>
515
516   <xsl:if test="string-length($n) != 0">
517     <xsl:element name="meta" namespace="{$opf.namespace}">
518       <xsl:attribute name="id">
519         <xsl:value-of select="concat($epub.meta.creator.id, position())"/>
520       </xsl:attribute>
521       <xsl:attribute name="property">dcterms:creator</xsl:attribute>
522       <xsl:value-of select="normalize-space(string($n))"/>
523     </xsl:element>
524
525     <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
526       <dc:creator>
527         <xsl:attribute name="id">
528           <xsl:value-of select="concat($epub.dc.creator.id, position())"/>
529         </xsl:attribute>
530         <xsl:value-of select="$n"/>
531       </dc:creator>
532     </xsl:if>
533   </xsl:if>
534 </xsl:template>
535
536 <xsl:template match="d:editor" mode="opf.metadata">
537   <xsl:variable name="n">
538     <xsl:choose>
539       <xsl:when test="d:orgname">
540         <xsl:apply-templates select="d:orgname"/>
541       </xsl:when>
542       <xsl:otherwise>
543         <xsl:call-template name="person.name">
544           <xsl:with-param name="node" select="."/>
545         </xsl:call-template>
546       </xsl:otherwise>
547     </xsl:choose>
548   </xsl:variable>
549
550   <xsl:variable name="name">
551     <xsl:choose>
552       <xsl:when test="string-length($editor.property) != 0">
553         <xsl:value-of select="$editor.property"/>
554       </xsl:when>
555       <xsl:otherwise>
556         <xsl:text>contributor</xsl:text>
557       </xsl:otherwise>
558     </xsl:choose>
559   </xsl:variable>
560
561   <xsl:element name="meta" namespace="{$opf.namespace}">
562     <xsl:attribute name="property">
563       <xsl:text>dcterms:</xsl:text>
564       <xsl:value-of select="$name"/>
565     </xsl:attribute>
566     <xsl:value-of select="normalize-space($n)"/>
567   </xsl:element>
568
569   <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
570     <xsl:choose>
571       <xsl:when test="$name = 'creator'">
572         <dc:creator>
573           <xsl:value-of select="normalize-space($n)"/>
574         </dc:creator>
575       </xsl:when>
576       <xsl:when test="$name = 'contributor'">
577         <dc:contributor>
578           <xsl:value-of select="normalize-space($n)"/>
579         </dc:contributor>
580       </xsl:when>
581       <xsl:otherwise>
582         <xsl:element namespace="{$dc.namespace}" name="{$name}">
583           <xsl:value-of select="normalize-space($n)"/>
584         </xsl:element>
585       </xsl:otherwise>
586     </xsl:choose>
587   </xsl:if>
588
589 </xsl:template>
590
591 <xsl:template match="d:corpcredit" mode="opf.metadata">
592   <xsl:element name="meta" namespace="{$opf.namespace}">
593     <xsl:attribute name="property">dcterms:contributor</xsl:attribute>
594     <xsl:value-of select="normalize-space(.)"/>
595   </xsl:element>
596
597   <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
598     <dc:contributor>
599       <xsl:value-of select="normalize-space(.)"/>
600     </dc:contributor>
601   </xsl:if>
602 </xsl:template>
603
604 <xsl:template match="d:collab|d:othercredit" mode="opf.metadata">
605   <xsl:variable name="content">
606     <xsl:choose>
607       <xsl:when test="d:collabname">
608         <xsl:apply-templates select="d:collabname"/>
609       </xsl:when>
610       <xsl:when test="d:org/d:orgname">
611         <xsl:apply-templates select="d:org/d:orgname"/>
612       </xsl:when>
613       <xsl:when test="d:orgname">
614         <xsl:apply-templates select="d:orgname"/>
615       </xsl:when>
616       <xsl:when test="d:personname|d:firstname|d:surname|d:othername">
617         <xsl:call-template name="person.name"/>
618       </xsl:when>
619       <xsl:otherwise>
620         <xsl:value-of select="."/>
621       </xsl:otherwise>
622     </xsl:choose>
623   </xsl:variable>
624
625   <xsl:element name="meta" namespace="{$opf.namespace}">
626     <xsl:attribute name="property">dcterms:contributor</xsl:attribute>
627     <xsl:value-of select="normalize-space($content)"/>
628   </xsl:element>
629
630   <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
631     <dc:contributor>
632       <xsl:value-of select="normalize-space($content)"/>
633     </dc:contributor>
634   </xsl:if>
635
636 </xsl:template>
637
638 <xsl:template match="d:date|d:pubdate" mode="opf.metadata">
639   <xsl:variable name="date">
640     <xsl:call-template name="format.meta.date">
641       <xsl:with-param name="string" select="normalize-space(.)"/>
642     </xsl:call-template>
643   </xsl:variable>
644
645   <xsl:if test="string-length($date) != 0">
646     <!-- Can only output one date for epub, pubdate has priority -->
647     <xsl:if test="self::d:pubdate or (self::d:date and not(../d:pubdate) )">
648       <xsl:element name="meta" namespace="{$opf.namespace}">
649         <xsl:attribute name="property">dcterms:date</xsl:attribute>
650         <xsl:value-of select="$date"/>
651       </xsl:element>
652     
653       <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
654         <dc:date>
655           <xsl:value-of select="$date"/>
656         </dc:date>
657       </xsl:if>
658     </xsl:if>
659   </xsl:if>
660
661 </xsl:template>
662
663 <!-- EPUB3 meta date should be of the form:
664   YYYY, YYYY-MM or YYYY-MM-DD -->
665 <xsl:template name="format.meta.date">
666   <xsl:param name="string" select="''"/>
667   <xsl:param name="node" select="."/>
668   
669   <!-- FIXME: this needs further work, so just check the
670   string format and return the date string for now -->
671   <xsl:variable name="normalized" 
672                 select="translate($string, '0123456789', '##########')"/>
673
674   <xsl:variable name="date.ok">
675     <xsl:choose>
676       <xsl:when test="string-length($string) = 4 and
677                       $normalized = '####'">1</xsl:when>
678       <xsl:when test="string-length($string) = 7 and
679                       $normalized = '####-##'">1</xsl:when>
680       <xsl:when test="string-length($string) = 10 and
681                       $normalized = '####-##-##'">1</xsl:when>
682       <xsl:when test="string-length($string) = 10 and
683                       $normalized = '####-##-##'">1</xsl:when>
684       <xsl:otherwise>0</xsl:otherwise>
685     </xsl:choose>
686   </xsl:variable>
687
688   <xsl:if test="$date.ok = 0">
689     <xsl:message>
690       <xsl:text>WARNING: wrong metadata date format: '</xsl:text>
691       <xsl:value-of select="$string"/>
692       <xsl:text>' in element </xsl:text>
693       <xsl:value-of select="local-name($node/..)"/>
694       <xsl:text>/</xsl:text>
695       <xsl:value-of select="local-name($node)"/>
696       <xsl:text>. It must be in one of these forms: </xsl:text>
697       <xsl:text>YYYY, YYYY-MM, or YYYY-MM-DD.</xsl:text>
698     </xsl:message>
699   </xsl:if>
700
701   <!-- return the string anyway -->
702   <xsl:value-of select="$string"/>
703
704 </xsl:template>
705
706
707 <!-- Space separate the compontents of the abstract (dropping the inline markup, sadly) -->
708 <xsl:template match="d:abstract" mode="opf.metadata">
709   <xsl:variable name="content">
710     <xsl:for-each select="d:formalpara|d:para|d:simpara|d:title">
711       <xsl:choose>
712         <xsl:when test="self::d:formalpara">
713           <xsl:value-of select="normalize-space(string(d:title))"/>
714           <xsl:text>: </xsl:text>
715           <xsl:value-of select="normalize-space(string(d:para))"/>
716         </xsl:when>
717         <xsl:otherwise>
718           <xsl:value-of select="normalize-space(string(.))"/>
719         </xsl:otherwise>  
720       </xsl:choose>
721       <xsl:if test="self::d:title">
722         <xsl:text>:</xsl:text>
723       </xsl:if>
724       <xsl:if test="not(position() = last())">
725         <xsl:text> </xsl:text>
726       </xsl:if>
727     </xsl:for-each>  
728   </xsl:variable>
729
730   <xsl:element name="meta" namespace="{$opf.namespace}">
731     <xsl:attribute name="property">dcterms:description</xsl:attribute>
732     <xsl:copy-of select="$content"/>
733   </xsl:element>
734
735   <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
736     <dc:description>
737       <xsl:copy-of select="$content"/>
738     </dc:description>
739   </xsl:if>
740 </xsl:template>
741
742 <xsl:template match="d:subjectset" mode="opf.metadata">
743   <xsl:apply-templates select="d:subject/d:subjectterm" mode="opf.metadata"/>
744 </xsl:template>
745
746 <xsl:template match="d:subjectterm" mode="opf.metadata">
747   <xsl:element name="meta" namespace="{$opf.namespace}">
748     <xsl:attribute name="property">dcterms:subject</xsl:attribute>
749     <xsl:value-of select="normalize-space(string(.))"/>
750   </xsl:element>
751
752   <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
753     <dc:subject>
754       <xsl:value-of select="normalize-space(string(.))"/>
755     </dc:subject>
756   </xsl:if>
757 </xsl:template>
758
759 <xsl:template match="d:keywordset" mode="opf.metadata">
760   <xsl:apply-templates select="d:keyword" mode="opf.metadata"/>
761 </xsl:template>
762
763 <xsl:template match="d:keyword" mode="opf.metadata">
764   <xsl:element name="meta" namespace="{$opf.namespace}">
765     <xsl:attribute name="property">dcterms:subject</xsl:attribute>
766     <xsl:value-of select="normalize-space(string(.))"/>
767   </xsl:element>
768
769   <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
770     <dc:subject>
771       <xsl:value-of select="normalize-space(string(.))"/>
772     </dc:subject>
773   </xsl:if>
774 </xsl:template>
775
776 <xsl:template match="d:publisher" mode="opf.metadata">
777   <xsl:apply-templates select="d:publishername" mode="opf.metadata"/>
778 </xsl:template>
779
780 <xsl:template match="d:publishername" mode="opf.metadata">
781   <xsl:element name="meta" namespace="{$opf.namespace}">
782     <xsl:attribute name="property">dcterms:publisher</xsl:attribute>
783     <xsl:value-of select="normalize-space(string(.))"/>
784   </xsl:element>
785
786   <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
787     <dc:publisher>
788       <xsl:value-of select="normalize-space(string(.))"/>
789     </dc:publisher>
790   </xsl:if>
791 </xsl:template>
792
793 <xsl:template match="d:bibliocoverage" mode="opf.metadata">
794   <xsl:element name="meta" namespace="{$opf.namespace}">
795     <xsl:attribute name="property">dcterms:coverage</xsl:attribute>
796     <xsl:value-of select="normalize-space(string(.))"/>
797   </xsl:element>
798
799   <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
800     <dc:coverage>
801       <xsl:value-of select="normalize-space(string(.))"/>
802     </dc:coverage>
803   </xsl:if>
804 </xsl:template>
805
806 <xsl:template match="d:bibliorelation" mode="opf.metadata">
807   <xsl:element name="meta" namespace="{$opf.namespace}">
808     <xsl:attribute name="property">dcterms:relation</xsl:attribute>
809     <xsl:value-of select="normalize-space(string(.))"/>
810   </xsl:element>
811
812   <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
813     <dc:relation>
814       <xsl:value-of select="normalize-space(string(.))"/>
815     </dc:relation>
816   </xsl:if>
817 </xsl:template>
818
819 <xsl:template match="d:bibliosource" mode="opf.metadata">
820   <xsl:element name="meta" namespace="{$opf.namespace}">
821     <xsl:attribute name="property">dcterms:source</xsl:attribute>
822     <xsl:value-of select="normalize-space(string(.))"/>
823   </xsl:element>
824
825   <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
826     <dc:source>
827       <xsl:value-of select="normalize-space(string(.))"/>
828     </dc:source>
829   </xsl:if>
830 </xsl:template>
831
832 <xsl:template match="d:copyright" mode="opf.metadata">
833   <xsl:variable name="copyright.date">
834     <xsl:call-template name="copyright.years">
835       <xsl:with-param name="years" select="d:year"/>
836       <xsl:with-param name="print.ranges" select="$make.year.ranges"/>
837       <xsl:with-param name="single.year.ranges" select="$make.single.year.ranges"/>
838     </xsl:call-template>
839   </xsl:variable>
840
841   <!-- if no docbook date element, use copyright year for single date metadata -->
842   <xsl:if test="not(../d:date) and not(../d:pubdate)">
843     <xsl:variable name="date.content">
844       <xsl:call-template name="format.meta.date">
845         <xsl:with-param name="string">
846           <xsl:call-template name="copyright.years">
847             <xsl:with-param name="years" select="d:year[last()]"/>
848             <xsl:with-param name="print.ranges" select="0"/>
849             <xsl:with-param name="single.year.ranges" select="0"/>
850           </xsl:call-template>
851         </xsl:with-param>
852       </xsl:call-template>
853     </xsl:variable>
854     <xsl:element name="meta" namespace="{$opf.namespace}">
855       <xsl:attribute name="property">dcterms:date</xsl:attribute>
856       <xsl:copy-of select="$date.content"/>
857     </xsl:element>
858     <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
859       <dc:date>
860         <xsl:copy-of select="$date.content"/>
861       </dc:date>
862     </xsl:if>
863   </xsl:if>
864
865   <xsl:variable name="rights.content">
866     <xsl:call-template name="gentext">
867       <xsl:with-param name="key" select="'Copyright'"/>
868     </xsl:call-template>
869     <xsl:call-template name="gentext.space"/>
870     <xsl:text>&#x00A9;</xsl:text>
871     <xsl:call-template name="gentext.space"/>
872     <xsl:value-of select="$copyright.date"/>
873     <xsl:call-template name="gentext.space"/>
874     <xsl:apply-templates select="d:holder" mode="titlepage.mode"/>
875   </xsl:variable>
876
877   <xsl:element name="meta" namespace="{$opf.namespace}">
878     <xsl:attribute name="property">dcterms:rights</xsl:attribute>
879     <xsl:copy-of select="$rights.content"/>
880   </xsl:element>
881   <xsl:if test="$epub.include.optional.metadata.dc.elements != 0">
882     <dc:rights>
883       <xsl:copy-of select="$rights.content"/>
884     </dc:rights>
885   </xsl:if>
886
887   <xsl:element name="meta" namespace="{$opf.namespace}">
888     <xsl:attribute name="property">dcterms:rightsHolder</xsl:attribute>
889     <xsl:apply-templates select="d:holder" mode="titlepage.mode"/>
890   </xsl:element>
891 </xsl:template>
892
893 <xsl:template name="package.guide">
894
895   <xsl:variable name="info" select="./*[contains(local-name(.), 'info')][1]"/>
896
897   <xsl:variable name="toc.params">
898     <xsl:call-template name="find.path.params">
899       <xsl:with-param name="node" select="."/>
900       <xsl:with-param name="table" select="normalize-space($generate.toc)"/>
901     </xsl:call-template>
902   </xsl:variable>
903
904   <xsl:if test="contains($toc.params, 'toc') or 
905                 $info/d:cover or
906                 $info//d:mediaobject[@role='cover' or ancestor::d:cover]">
907     <xsl:element namespace="{$opf.namespace}" name="guide">
908       <xsl:if test="$info/d:cover or
909                     $info//d:mediaobject[@role='cover' or ancestor::d:cover]">
910         <xsl:element namespace="{$opf.namespace}" name="reference">
911           <xsl:attribute name="href">
912             <xsl:value-of select="$epub.cover.filename" />
913           </xsl:attribute>
914           <xsl:attribute name="type">cover</xsl:attribute>
915           <xsl:attribute name="title">Cover</xsl:attribute>
916         </xsl:element>
917       </xsl:if>  
918
919       <xsl:if test="contains($toc.params, 'toc')">
920         <xsl:element namespace="{$opf.namespace}" name="reference">
921           <xsl:attribute name="href">
922             <xsl:call-template name="toc-href">
923               <xsl:with-param name="node" select="."/>
924             </xsl:call-template>
925           </xsl:attribute>
926           <xsl:attribute name="type">toc</xsl:attribute>
927           <xsl:attribute name="title">Table of Contents</xsl:attribute>
928         </xsl:element>
929       </xsl:if>  
930     </xsl:element>  
931   </xsl:if>  
932 </xsl:template>
933
934
935 <xsl:template name="package-identifier">  
936
937   <xsl:variable name="info" select="./*[contains(local-name(.), 'info')][1]"/>
938
939   <xsl:choose>
940     <xsl:when test="$info/d:biblioid">
941       <xsl:if test="$info/d:biblioid[1][@class = 'doi' or
942                                         @class = 'isbn' or
943                                         @class = 'isrn' or
944                                         @class = 'istc' or
945                                         @class = 'issn']">
946         <xsl:text>urn:</xsl:text>
947         <xsl:value-of select="$info/d:biblioid[1]/@class"/>
948         <xsl:text>:</xsl:text>
949       </xsl:if>
950       <xsl:value-of select="normalize-space($info/d:biblioid[1])"/>
951     </xsl:when>
952     <xsl:when test="$info/d:isbn">
953       <xsl:text>urn:isbn:</xsl:text>
954       <xsl:value-of select="$info/d:isbn[1]"/>
955     </xsl:when>
956     <xsl:when test="$info/d:issn">
957       <xsl:text>urn:issn:</xsl:text>
958       <xsl:value-of select="$info/d:issn[1]"/>
959     </xsl:when>
960     <xsl:otherwise>
961       <xsl:choose>
962         <xsl:when test="$info/d:invpartnumber">
963           <xsl:value-of select="$info/d:invpartnumber[1]"/>
964         </xsl:when>
965         <xsl:when test="$info/d:issuenum">
966           <xsl:value-of select="$info/d:issuenum[1]"/>
967         </xsl:when>
968         <xsl:when test="$info/d:productnumber">
969           <xsl:value-of select="$info/d:productnumber[1]"/>
970         </xsl:when>
971         <xsl:when test="$info/d:seriesvolnums">
972           <xsl:value-of select="$info/d:seriesvolnums[1]"/>
973         </xsl:when>
974         <xsl:when test="$info/d:volumenum">
975           <xsl:value-of select="$info/d:volumenum[1]"/>
976         </xsl:when>
977         <!-- Deprecated -->
978         <xsl:when test="$info/d:pubsnumber">
979           <xsl:value-of select="$info/d:pubsnumber[1]"/>
980         </xsl:when>
981       </xsl:choose>  
982       <xsl:text>_</xsl:text>
983       <xsl:choose>
984         <xsl:when test="@id">
985           <xsl:value-of select="@id"/>
986         </xsl:when>
987         <xsl:when test="@xml:id">
988           <xsl:value-of select="@xml:id"/>
989         </xsl:when>
990         <xsl:otherwise>
991           <!-- TODO: Do UUIDs here -->
992           <xsl:value-of select="generate-id(.)"/>
993         </xsl:otherwise>
994       </xsl:choose>
995     </xsl:otherwise>
996   </xsl:choose>
997 </xsl:template>
998
999 <!-- EPUB3: add epub:type attribute where appropriate -->
1000 <xsl:template match="*" mode="common.html.attributes">
1001   <xsl:param name="class" select="local-name(.)"/>
1002   <xsl:param name="inherit" select="0"/>
1003   <xsl:call-template name="generate.html.lang"/>
1004   <xsl:call-template name="dir">
1005     <xsl:with-param name="inherit" select="$inherit"/>
1006   </xsl:call-template>
1007   <xsl:apply-templates select="." mode="class.attribute">
1008     <xsl:with-param name="class" select="$class"/>
1009   </xsl:apply-templates>
1010   <xsl:call-template name="generate.html.title"/>
1011   <xsl:apply-templates select="." mode="epub.type"/>
1012 </xsl:template>
1013
1014 <xsl:template match="*" mode="epub.type" priority="-1"/>
1015
1016 <xsl:template match="d:chapter
1017                     |d:appendix
1018                     |d:epigraph
1019                     |d:warning
1020                     |d:preface
1021                     |d:index
1022                     |d:colophon
1023                     |d:glossary
1024                     |d:biblioentry
1025                     |d:bibliography
1026                     |d:dedication
1027                     |d:sidebar
1028                     |d:footnote
1029                     |d:glossterm
1030                     |d:glossdef
1031                     |d:bridgehead
1032                     |d:part" mode="epub.type">
1033   <xsl:variable name="type" select="local-name()"/>
1034
1035   <xsl:if test="$epub.output.epub.types != 0">
1036     <xsl:attribute name="epub:type">
1037       <xsl:value-of select="$type"/>
1038     </xsl:attribute>
1039   </xsl:if>
1040 </xsl:template>
1041
1042 <xsl:template match="d:section[parent::d:chapter] | d:sect1" mode="epub.type">
1043   <xsl:if test="$epub.output.epub.types != 0">
1044     <xsl:attribute name="epub:type">subchapter</xsl:attribute>
1045   </xsl:if>
1046 </xsl:template>
1047
1048 <xsl:template match="d:section[not(parent::d:chapter)] |
1049                      d:sect2 |
1050                      d:sect3 |
1051                      d:sect4 |
1052                      d:sect5 |
1053                      d:sect6" mode="epub.type">
1054   <xsl:if test="$epub.output.epub.types != 0">
1055     <xsl:attribute name="epub:type">division</xsl:attribute>
1056   </xsl:if>
1057 </xsl:template>
1058
1059 <xsl:template match="d:note|d:tip|d:caution|d:important" mode="epub.type">
1060   <xsl:if test="$epub.output.epub.types != 0">
1061     <xsl:attribute name="epub:type">notice</xsl:attribute>
1062   </xsl:if>
1063 </xsl:template>
1064
1065 <xsl:template match="d:orderedlist|d:itemizedlist|d:variablelist|d:simplelist" mode="epub.type">
1066   <xsl:if test="$epub.output.epub.types != 0">
1067     <xsl:attribute name="epub:type">list</xsl:attribute>
1068   </xsl:if>
1069 </xsl:template>
1070
1071 <xsl:template match="d:listitem" mode="epub.type">
1072   <xsl:if test="$epub.output.epub.types != 0">
1073     <xsl:attribute name="epub:type">list-item</xsl:attribute>
1074   </xsl:if>
1075 </xsl:template>
1076
1077 <!-- EPUB3: to add attributes to root output element -->
1078 <xsl:template name="root.attributes">
1079   <!-- collect and output all namespace declarations -->
1080   <xsl:call-template name="all.namespaces"/>
1081 </xsl:template>
1082
1083 <xsl:template name="all.namespaces">
1084   <!-- add the epub3 namespaces to the top output element -->
1085   <xsl:variable name="temp">
1086     <epub:foo/>
1087     <m:foo/>
1088     <pls:foo/>
1089     <ssml:foo/>
1090     <svg:foo/>
1091   </xsl:variable>
1092
1093   <xsl:variable name="nodes" select="exsl:node-set($temp)"/>
1094   <xsl:for-each select="$nodes//*/namespace::*">
1095     <xsl:copy-of select="."/>
1096   </xsl:for-each>
1097
1098 </xsl:template>
1099
1100 <xsl:template name="add.package.namespaces">
1101   <!-- add the epub3 package namespaces to the top output element -->
1102   <xsl:variable name="temp">
1103     <dc:foo/>
1104     <dcterms:foo/>
1105   </xsl:variable>
1106
1107   <xsl:variable name="nodes" select="exsl:node-set($temp)"/>
1108   <xsl:for-each select="$nodes//*[local-name(.) ='foo']/namespace::*">
1109     <xsl:if test="contains(., 'purl.org')">
1110       <xsl:copy-of select="."/>
1111     </xsl:if>
1112   </xsl:for-each>
1113
1114 </xsl:template>
1115
1116
1117 <xsl:template name="footnotes.attributes">
1118   <xsl:if test="$epub.output.epub.types != 0">
1119     <xsl:attribute name="epub:type">footnotes</xsl:attribute>
1120   </xsl:if>
1121 </xsl:template>
1122
1123 <xsl:template name="package.manifest">
1124   <xsl:element name="manifest" namespace="{$opf.namespace}">
1125     <xsl:if test="$epub.include.ncx != 0">
1126       <xsl:call-template name="manifest.ncx"/>
1127     </xsl:if>
1128     <xsl:call-template name="manifest.fonts"/>
1129     <xsl:call-template name="manifest.toc"/>
1130     <xsl:call-template name="manifest.css"/>
1131     <xsl:call-template name="manifest.cover"/>
1132     <xsl:call-template name="manifest.other.items"/>
1133     <xsl:call-template name="manifest.content.items"/>
1134     <xsl:call-template name="user.manifest.items"/>
1135   </xsl:element>
1136 </xsl:template>
1137
1138 <xsl:template name="user.manifest.items"/>
1139
1140 <xsl:template name="manifest.css">
1141   <xsl:if test="$html.stylesheet != ''">
1142     <xsl:call-template name="css.item">
1143       <xsl:with-param name="stylesheets" select="$html.stylesheet"/>
1144     </xsl:call-template>
1145   </xsl:if>
1146   <xsl:if test="string-length($docbook.css.source) != 0">
1147     <xsl:variable name="dfilename">
1148       <xsl:call-template name="css.output.filename">
1149         <xsl:with-param name="src" select="$docbook.css.source"/>
1150       </xsl:call-template>
1151     </xsl:variable>
1152     <xsl:element namespace="{$opf.namespace}" name="item">
1153       <xsl:attribute name="media-type">text/css</xsl:attribute>
1154       <xsl:attribute name="id">docbook-css</xsl:attribute>
1155       <xsl:attribute name="href">
1156         <xsl:value-of select="$dfilename"/>
1157       </xsl:attribute>
1158     </xsl:element>
1159   </xsl:if>
1160   <xsl:if test="string-length($custom.css.source) != 0">
1161     <xsl:variable name="cfilename">
1162       <xsl:call-template name="css.output.filename">
1163         <xsl:with-param name="src" select="$custom.css.source"/>
1164       </xsl:call-template>
1165     </xsl:variable>
1166     <xsl:element namespace="{$opf.namespace}" name="item">
1167       <xsl:attribute name="media-type">text/css</xsl:attribute>
1168       <xsl:attribute name="id">custom-css</xsl:attribute>
1169       <xsl:attribute name="href">
1170         <xsl:value-of select="$cfilename"/>
1171       </xsl:attribute>
1172     </xsl:element>
1173   </xsl:if>
1174 </xsl:template>
1175
1176 <xsl:template name="css.item">
1177   <xsl:param name="stylesheets" select="''"/>
1178   <xsl:param name="count" select="1"/>
1179
1180   <xsl:choose>
1181     <xsl:when test="contains($stylesheets, ' ')">
1182       <xsl:variable name="css.filename" select="substring-before($stylesheets, ' ')"/>
1183       <xsl:if test="$css.filename != ''">
1184         <xsl:element namespace="{$opf.namespace}" name="item">
1185           <xsl:attribute name="media-type">text/css</xsl:attribute>
1186           <xsl:attribute name="id">
1187             <xsl:text>html-css</xsl:text>
1188             <xsl:if test="$count &gt; 1">
1189               <xsl:value-of select="$count"/>
1190             </xsl:if>
1191           </xsl:attribute>
1192           <xsl:attribute name="href">
1193             <xsl:value-of select="$css.filename"/>
1194           </xsl:attribute>
1195         </xsl:element>
1196       </xsl:if>
1197
1198       <xsl:call-template name="css.item">
1199         <xsl:with-param name="stylesheets" select="substring-after($stylesheets, ' ')"/>
1200         <xsl:with-param name="count" select="$count + 1"/>
1201       </xsl:call-template>
1202     </xsl:when>
1203     <xsl:otherwise>
1204       <xsl:if test="$stylesheets != ''">
1205         <xsl:element namespace="{$opf.namespace}" name="item">
1206           <xsl:attribute name="media-type">text/css</xsl:attribute>
1207           <xsl:attribute name="id">
1208             <xsl:text>html-css</xsl:text>
1209             <xsl:if test="$count &gt; 1">
1210               <xsl:value-of select="$count"/>
1211             </xsl:if>
1212           </xsl:attribute>
1213           <xsl:attribute name="href">
1214             <xsl:value-of select="$stylesheets"/>
1215           </xsl:attribute>
1216         </xsl:element>
1217       </xsl:if>
1218     </xsl:otherwise>
1219   </xsl:choose>
1220 </xsl:template>
1221
1222 <xsl:template name="manifest.ncx">
1223   <xsl:element name="item" namespace="{$opf.namespace}">
1224     <xsl:attribute name="id">
1225       <xsl:value-of select="$epub.ncx.manifest.id"/>
1226     </xsl:attribute>
1227     <xsl:attribute name="href">
1228       <xsl:value-of select="$epub.ncx.filename"/>
1229     </xsl:attribute>
1230     <xsl:attribute name="media-type">
1231       <xsl:value-of select="$epub.ncx.mediatype"/>
1232     </xsl:attribute>
1233   </xsl:element>
1234 </xsl:template>
1235
1236 <xsl:template name="manifest.fonts">
1237   <xsl:param name="font.list" select="$epub.embedded.fonts"/>
1238   <xsl:param name="count" select="0"/>
1239
1240   <xsl:choose>
1241     <xsl:when test="$font.list != '' and
1242             not(contains($font.list, ','))">
1243       <xsl:call-template name="embedded-font-item">
1244         <xsl:with-param name="font.file" select="normalize-space($font.list)"/>
1245         <xsl:with-param name="font.order" select="$count + 1"/>
1246       </xsl:call-template>
1247     </xsl:when>
1248     <xsl:when test="$font.list != '' and
1249            contains($font.list, ',')">
1250       <xsl:variable name="this.font" 
1251                     select="substring-before($font.list, ',')"/>
1252       <xsl:variable name="rest"
1253                     select="substring-after($font.list, ',')"/>
1254
1255       <xsl:call-template name="embedded-font-item">
1256         <xsl:with-param name="font.file" select="normalize-space($this.font)"/>
1257         <xsl:with-param name="font.order" select="$count + 1"/>
1258       </xsl:call-template>
1259
1260       <!-- recurse to process the rest -->
1261       <xsl:call-template name="manifest.fonts">
1262         <xsl:with-param name="font.list" select="$rest"/>
1263         <xsl:with-param name="count" select="$count + 1"/>
1264       </xsl:call-template>
1265     </xsl:when>
1266   </xsl:choose>
1267
1268 </xsl:template>
1269
1270
1271 <xsl:template name="embedded-font-item">
1272   <xsl:param name="font.file"/>
1273   <xsl:param name="font.order" select="1"/>
1274
1275   <xsl:element namespace="http://www.idpf.org/2007/opf" name="item">
1276     <xsl:attribute name="id">
1277       <xsl:value-of select="concat('epub.embedded.font.', $font.order)"/>
1278     </xsl:attribute>
1279     <xsl:attribute name="href">
1280       <xsl:value-of select="$font.file"/>
1281     </xsl:attribute>
1282     <xsl:choose>
1283       <xsl:when test="contains($font.file, '.otf')">
1284         <xsl:attribute name="media-type">application/vnd.ms-opentype</xsl:attribute>
1285       </xsl:when>
1286       <xsl:when test="contains($font.file, '.woff')">
1287         <xsl:attribute name="media-type">application/font-woff</xsl:attribute>
1288       </xsl:when>
1289       <xsl:otherwise>
1290         <xsl:message>
1291           <xsl:text>WARNING: embedded fonts should be OpenType or WOFF!  (</xsl:text>
1292           <xsl:value-of select="$font.file"/>
1293           <xsl:text>)</xsl:text>
1294         </xsl:message>
1295       </xsl:otherwise>
1296     </xsl:choose>
1297   </xsl:element>
1298 </xsl:template>
1299
1300 <!--Misc items in the manifest based on content -->
1301 <xsl:template name="manifest.other.items">
1302 </xsl:template>
1303
1304
1305 <xsl:template name="manifest.cover">
1306   <xsl:variable name="info" select="./*[contains(local-name(.), 'info')][1]"/>
1307   <xsl:variable name="cover.image" 
1308                 select="$info//d:mediaobject[@role='cover' or ancestor::d:cover]"/>
1309
1310   <xsl:if test="$cover.image">
1311
1312     <!-- generate the manifest link to that page -->
1313     <xsl:variable name="olist" select="$cover.image/d:imageobject|$cover.image/d:imageobjectco
1314                        |$cover.image/d:videoobject|$cover.image/d:audioobject
1315                        |$cover.image/d:textobject"/>
1316   
1317     <xsl:variable name="object.index">
1318       <xsl:call-template name="select.mediaobject.index">
1319         <xsl:with-param name="olist" select="$olist"/>
1320         <xsl:with-param name="count" select="1"/>
1321       </xsl:call-template>
1322     </xsl:variable>
1323   
1324     <xsl:variable name="object" select="$olist[position() = $object.index]"/>
1325
1326     <xsl:variable name="output_filename">
1327       <xsl:call-template name="mediaobject.filename">
1328         <xsl:with-param name="object" select="$object"/>
1329       </xsl:call-template>
1330     </xsl:variable>
1331
1332     <xsl:variable name="image.filename">
1333        <xsl:if test="$img.src.path != '' and
1334                        not(starts-with($output_filename, '/')) and
1335                        not(contains($output_filename, '://'))">
1336          <xsl:value-of select="$img.src.path"/>
1337        </xsl:if>
1338        <xsl:value-of select="$output_filename"/>
1339     </xsl:variable>
1340     <xsl:variable name="image.extension">
1341       <xsl:call-template name="filename-extension">
1342         <xsl:with-param name="filename" select="$image.filename"/>
1343       </xsl:call-template>
1344     </xsl:variable>
1345
1346     <xsl:variable name="image.type">
1347       <xsl:call-template name="graphic.format.content-type">
1348         <xsl:with-param name="format" select="translate($image.extension, 
1349                    &lowercase;, &uppercase;)"/>
1350       </xsl:call-template>
1351     </xsl:variable>
1352
1353     <!-- Reference to cover html file -->
1354     <xsl:element namespace="{$opf.namespace}" name="item">
1355       <xsl:attribute name="id">
1356         <xsl:value-of select="$epub.cover.filename.id"/>
1357       </xsl:attribute>
1358       <xsl:attribute name="href">
1359         <xsl:value-of select="$epub.cover.filename"/>
1360       </xsl:attribute>
1361       <xsl:attribute name="media-type">
1362         <xsl:value-of select="$epub.xhtml.mediatype"/>
1363       </xsl:attribute>
1364     </xsl:element>
1365
1366     <!-- special item with property="cover-image" -->
1367     <xsl:element namespace="{$opf.namespace}" name="item">
1368       <xsl:attribute name="id">
1369         <xsl:value-of select="$epub.cover.image.id"/>
1370       </xsl:attribute>
1371       <xsl:attribute name="properties">cover-image</xsl:attribute>
1372       <xsl:attribute name="href">
1373         <xsl:value-of select="$image.filename"/>
1374       </xsl:attribute>
1375       <xsl:if test="$image.type">
1376         <xsl:attribute name="media-type">
1377           <xsl:value-of select="$image.type"/>
1378         </xsl:attribute>
1379       </xsl:if>
1380     </xsl:element>
1381
1382     <!-- And generate the cover html file -->
1383     <xsl:apply-templates select="$cover.image"/>
1384
1385   </xsl:if>
1386 </xsl:template>
1387
1388 <xsl:template name="manifest.toc">
1389   <xsl:variable name="toc.params">
1390     <xsl:call-template name="find.path.params">
1391       <xsl:with-param name="node" select="."/>
1392       <xsl:with-param name="table" select="normalize-space($generate.toc)"/>
1393     </xsl:call-template>
1394   </xsl:variable>
1395
1396   <xsl:if test="contains($toc.params, 'toc')">
1397     <xsl:element namespace="{$opf.namespace}" name="item">
1398       <xsl:attribute name="id">
1399         <xsl:value-of select="$epub.html.toc.id"/>
1400       </xsl:attribute>
1401       <xsl:attribute name="properties">nav</xsl:attribute>
1402       <xsl:attribute name="media-type">
1403         <xsl:value-of select="$epub.xhtml.mediatype"/>
1404       </xsl:attribute>
1405       <xsl:attribute name="href">
1406         <xsl:call-template name="toc-href">
1407           <xsl:with-param name="node" select="."/>
1408         </xsl:call-template>
1409       </xsl:attribute>
1410     </xsl:element>
1411   </xsl:if>  
1412 </xsl:template>
1413
1414 <xsl:template name="toc-href">
1415   <xsl:param name="node" select="."/>
1416   <!-- FIXME -->
1417   <xsl:apply-templates select="$node" mode="recursive-chunk-filename">
1418     <xsl:with-param name="recursive" select="true()"/>
1419   </xsl:apply-templates>
1420   <xsl:text>-toc</xsl:text>
1421   <xsl:value-of select="$html.ext"/>
1422 </xsl:template>
1423
1424 <xsl:template match="*" mode="recursive-chunk-filename">
1425   <!-- placeholder for real template in chunking stylesheet -->
1426   <xsl:value-of select="concat(local-name(.), '-', generate-id(.))"/>
1427 </xsl:template>
1428
1429 <xsl:template name="manifest.content.items">
1430   <xsl:apply-templates select="." mode="package.manifest"/>
1431   <xsl:call-template name="manifest.images"/>
1432 </xsl:template>
1433
1434 <xsl:template match="text()" mode="package.manifest"/>
1435
1436 <xsl:template match="d:mediaobject|d:mediaobjectco|d:inlinemediaobject" priority="1"
1437               mode="package.manifest">
1438   <!-- These are handled out of line so a unique list is created
1439        to remove duplicate references -->
1440 </xsl:template>
1441
1442 <xsl:template match="*" mode="package.manifest">
1443
1444   <xsl:variable name="is.chunk">
1445     <xsl:call-template name="chunk">
1446       <xsl:with-param name="node" select="."/>
1447     </xsl:call-template>
1448   </xsl:variable>
1449
1450   <xsl:if test="$is.chunk != 0">
1451     <xsl:variable name="href">
1452       <xsl:call-template name="href.target.with.base.dir">
1453         <xsl:with-param name="context" select="/"/>
1454       </xsl:call-template>
1455     </xsl:variable>
1456   
1457     <xsl:variable name="id" select="concat($epub.package.id.prefix, generate-id())"/>
1458
1459     <xsl:variable name="properties.set">
1460       <xsl:call-template name="svg.property"/>
1461       <xsl:text> </xsl:text>
1462       <xsl:call-template name="mathml.property"/>
1463     </xsl:variable>
1464
1465     <xsl:variable name="properties" select="normalize-space($properties.set)"/>
1466
1467     <xsl:element namespace="{$opf.namespace}" name="item">
1468       <xsl:attribute name="id">
1469         <xsl:value-of select="$id"/>
1470       </xsl:attribute>
1471       <xsl:attribute name="href">
1472         <xsl:value-of select="$href"/>
1473       </xsl:attribute>
1474       <xsl:attribute name="media-type">application/xhtml+xml</xsl:attribute>
1475       <xsl:if test="string-length($properties) != 0">
1476         <xsl:attribute name="properties">
1477           <xsl:value-of select="$properties"/>
1478         </xsl:attribute>
1479       </xsl:if>
1480     </xsl:element>
1481   </xsl:if>  
1482   <xsl:apply-templates mode="package.manifest"/>
1483
1484 </xsl:template>
1485
1486 <xsl:template name="svg.property">
1487   <xsl:param name="this.chunk" select="."/>
1488
1489   <xsl:variable name="genid" select="generate-id($this.chunk)"/>
1490
1491   <!-- get the chunkfast div element for this chunk -->
1492   <xsl:variable name="div" select="$chunkset[@id=$genid or @xml:id=$genid]"/>
1493
1494   <!-- get the chunkfast div element the next chunk -->
1495   <xsl:variable name="nextdiv"
1496                 select="($div/following-sibling::cf:div|
1497                          $div/following::cf:div|
1498                          $div/cf:div)[1]"/>
1499
1500   <!-- get the element corresponding to the next chunk -->
1501   <xsl:variable name="next.chunk" select="key('genid', ($nextdiv/@id|$nextdiv/@xml:id)[1])"/>
1502
1503   <xsl:choose>
1504     <xsl:when test="$next.chunk">
1505       <xsl:variable name="this.imagedata"
1506                     select="$this.chunk//d:mediaobject"/>
1507       <xsl:variable name="before.next"
1508                     select="$next.chunk/preceding::d:mediaobject"/>
1509       
1510       <!-- select for an SVG imagedata in the intersection of them -->
1511       <xsl:variable name="mediaobject.set"
1512           select="$this.imagedata[count(.|$before.next) = count($before.next)]"/>
1513       <xsl:variable name="svg.imagedata">
1514         <xsl:for-each select="$mediaobject.set">
1515           <xsl:variable name="olist" select="d:imageobject[not(@role = 'poster')] |
1516                                              d:imageobjectco"/>
1517           <xsl:variable name="mediaobject.index">
1518             <xsl:call-template name="select.mediaobject.index">
1519               <xsl:with-param name="olist" select="$olist"/>
1520             </xsl:call-template>
1521           </xsl:variable>
1522           <xsl:variable name="object" select="$olist[position() = $mediaobject.index]"/>
1523           <xsl:if test="$object/d:imagedata[contains(
1524                       substring(@fileref, string-length(@fileref)-3,4), '.svg')]">
1525             <xsl:text>svg</xsl:text>
1526           </xsl:if>
1527         </xsl:for-each>
1528       </xsl:variable>
1529     
1530       <xsl:if test="contains($svg.imagedata, 'svg')">
1531         <xsl:text>svg</xsl:text>
1532      </xsl:if>
1533     </xsl:when>
1534     <xsl:otherwise>
1535       <xsl:variable name="mediaobject.set"
1536                     select="$this.chunk//d:mediaobject"/>
1537       <xsl:variable name="svg.imagedata">
1538         <xsl:for-each select="$mediaobject.set">
1539           <xsl:variable name="olist" select="d:imageobject[not(@role = 'poster')] |
1540                                              d:imageobjectco"/>
1541           <xsl:variable name="mediaobject.index">
1542             <xsl:call-template name="select.mediaobject.index">
1543               <xsl:with-param name="olist" select="$olist"/>
1544             </xsl:call-template>
1545           </xsl:variable>
1546           <xsl:variable name="object" select="$olist[position() = $mediaobject.index]"/>
1547           <xsl:if test="$object/d:imagedata[contains(
1548                       substring(@fileref, string-length(@fileref)-3,4), '.svg')]">
1549             <xsl:text>svg</xsl:text>
1550           </xsl:if>
1551         </xsl:for-each>
1552       </xsl:variable>
1553     
1554       <xsl:if test="contains($svg.imagedata, 'svg')">
1555         <xsl:text>svg</xsl:text>
1556      </xsl:if>
1557     </xsl:otherwise>
1558   </xsl:choose>
1559
1560 </xsl:template>
1561
1562 <xsl:template name="mathml.property">
1563   <xsl:param name="this.chunk" select="."/>
1564
1565   <xsl:variable name="genid" select="generate-id($this.chunk)"/>
1566
1567   <!-- get the chunkfast div element for this chunk -->
1568   <xsl:variable name="div" select="$chunkset[@id=$genid or @xml:id=$genid]"/>
1569
1570   <!-- get the chunkfast div element the next chunk -->
1571   <xsl:variable name="nextdiv"
1572                 select="($div/following-sibling::cf:div|
1573                          $div/following::cf:div|
1574                          $div/cf:div)[1]"/>
1575
1576   <!-- get the element corresponding to the next chunk -->
1577   <xsl:variable name="next.chunk" select="key('genid', ($nextdiv/@id|$nextdiv/@xml:id)[1])"/>
1578
1579   <xsl:variable name="this.math"
1580                 select="$this.chunk//m:*"/>
1581   <xsl:variable name="before.next"
1582                 select="$next.chunk/preceding::m:*"/>
1583   
1584   <!-- select for an SVG imagedata in the intersection of them -->
1585   <xsl:variable name="intersection"
1586       select="$this.math[count(.|$before.next) = count($before.next)]"/>
1587
1588   <xsl:if test="count($intersection) != 0">
1589     <xsl:text>mathml</xsl:text>
1590  </xsl:if>
1591 </xsl:template>
1592
1593 <xsl:template name="manifest.image.item">
1594 </xsl:template>
1595
1596 <!-- Need a uniqued list of images -->
1597 <xsl:template name="manifest.images">
1598   <xsl:message>Generating image list ...</xsl:message>
1599   <xsl:variable name="imagelist">
1600     <xsl:choose>
1601       <xsl:when test="$rootid != ''">
1602         <xsl:apply-templates select="key('id', $rootid)" mode="enumerate-images"/>
1603       </xsl:when>
1604       <xsl:otherwise>
1605         <xsl:apply-templates select="/" mode="enumerate-images"/>
1606       </xsl:otherwise>
1607     </xsl:choose>
1608   </xsl:variable>
1609
1610   <xsl:choose>
1611     <xsl:when test="$exsl.node.set.available != 0 and 
1612                     function-available('set:distinct')">
1613       <xsl:for-each select="set:distinct(exsl:node-set($imagelist)/*)">
1614         <xsl:if test="string-length(tmp-href) != 0">
1615           <!-- convert the child elements to attributes -->
1616           <xsl:element name="item" namespace="{$opf.namespace}">
1617             <xsl:attribute name="id">
1618               <xsl:value-of select="generate-id()"/>
1619             </xsl:attribute>
1620             <xsl:attribute name="href">
1621               <xsl:value-of select="tmp-href"/>
1622             </xsl:attribute>
1623             <xsl:attribute name="media-type">
1624               <xsl:value-of select="media-type"/>
1625             </xsl:attribute>
1626           </xsl:element>
1627         </xsl:if>
1628       </xsl:for-each>
1629     </xsl:when>
1630     <xsl:when test="$exsl.node.set.available != 0">
1631       <xsl:for-each select="exsl:node-set($imagelist)/*">
1632         <xsl:element name="item" namespace="{$opf.namespace}">
1633           <xsl:attribute name="id">
1634             <xsl:value-of select="generate-id()"/>
1635           </xsl:attribute>
1636           <xsl:attribute name="href">
1637             <xsl:value-of select="tmp-href"/>
1638           </xsl:attribute>
1639           <xsl:attribute name="media-type">
1640             <xsl:value-of select="media-type"/>
1641           </xsl:attribute>
1642         </xsl:element>
1643       </xsl:for-each>
1644     </xsl:when>
1645     <xsl:otherwise>
1646       <xsl:message terminate="yes">
1647         <xsl:text>ERROR: cannot process images list without </xsl:text>
1648         <xsl:text>exsl:node-set() function</xsl:text>
1649       </xsl:message>
1650       <!-- cannot do this without node-set() -->
1651     </xsl:otherwise>
1652   </xsl:choose>
1653
1654 </xsl:template>
1655
1656 <xsl:template match="*" mode="enumerate-images">
1657   <xsl:apply-templates mode="enumerate-images"/>
1658 </xsl:template>
1659
1660 <xsl:template match="text()" mode="enumerate-images"/>
1661
1662 <xsl:template match="d:graphic|d:inlinegraphic[@format!='linespecific']"
1663               mode="enumerate-images">
1664 </xsl:template>
1665
1666 <!-- cover image handled separates to give it an extra property attribute -->
1667 <xsl:template match="d:mediaobject[@role='cover' or ancestor::d:cover]"
1668               mode="enumerate-images"/>
1669
1670 <xsl:template match="d:mediaobject|d:inlinemediaobject" mode="enumerate-images">
1671
1672   <xsl:variable name="olist" 
1673                 select="d:imageobject[not(@role = 'poster')]
1674                        |d:imageobjectco
1675                        |d:videoobject
1676                        |d:audioobject
1677                        |d:textobject"/>
1678  
1679   <xsl:variable name="object.index">
1680     <xsl:call-template name="select.mediaobject.index">
1681       <xsl:with-param name="olist" select="$olist"/>
1682       <xsl:with-param name="count" select="1"/>
1683     </xsl:call-template>
1684   </xsl:variable>
1685   
1686   <xsl:variable name="object" select="$olist[position() = $object.index]"/>
1687
1688   <xsl:apply-templates select="$object" mode="enumerate-images"/>
1689
1690   <!-- also include a poster image if present -->
1691   <xsl:apply-templates select="d:imageobject[@role = 'poster']" mode="enumerate-images"/>
1692
1693 </xsl:template>
1694
1695 <xsl:template match="d:imageobject|d:videoobject|d:audioobject" mode="enumerate-images">
1696   <xsl:param name="object" select="."/>
1697
1698   <xsl:if test="$object">
1699     <xsl:for-each select="$object/d:imagedata|$object/d:videodata|$object/d:audiodata">
1700       <xsl:variable name="output_filename">
1701         <xsl:call-template name="mediaobject.filename">
1702           <xsl:with-param name="object" select="."/>
1703         </xsl:call-template>
1704       </xsl:variable>
1705   
1706       <xsl:variable name="image.filename">
1707          <xsl:if test="$img.src.path != '' and
1708                          not(starts-with($output_filename, '/')) and
1709                          not(contains($output_filename, '://'))">
1710            <xsl:value-of select="$img.src.path"/>
1711          </xsl:if>
1712          <xsl:value-of select="$output_filename"/>
1713       </xsl:variable>
1714   
1715       <xsl:variable name="image.extension">
1716         <xsl:call-template name="filename-extension">
1717           <xsl:with-param name="filename" select="$image.filename"/>
1718         </xsl:call-template>
1719       </xsl:variable>
1720   
1721       <xsl:variable name="image.type">
1722         <xsl:call-template name="graphic.format.content-type">
1723           <xsl:with-param name="format" select="translate($image.extension, 
1724                      &lowercase;, &uppercase;)"/>
1725         </xsl:call-template>
1726       </xsl:variable>
1727   
1728       <xsl:element name="tmp-filename" namespace="">
1729         <xsl:element name="tmp-href" namespace="">
1730           <xsl:value-of select="$image.filename"/>
1731         </xsl:element>
1732         <xsl:element name="media-type" namespace="">
1733           <xsl:value-of select="$image.type"/>
1734         </xsl:element>
1735       </xsl:element>
1736     </xsl:for-each>
1737
1738   </xsl:if>
1739 </xsl:template>
1740  
1741 <!-- Add in the generated images -->
1742 <xsl:template match="d:note|d:caution|d:warning|d:important|d:tip" mode="enumerate-images">
1743   <xsl:if test="$admon.graphics != 0">
1744     <xsl:variable name="image.filename">
1745       <xsl:call-template name="admon.graphic"/>
1746     </xsl:variable>
1747
1748     <xsl:variable name="image.type">
1749       <xsl:call-template name="graphic.format.content-type">
1750         <xsl:with-param name="format" select="translate(
1751                substring-after($admon.graphics.extension,'.'), 
1752                    &lowercase;, &uppercase;)"/>
1753       </xsl:call-template>
1754     </xsl:variable>
1755
1756     <xsl:element name="tmp-filename" namespace="">
1757       <xsl:element name="tmp-href" namespace="">
1758         <xsl:value-of select="$image.filename"/>
1759       </xsl:element>
1760       <xsl:element name="media-type" namespace="">
1761         <xsl:value-of select="$image.type"/>
1762       </xsl:element>
1763     </xsl:element>
1764
1765   </xsl:if>
1766 </xsl:template>
1767
1768 <xsl:template match="d:callout" mode="enumerate-images">
1769   <!-- process arearefs to get name of callout bug image files -->
1770   <xsl:if test="$callout.graphics != 0">
1771     <xsl:variable name="arearefs">
1772       <xsl:call-template name="callout.arearefs">
1773         <xsl:with-param name="arearefs" select="@arearefs"/>
1774       </xsl:call-template>
1775     </xsl:variable>
1776
1777     <xsl:variable name="nodes" select="exsl:node-set($arearefs)"/>
1778
1779     <xsl:for-each select="$nodes//*[@src]">
1780       <xsl:variable name="image.filename" select="@src"/>
1781
1782       <xsl:variable name="image.type">
1783         <xsl:call-template name="graphic.format.content-type">
1784           <xsl:with-param name="format" select="translate(
1785                  substring-after($callout.graphics.extension,'.'), 
1786                      &lowercase;, &uppercase;)"/>
1787         </xsl:call-template>
1788       </xsl:variable>
1789
1790       <xsl:element name="tmp-filename" namespace="">
1791         <xsl:element name="tmp-href" namespace="">
1792           <xsl:value-of select="$image.filename"/>
1793         </xsl:element>
1794         <xsl:element name="media-type" namespace="">
1795           <xsl:value-of select="$image.type"/>
1796         </xsl:element>
1797       </xsl:element>
1798     </xsl:for-each>
1799
1800   </xsl:if>
1801 </xsl:template>
1802
1803 <xsl:template match="d:co" mode="enumerate-images">
1804   <!-- process co to get name of callout bug image file -->
1805   <xsl:if test="$callout.graphics != 0">
1806     <xsl:variable name="result">
1807       <xsl:apply-templates select="." mode="callout-bug"/>
1808     </xsl:variable>
1809
1810     <xsl:variable name="nodes" select="exsl:node-set($result)"/>
1811
1812     <xsl:for-each select="$nodes//*[@src]">
1813       <xsl:variable name="image.filename" select="@src"/>
1814
1815       <xsl:variable name="image.type">
1816         <xsl:call-template name="graphic.format.content-type">
1817           <xsl:with-param name="format" select="translate(
1818                  substring-after($callout.graphics.extension,'.'), 
1819                      &lowercase;, &uppercase;)"/>
1820         </xsl:call-template>
1821       </xsl:variable>
1822
1823       <xsl:element name="tmp-filename" namespace="">
1824         <xsl:element name="tmp-href" namespace="">
1825           <xsl:value-of select="$image.filename"/>
1826         </xsl:element>
1827         <xsl:element name="media-type" namespace="">
1828           <xsl:value-of select="$image.type"/>
1829         </xsl:element>
1830       </xsl:element>
1831     </xsl:for-each>
1832
1833   </xsl:if>
1834
1835 </xsl:template>
1836
1837 <!-- ======================================================== -->
1838 <!-- NCX templates are for backwards compatibility with EPUB2 -->
1839 <!-- ======================================================== -->
1840
1841 <xsl:template name="ncx">
1842   <xsl:message>Generating NCX file ...</xsl:message>
1843   <xsl:call-template name="write.chunk">
1844     <xsl:with-param name="filename">
1845       <xsl:value-of select="$epub.ncx.pathname" />
1846     </xsl:with-param>
1847     <xsl:with-param name="method" select="'xml'" />
1848     <xsl:with-param name="encoding" select="'utf-8'" />
1849     <xsl:with-param name="indent" select="'no'" />
1850     <xsl:with-param name="quiet" select="$chunk.quietly" />
1851     <xsl:with-param name="doctype-public" select="''"/> <!-- intentionally blank -->
1852     <xsl:with-param name="doctype-system" select="''"/> <!-- intentionally blank -->
1853     <xsl:with-param name="content">
1854       <xsl:element name="ncx" namespace="{$ncx.namespace}">
1855         <xsl:attribute name="version">2005-1</xsl:attribute>
1856
1857           <!-- Via Martin Goerner: On covers: the IDPF2.0 standard unfortunately does not have a provision for
1858           covers. We had to add one and we did so in conjunction with the IDPF and
1859           various publishers. The tag chosen to define the covers is:
1860           <meta name="cover" content="-reference to a manifest item-">
1861           Then, we also added a bit of logic to get rid cleanly of the HTML cover
1862           people usually add because the logical cover is not specced by the IDPF. So,
1863           if the HTML cover item is marked linear="no" AND there is a guide item of
1864           type="cover" pointing to it AND there is a logical cover specified in a
1865           <meta name="cover"> tag, THEN, the HTML cover is discarded. -->
1866         <xsl:element name="head" namespace="{$ncx.namespace}">
1867           <xsl:if test="/*/*[d:cover or contains(name(.), 'info')]//d:mediaobject[@role='cover' or ancestor::d:cover]">
1868             <xsl:element name="meta" namespace="{$ncx.namespace}">
1869               <xsl:attribute name="name">cover</xsl:attribute>
1870               <xsl:attribute name="content">
1871                 <xsl:value-of select="$epub.cover.filename.id"/>
1872               </xsl:attribute>
1873             </xsl:element>
1874           </xsl:if>
1875           <xsl:element name="meta" namespace="{$ncx.namespace}">
1876             <xsl:attribute name="name">dtb:uid</xsl:attribute>
1877             <xsl:attribute name="content"><xsl:call-template name="package-identifier"/></xsl:attribute>
1878           </xsl:element>
1879         </xsl:element>
1880
1881         <xsl:choose>
1882           <xsl:when test="$rootid != ''">
1883             <xsl:variable name="title">
1884               <xsl:if test="$epub.autolabel != 0">
1885                 <xsl:variable name="label.markup">
1886                   <xsl:apply-templates select="key('id',$rootid)" mode="label.markup" />
1887                 </xsl:variable>
1888                 <xsl:if test="normalize-space($label.markup)">
1889                   <xsl:value-of select="concat($label.markup,$autotoc.label.separator)" />
1890                 </xsl:if>
1891               </xsl:if>
1892               <xsl:apply-templates select="key('id',$rootid)" mode="title.markup" />
1893             </xsl:variable>
1894             <xsl:variable name="href">
1895               <xsl:call-template name="href.target.with.base.dir">
1896                 <xsl:with-param name="object" select="key('id',$rootid)" />
1897               </xsl:call-template>
1898             </xsl:variable>
1899             <xsl:element name="docTitle" namespace="{$ncx.namespace}">
1900               <xsl:element name="text" namespace="{$ncx.namespace}"><xsl:value-of select="normalize-space($title)" />  </xsl:element>
1901             </xsl:element>
1902             <xsl:element name="navMap" namespace="{$ncx.namespace}">
1903               <xsl:apply-templates select="key('id',$rootid)/*" mode="ncx" />
1904             </xsl:element>
1905           </xsl:when>
1906           <xsl:otherwise>
1907             <xsl:variable name="title">
1908               <xsl:if test="$epub.autolabel != 0">
1909                 <xsl:variable name="label.markup">
1910                   <xsl:apply-templates select="/*" mode="label.markup" />
1911                 </xsl:variable>
1912                 <xsl:if test="normalize-space($label.markup)">
1913                   <xsl:value-of select="concat($label.markup,$autotoc.label.separator)" />
1914                 </xsl:if>
1915               </xsl:if>
1916               <xsl:apply-templates select="/*" mode="title.markup" />
1917             </xsl:variable>
1918             <xsl:variable name="href">
1919               <xsl:call-template name="href.target.with.base.dir">
1920                 <xsl:with-param name="object" select="/" />
1921               </xsl:call-template>
1922             </xsl:variable>
1923             <xsl:element name="docTitle" namespace="{$ncx.namespace}">
1924               <xsl:element name="text" namespace="{$ncx.namespace}">
1925                 <xsl:value-of select="normalize-space($title)" />
1926               </xsl:element>
1927             </xsl:element>
1928             <xsl:element name="navMap" namespace="{$ncx.namespace}">
1929               <xsl:choose>
1930                 <xsl:when test="$root.is.a.chunk != '0'">
1931                   <xsl:apply-templates select="/*" mode="ncx" />
1932                   <xsl:apply-templates select="/*/*" mode="ncx" />
1933                 </xsl:when>
1934                 <xsl:otherwise>
1935                   <xsl:apply-templates select="/*/*" mode="ncx" />
1936                 </xsl:otherwise>
1937               </xsl:choose>
1938             </xsl:element>
1939           </xsl:otherwise>
1940
1941         </xsl:choose>
1942       </xsl:element>
1943     </xsl:with-param>
1944   </xsl:call-template>
1945 </xsl:template>
1946
1947 <xsl:template match="d:book|
1948                      d:article|
1949                      d:topic|
1950                      d:part|
1951                      d:reference|
1952                      d:preface|
1953                      d:chapter|
1954                      d:bibliography|
1955                      d:appendix|
1956                      d:glossary|
1957                      d:section|
1958                      d:sect1|
1959                      d:sect2|
1960                      d:sect3|
1961                      d:sect4|
1962                      d:sect5|
1963                      d:refentry|
1964                      d:colophon|
1965                      d:bibliodiv[d:title]|
1966                      d:setindex|
1967                      d:index"
1968               mode="ncx">
1969   <xsl:variable name="depth" select="count(ancestor::*)"/>
1970   <xsl:variable name="title">
1971     <xsl:if test="$epub.autolabel != 0">
1972       <xsl:variable name="label.markup">
1973         <xsl:apply-templates select="." mode="label.markup" />
1974       </xsl:variable>
1975       <xsl:if test="normalize-space($label.markup)">
1976         <xsl:value-of
1977           select="concat($label.markup,$autotoc.label.separator)" />
1978       </xsl:if>
1979     </xsl:if>
1980     <xsl:apply-templates select="." mode="title.markup" />
1981   </xsl:variable>
1982
1983   <xsl:variable name="href">
1984     <xsl:call-template name="href.target.with.base.dir">
1985       <xsl:with-param name="context" select="/" />
1986       <!-- Generate links relative to the location of root file/toc.xml file -->
1987     </xsl:call-template>
1988   </xsl:variable>
1989
1990   <xsl:variable name="id">
1991     <xsl:value-of select="generate-id(.)"/>
1992   </xsl:variable>
1993   <xsl:variable name="order">
1994     <xsl:value-of select="$depth +
1995                                 count(preceding::d:part|
1996                                 preceding::d:reference|
1997                                 preceding::d:book[parent::d:set]|
1998                                 preceding::d:preface|
1999                                 preceding::d:chapter|
2000                                 preceding::d:bibliography|
2001                                 preceding::d:appendix|
2002                                 preceding::d:article|
2003                                 preceding::d:topic|
2004                                 preceding::d:glossary|
2005                                 preceding::d:section[not(parent::d:partintro)]|
2006                                 preceding::d:sect1[not(parent::d:partintro)]|
2007                                 preceding::d:sect2[not(ancestor::d:partintro)]|
2008                                 preceding::d:sect3[not(ancestor::d:partintro)]|
2009                                 preceding::d:sect4[not(ancestor::d:partintro)]|
2010                                 preceding::d:sect5[not(ancestor::d:partintro)]|
2011                                 preceding::d:refentry|
2012                                 preceding::d:colophon|
2013                                 preceding::d:bibliodiv[d:title]|
2014                                 preceding::d:index)"/>
2015   </xsl:variable>
2016
2017   <xsl:element name="navPoint" namespace="{$ncx.namespace}">
2018     <xsl:attribute name="id">
2019       <xsl:value-of select="$id"/>
2020     </xsl:attribute>
2021
2022     <xsl:attribute name="playOrder">
2023       <xsl:choose>
2024         <xsl:when test="/*[self::d:set]">
2025           <xsl:value-of select="$order"/>
2026         </xsl:when>
2027         <xsl:when test="$root.is.a.chunk != '0'">
2028           <xsl:value-of select="$order + 1"/>
2029         </xsl:when>
2030         <xsl:otherwise>
2031           <xsl:value-of select="$order - 0"/>
2032         </xsl:otherwise>
2033       </xsl:choose>
2034     </xsl:attribute>
2035     <xsl:element name="navLabel" namespace="{$ncx.namespace}">
2036       <xsl:element name="text" namespace="{$ncx.namespace}"><xsl:value-of select="normalize-space($title)"/> </xsl:element>
2037     </xsl:element>
2038     <xsl:element name="content" namespace="{$ncx.namespace}">
2039       <xsl:attribute name="src">
2040         <xsl:value-of select="$href"/>
2041       </xsl:attribute>
2042     </xsl:element>
2043     <xsl:if test="$depth != 0">
2044       <!-- Don't recurse on root element, but treat it as a single point so
2045       the progress bar shows all top level children -->
2046       <xsl:apply-templates select="d:book[parent::d:set]|d:part|d:reference|d:preface|d:chapter|d:bibliography|d:appendix|d:article|d:topic|d:glossary|d:section|d:sect1|d:sect2|d:sect3|d:sect4|d:sect5|d:refentry|d:colophon|d:bibliodiv[d:title]|d:setindex|d:index" mode="ncx"/>
2047     </xsl:if>
2048   </xsl:element>
2049
2050 </xsl:template>
2051
2052 <xsl:template match="text()" mode="ncx" />
2053
2054 <xsl:template name="package.spine">
2055
2056   <xsl:variable name="toc.params">
2057     <xsl:call-template name="find.path.params">
2058       <xsl:with-param name="node" select="."/>
2059       <xsl:with-param name="table" select="normalize-space($generate.toc)"/>
2060     </xsl:call-template>
2061   </xsl:variable>
2062
2063   <xsl:element namespace="http://www.idpf.org/2007/opf" name="spine">
2064     <xsl:if test="$epub.include.ncx != 0">
2065       <xsl:attribute name="toc">
2066         <xsl:value-of select="$epub.ncx.manifest.id"/>
2067       </xsl:attribute>
2068     </xsl:if>
2069
2070     <xsl:call-template name="spine.cover"/>
2071
2072
2073     <xsl:if test="contains($toc.params, 'toc')">
2074       <xsl:element namespace="http://www.idpf.org/2007/opf" name="itemref">
2075         <xsl:attribute name="idref"> <xsl:value-of select="$epub.html.toc.id"/> </xsl:attribute>
2076         <xsl:attribute name="linear">yes</xsl:attribute>
2077       </xsl:element>
2078     </xsl:if>  
2079
2080     <!-- TODO: be nice to have a idref="titlepage" here -->
2081     <xsl:choose>
2082       <xsl:when test="$root.is.a.chunk != '0'">
2083         <xsl:apply-templates select="/*" mode="package.spine"/>
2084       </xsl:when>
2085       <xsl:otherwise>
2086         <xsl:apply-templates select="/*/*" mode="package.spine"/>
2087       </xsl:otherwise>
2088     </xsl:choose>
2089                                  
2090   </xsl:element>
2091 </xsl:template>
2092
2093 <xsl:template name="spine.cover">
2094   <xsl:variable name="info" select="./*[contains(local-name(.), 'info')][1]"/>
2095   <xsl:variable name="cover.image" 
2096                 select="$info//d:mediaobject[@role='cover' or ancestor::d:cover]"/>
2097
2098   <xsl:if test="$cover.image">
2099     <!-- generate the spine reference to that cover html file -->
2100     <xsl:element namespace="http://www.idpf.org/2007/opf" name="itemref">
2101       <xsl:attribute name="idref">
2102         <xsl:value-of select="$epub.cover.filename.id"/>
2103       </xsl:attribute>
2104       <xsl:attribute name="linear">
2105       <xsl:choose>
2106         <xsl:when test="$epub.cover.linear">
2107           <xsl:text>yes</xsl:text>
2108         </xsl:when>
2109         <xsl:otherwise>no</xsl:otherwise>
2110       </xsl:choose>
2111       </xsl:attribute>
2112     </xsl:element>
2113   </xsl:if>
2114 </xsl:template>
2115
2116 <xsl:template match="*" mode="package.spine">
2117   <xsl:variable name="is.chunk">
2118     <xsl:call-template name="chunk">
2119       <xsl:with-param name="node" select="."/>
2120     </xsl:call-template>
2121   </xsl:variable>
2122
2123   <xsl:if test="$is.chunk != 0">
2124     <xsl:element namespace="http://www.idpf.org/2007/opf" name="itemref">
2125       <xsl:attribute name="idref">
2126         <xsl:value-of select="concat($epub.package.id.prefix, generate-id(.))"/>
2127       </xsl:attribute>
2128     </xsl:element>
2129     <xsl:apply-templates select="*|.//d:refentry" mode="package.spine"/>
2130   </xsl:if>
2131 </xsl:template>
2132
2133 <xsl:template match="*" mode="container">
2134   <xsl:call-template name="container"/>
2135 </xsl:template>
2136
2137 <xsl:template name="container">
2138   <!-- The path in rootfile does not include all of base.dir, only the last part -->
2139   <xsl:variable name="full-path-dir">
2140     <xsl:call-template name="filename-basename">
2141       <xsl:with-param name="filename" select="$chunk.base.dir"/>
2142     </xsl:call-template>
2143   </xsl:variable>
2144   <xsl:call-template name="write.chunk">
2145     <xsl:with-param name="filename">
2146       <xsl:value-of select="$epub.container.pathname" />
2147     </xsl:with-param>
2148     <xsl:with-param name="method" select="'xml'" />
2149     <xsl:with-param name="encoding" select="'utf-8'" />
2150     <xsl:with-param name="indent" select="'no'" />
2151     <xsl:with-param name="quiet" select="$chunk.quietly" />
2152     <xsl:with-param name="doctype-public" select="''"/> <!-- intentionally blank -->
2153     <xsl:with-param name="doctype-system" select="''"/> <!-- intentionally blank -->
2154
2155     <xsl:with-param name="content">
2156       <xsl:element namespace="urn:oasis:names:tc:opendocument:xmlns:container" name="container">
2157         <xsl:attribute name="version">1.0</xsl:attribute>
2158         <xsl:element namespace="urn:oasis:names:tc:opendocument:xmlns:container" name="rootfiles">
2159           <xsl:element namespace="urn:oasis:names:tc:opendocument:xmlns:container" name="rootfile">
2160             <xsl:attribute name="full-path">
2161               <xsl:value-of 
2162                      select="concat($full-path-dir, $epub.package.filename)"/>
2163             </xsl:attribute>
2164             <xsl:attribute name="media-type">
2165               <xsl:text>application/oebps-package+xml</xsl:text>
2166             </xsl:attribute>
2167           </xsl:element>
2168         </xsl:element>
2169       </xsl:element>
2170     </xsl:with-param>
2171   </xsl:call-template>
2172 </xsl:template>
2173
2174 <xsl:template name="mimetype">
2175   <xsl:call-template name="write.text.chunk">
2176     <xsl:with-param name="filename" select="$epub.mimetype.pathname"/>
2177     <xsl:with-param name="content" select="$epub.mimetype.value"/>
2178   </xsl:call-template>
2179 </xsl:template>
2180
2181 <xsl:template match="d:mediaobject[@role='cover' or ancestor::d:cover]">
2182   <xsl:call-template name="write.chunk">
2183     <xsl:with-param name="filename">
2184       <xsl:value-of select="$epub.cover.pathname" />
2185     </xsl:with-param>
2186     <xsl:with-param name="method" select="'xml'" />
2187     <xsl:with-param name="encoding" select="'utf-8'" />
2188     <xsl:with-param name="indent" select="'no'" />
2189     <xsl:with-param name="quiet" select="$chunk.quietly" />
2190     <xsl:with-param name="content">
2191       <xsl:element namespace="http://www.w3.org/1999/xhtml" name="html">
2192         <xsl:element namespace="http://www.w3.org/1999/xhtml" name="head">
2193           <xsl:element namespace="http://www.w3.org/1999/xhtml" name="title">Cover</xsl:element>
2194           <xsl:element namespace="http://www.w3.org/1999/xhtml" name="style">
2195             <xsl:attribute name="type">text/css</xsl:attribute>
2196             <!-- Help the cover image scale nicely in the CSS then apply a max-width to look better in Adobe Digital Editions -->
2197             <xsl:text> img { max-width: 100%; }</xsl:text>
2198           </xsl:element>
2199         </xsl:element>
2200         <xsl:element namespace="http://www.w3.org/1999/xhtml" name="body">
2201           <xsl:element namespace="http://www.w3.org/1999/xhtml" name="div">
2202             <xsl:attribute name="id">
2203               <xsl:value-of select="$epub.cover.image.id"/>
2204             </xsl:attribute>
2205             <xsl:choose>
2206               <xsl:when test="d:imageobject[@role='front-large']">
2207                 <xsl:apply-templates select="d:imageobject[@role='front-large']"/>
2208               </xsl:when>
2209               <xsl:otherwise>
2210                 <xsl:apply-templates select="d:imageobject[1]"/>
2211               </xsl:otherwise>
2212             </xsl:choose>
2213           </xsl:element>
2214           <!-- If this is defined as an explicit cover page, then process
2215           any remaining text -->
2216           <xsl:if test="ancestor::d:cover">
2217             <xsl:apply-templates select="ancestor::d:cover/d:para"/>
2218           </xsl:if>
2219         </xsl:element>
2220       </xsl:element>
2221     </xsl:with-param>  
2222   </xsl:call-template>  
2223 </xsl:template>
2224
2225 <!-- EPUB3: use <nav> and <ol> in TOC lists  -->
2226 <xsl:template name="make.toc">
2227   <xsl:param name="toc-context" select="."/>
2228   <xsl:param name="toc.title.p" select="true()"/>
2229   <xsl:param name="nodes" select="/NOT-AN-ELEMENT"/>
2230
2231   <xsl:variable name="nodes.plus" select="$nodes | d:qandaset"/>
2232
2233   <xsl:variable name="toc.title">
2234     <xsl:if test="$toc.title.p">
2235       <xsl:choose>
2236         <xsl:when test="$make.clean.html != 0">
2237           <div class="toc-title">
2238             <xsl:call-template name="gentext">
2239               <xsl:with-param name="key">TableofContents</xsl:with-param>
2240             </xsl:call-template>
2241           </div>
2242         </xsl:when>
2243         <xsl:otherwise>
2244           <p>
2245             <b>
2246               <xsl:call-template name="gentext">
2247                 <xsl:with-param name="key">TableofContents</xsl:with-param>
2248               </xsl:call-template>
2249             </b>
2250           </p>
2251         </xsl:otherwise>
2252       </xsl:choose>
2253     </xsl:if>
2254   </xsl:variable>
2255
2256   <xsl:choose>
2257     <xsl:when test="$manual.toc != ''">
2258       <xsl:variable name="id">
2259         <xsl:call-template name="object.id"/>
2260       </xsl:variable>
2261       <xsl:variable name="toc" select="document($manual.toc, .)"/>
2262       <xsl:variable name="tocentry" select="$toc//d:tocentry[@linkend=$id]"/>
2263       <xsl:if test="$tocentry and $tocentry/*">
2264         <div class="toc">
2265           <xsl:copy-of select="$toc.title"/>
2266           <nav epub:type="toc">
2267             <xsl:element name="{$toc.list.type}">
2268               <xsl:call-template name="manual-toc">
2269                 <xsl:with-param name="tocentry" select="$tocentry/*[1]"/>
2270               </xsl:call-template>
2271             </xsl:element>
2272           </nav>
2273         </div>
2274       </xsl:if>
2275     </xsl:when>
2276     <xsl:otherwise>
2277       <xsl:choose>
2278         <xsl:when test="$qanda.in.toc != 0">
2279           <div class="toc">
2280             <xsl:copy-of select="$toc.title"/>
2281             <nav epub:type="toc">
2282               <xsl:element name="{$toc.list.type}">
2283                 <xsl:if test="$nodes.plus">
2284                   <xsl:apply-templates select="$nodes.plus" mode="toc">
2285                     <xsl:with-param name="toc-context" select="$toc-context"/>
2286                   </xsl:apply-templates>
2287                 </xsl:if>
2288               </xsl:element>
2289             </nav>
2290           </div>
2291         </xsl:when>
2292         <xsl:otherwise>
2293           <div class="toc">
2294             <xsl:copy-of select="$toc.title"/>
2295             <nav epub:type="toc">
2296               <xsl:element name="{$toc.list.type}">
2297                 <xsl:if test="$nodes">
2298                   <xsl:apply-templates select="$nodes" mode="toc">
2299                     <xsl:with-param name="toc-context" select="$toc-context"/>
2300                   </xsl:apply-templates>
2301                 </xsl:if>
2302               </xsl:element>
2303             </nav>
2304           </div>
2305         </xsl:otherwise>
2306       </xsl:choose>
2307
2308     </xsl:otherwise>
2309   </xsl:choose>
2310 </xsl:template>
2311
2312 <xsl:template name="list.of.titles">
2313   <xsl:param name="toc-context" select="."/>
2314   <xsl:param name="titles" select="'table'"/>
2315   <xsl:param name="nodes" select=".//d:table"/>
2316
2317   <xsl:variable name="epub.type">
2318     <xsl:choose>
2319       <xsl:when test="$titles='table'">lot</xsl:when>
2320       <xsl:when test="$titles='figure'">loi</xsl:when>
2321       <xsl:when test="$titles='equation'">loi</xsl:when>
2322       <xsl:when test="$titles='example'">loi</xsl:when>
2323       <xsl:when test="$titles='procedure'">loi</xsl:when>
2324       <xsl:otherwise>loi</xsl:otherwise>
2325     </xsl:choose>
2326   </xsl:variable>
2327
2328   <xsl:if test="$nodes">
2329     <div class="list-of-{$titles}s">
2330       <nav epub:type="{$epub.type}">
2331         <h4 class="toc-title">
2332           <xsl:call-template name="gentext">
2333             <xsl:with-param name="key">
2334               <xsl:choose>
2335                 <xsl:when test="$titles='table'">ListofTables</xsl:when>
2336                 <xsl:when test="$titles='figure'">ListofFigures</xsl:when>
2337                 <xsl:when test="$titles='equation'">ListofEquations</xsl:when>
2338                 <xsl:when test="$titles='example'">ListofExamples</xsl:when>
2339                 <xsl:when test="$titles='procedure'">ListofProcedures</xsl:when>
2340                 <xsl:otherwise>ListofUnknown</xsl:otherwise>
2341               </xsl:choose>
2342             </xsl:with-param>
2343           </xsl:call-template>
2344         </h4>
2345
2346         <xsl:element name="{$toc.list.type}">
2347           <xsl:apply-templates select="$nodes" mode="toc">
2348             <xsl:with-param name="toc-context" select="$toc-context"/>
2349           </xsl:apply-templates>
2350         </xsl:element>
2351       </nav>
2352     </div>
2353   </xsl:if>
2354 </xsl:template>
2355
2356 <!-- EPUB3: add hidden="" for sections below toc.section.depth -->
2357 <xsl:template name="subtoc">
2358   <xsl:param name="toc-context" select="."/>
2359   <xsl:param name="nodes" select="NOT-AN-ELEMENT"/>
2360
2361   <xsl:variable name="nodes.plus" select="$nodes | d:qandaset"/>
2362
2363   <xsl:variable name="depth">
2364     <xsl:choose>
2365       <xsl:when test="local-name(.) = 'section'">
2366         <xsl:value-of select="count(ancestor::d:section) + 1"/>
2367       </xsl:when>
2368       <xsl:when test="local-name(.) = 'sect1'">1</xsl:when>
2369       <xsl:when test="local-name(.) = 'sect2'">2</xsl:when>
2370       <xsl:when test="local-name(.) = 'sect3'">3</xsl:when>
2371       <xsl:when test="local-name(.) = 'sect4'">4</xsl:when>
2372       <xsl:when test="local-name(.) = 'sect5'">5</xsl:when>
2373       <xsl:when test="local-name(.) = 'refsect1'">1</xsl:when>
2374       <xsl:when test="local-name(.) = 'refsect2'">2</xsl:when>
2375       <xsl:when test="local-name(.) = 'refsect3'">3</xsl:when>
2376       <xsl:when test="local-name(.) = 'simplesect'">
2377         <!-- sigh... -->
2378         <xsl:choose>
2379           <xsl:when test="local-name(..) = 'section'">
2380             <xsl:value-of select="count(ancestor::d:section)"/>
2381           </xsl:when>
2382           <xsl:when test="local-name(..) = 'sect1'">2</xsl:when>
2383           <xsl:when test="local-name(..) = 'sect2'">3</xsl:when>
2384           <xsl:when test="local-name(..) = 'sect3'">4</xsl:when>
2385           <xsl:when test="local-name(..) = 'sect4'">5</xsl:when>
2386           <xsl:when test="local-name(..) = 'sect5'">6</xsl:when>
2387           <xsl:when test="local-name(..) = 'refsect1'">2</xsl:when>
2388           <xsl:when test="local-name(..) = 'refsect2'">3</xsl:when>
2389           <xsl:when test="local-name(..) = 'refsect3'">4</xsl:when>
2390           <xsl:otherwise>1</xsl:otherwise>
2391         </xsl:choose>
2392       </xsl:when>
2393       <xsl:otherwise>0</xsl:otherwise>
2394     </xsl:choose>
2395   </xsl:variable>
2396
2397   <xsl:variable name="depth.from.context" select="count(ancestor::*)-count($toc-context/ancestor::*)"/>
2398
2399   <xsl:variable name="subtoc">
2400     <xsl:element name="{$toc.list.type}">
2401       <xsl:choose>
2402         <xsl:when test="$toc.listitem.type = 'li'
2403                   and $toc.section.depth > $depth and 
2404                   ( ($qanda.in.toc = 0 and count($nodes)&gt;0) or
2405                     ($qanda.in.toc != 0 and count($nodes.plus)&gt;0) )
2406                   and $toc.max.depth > $depth.from.context">
2407           <!-- No @hidden attribute -->
2408         </xsl:when>
2409         <xsl:otherwise>
2410           <!-- Add empty @hidden attribute if too deep -->
2411           <xsl:attribute name="hidden"></xsl:attribute>
2412         </xsl:otherwise>
2413       </xsl:choose>
2414
2415       <xsl:choose>
2416         <xsl:when test="$qanda.in.toc != 0">
2417           <xsl:apply-templates mode="toc" select="$nodes.plus">
2418             <xsl:with-param name="toc-context" select="$toc-context"/>
2419           </xsl:apply-templates>
2420         </xsl:when>
2421         <xsl:otherwise>
2422           <xsl:apply-templates mode="toc" select="$nodes">
2423             <xsl:with-param name="toc-context" select="$toc-context"/>
2424           </xsl:apply-templates>
2425         </xsl:otherwise>
2426       </xsl:choose>
2427     </xsl:element>
2428   </xsl:variable>
2429
2430   <xsl:variable name="subtoc.list">
2431     <xsl:choose>
2432       <xsl:when test="$toc.dd.type = ''">
2433         <xsl:copy-of select="$subtoc"/>
2434       </xsl:when>
2435       <xsl:otherwise>
2436         <xsl:element name="{$toc.dd.type}">
2437           <xsl:copy-of select="$subtoc"/>
2438         </xsl:element>
2439       </xsl:otherwise>
2440     </xsl:choose>
2441   </xsl:variable>
2442
2443   <xsl:element name="{$toc.listitem.type}">
2444     <xsl:call-template name="toc.line">
2445       <xsl:with-param name="toc-context" select="$toc-context"/>
2446     </xsl:call-template>
2447     <xsl:if test="$toc.listitem.type = 'li'
2448                   and 
2449                   ( ($qanda.in.toc = 0 and count($nodes)&gt;0) or
2450                     ($qanda.in.toc != 0 and count($nodes.plus)&gt;0) ) ">
2451       <xsl:copy-of select="$subtoc.list"/>
2452     </xsl:if>
2453   </xsl:element>
2454 </xsl:template>
2455
2456 <!-- Inserted when a title is blank to avoid epubcheck error -->
2457 <xsl:param name="toc.entry.default.text">&#xA0;</xsl:param>
2458
2459 <!-- EPUB3: either <a> or <span>, but not both  -->
2460 <xsl:template name="toc.line">
2461   <xsl:param name="toc-context" select="."/>
2462   <xsl:param name="depth" select="1"/>
2463   <xsl:param name="depth.from.context" select="8"/>
2464
2465   <xsl:variable name="title">
2466     <xsl:apply-templates select="." mode="title.markup"/>
2467   </xsl:variable>
2468
2469   <a>
2470     <xsl:attribute name="href">
2471       <xsl:call-template name="href.target">
2472         <xsl:with-param name="context" select="$toc-context"/>
2473         <xsl:with-param name="toc-context" select="$toc-context"/>
2474       </xsl:call-template>
2475     </xsl:attribute>
2476     
2477     <!-- * if $autotoc.label.in.hyperlink is non-zero, then output the label -->
2478     <!-- * as part of the hyperlinked title -->
2479     <xsl:if test="not($autotoc.label.in.hyperlink = 0)">
2480       <xsl:variable name="label">
2481         <xsl:apply-templates select="." mode="label.markup"/>
2482       </xsl:variable>
2483       <xsl:copy-of select="$label"/>
2484       <xsl:if test="$label != ''">
2485         <xsl:value-of select="$autotoc.label.separator"/>
2486       </xsl:if>
2487     </xsl:if>
2488
2489     <xsl:choose>
2490       <xsl:when test="string-length(normalize-space($title)) != 0">
2491         <xsl:copy-of select="$title"/>
2492       </xsl:when>
2493       <xsl:otherwise>
2494         <xsl:value-of select="$toc.entry.default.text"/>
2495       </xsl:otherwise>
2496     </xsl:choose>
2497   </a>
2498 </xsl:template>
2499
2500 <!-- Make sure all text is inside the <a> element for epub3 -->
2501 <xsl:template match="d:figure|d:table|d:example|d:equation|d:procedure" mode="toc">
2502   <xsl:param name="toc-context" select="."/>
2503
2504   <xsl:element name="{$toc.listitem.type}">
2505     <a>
2506       <xsl:attribute name="href">
2507         <xsl:call-template name="href.target">
2508           <xsl:with-param name="toc-context" select="$toc-context"/>
2509         </xsl:call-template>
2510       </xsl:attribute>
2511       <xsl:variable name="label">
2512         <xsl:apply-templates select="." mode="label.markup"/>
2513       </xsl:variable>
2514       <xsl:copy-of select="$label"/>
2515       <xsl:if test="$label != ''">
2516         <xsl:value-of select="$autotoc.label.separator"/>
2517       </xsl:if>
2518       <xsl:apply-templates select="." mode="titleabbrev.markup"/>
2519     </a>
2520   </xsl:element>
2521 </xsl:template>
2522
2523 <!-- Remove spans from refentry TOC lines for epub3check -->
2524 <xsl:template match="d:refentry" mode="toc">
2525   <xsl:param name="toc-context" select="."/>
2526
2527   <xsl:variable name="refmeta" select=".//d:refmeta"/>
2528   <xsl:variable name="refentrytitle" select="$refmeta//d:refentrytitle"/>
2529   <xsl:variable name="refnamediv" select=".//d:refnamediv"/>
2530   <xsl:variable name="refname" select="$refnamediv//d:refname"/>
2531   <xsl:variable name="refdesc" select="$refnamediv//d:refdescriptor"/>
2532   <xsl:variable name="title">
2533     <xsl:choose>
2534       <xsl:when test="$refentrytitle">
2535         <xsl:apply-templates select="$refentrytitle[1]" mode="titleabbrev.markup"/>
2536       </xsl:when>
2537       <xsl:when test="$refdesc">
2538         <xsl:apply-templates select="$refdesc" mode="titleabbrev.markup"/>
2539       </xsl:when>
2540       <xsl:when test="$refname">
2541         <xsl:apply-templates select="$refname[1]" mode="titleabbrev.markup"/>
2542       </xsl:when>
2543     </xsl:choose>
2544   </xsl:variable>
2545
2546   <xsl:element name="{$toc.listitem.type}" namespace="http://www.w3.org/1999/xhtml">
2547     <a>
2548       <xsl:attribute name="href">
2549         <xsl:call-template name="href.target">
2550           <xsl:with-param name="toc-context" select="$toc-context"/>
2551         </xsl:call-template>
2552       </xsl:attribute>
2553       <xsl:copy-of select="$title"/>
2554       <xsl:if test="$annotate.toc != 0">
2555         <!-- * DocBook 5 says inlinemediaobject (among other things) -->
2556         <!-- * is allowed in refpurpose; so we need to run -->
2557         <!-- * apply-templates on refpurpose here, instead of value-of  -->
2558         <!-- Set allow-anchors=0 to avoid indexterms and other links -->
2559         <xsl:text> - </xsl:text>
2560         <xsl:apply-templates select="d:refnamediv/d:refpurpose" mode="no.anchor.mode"/>
2561       </xsl:if>
2562     </a>
2563   </xsl:element>
2564 </xsl:template>
2565
2566 <!-- Copy these here so relative document() open gets the correct css source -->
2567 <xsl:template name="generate.default.css.file">
2568   <xsl:if test="$make.clean.html != 0 and 
2569                 $generate.css.header = 0 and
2570                 $docbook.css.source != ''">
2571     <!-- Select default file relative to stylesheet -->
2572     <xsl:variable name="css.node" select="document($docbook.css.source)/*[1]"/>
2573
2574     <xsl:call-template name="generate.css.file">
2575       <xsl:with-param name="src" select="$docbook.css.source"/>
2576       <xsl:with-param name="css.node" select="$css.node"/>
2577     </xsl:call-template>
2578   </xsl:if>
2579 </xsl:template>
2580
2581 <xsl:template name="generate.custom.css.file">
2582   <xsl:if test="$custom.css.source != '' and
2583                 $generate.css.header = 0">
2584     <!-- Select custom file relative to document -->
2585     <xsl:variable name="css.node" select="document($custom.css.source,.)/*[1]"/>
2586
2587     <xsl:call-template name="generate.css.file">
2588       <xsl:with-param name="src" select="$custom.css.source"/>
2589       <xsl:with-param name="css.node" select="$css.node"/>
2590     </xsl:call-template>
2591   </xsl:if>
2592 </xsl:template>
2593
2594 </xsl:stylesheet>