missing anchors in book html
[librarian.git] / librarian / xslt / book2html.xslt
1 <?xml version="1.0" encoding="utf-8"?>
2 <!--
3
4    This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
5    Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6
7 -->
8 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
9     xmlns:wl="http://wolnelektury.pl/functions"
10     xmlns:dc="http://purl.org/dc/elements/1.1/" >
11
12 <xsl:output encoding="utf-8" indent="yes" omit-xml-declaration = "yes" version="2.0" />
13 <xsl:strip-space  elements="opowiadanie powiesc dramat_wierszowany_l dramat_wierszowany_lp dramat_wspolczesny liryka_l liryka_lp wywiad"/>
14 <xsl:template match="utwor">
15     <xsl:choose>
16         <xsl:when test="@full-page">
17             <html>
18             <head>
19                 <title>Książka z serwisu WolneLektury.pl</title>
20                 <meta charset="utf-8" />
21                 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
22                 <link href="https://static.wolnelektury.pl/css/compressed/book_text.css" rel="stylesheet" type="text/css" />
23             </head>
24             <body>
25                 <xsl:call-template name="book-text" />
26             </body>
27             </html>
28         </xsl:when>
29         <xsl:otherwise>
30             <xsl:call-template name="book-text" />
31         </xsl:otherwise>
32     </xsl:choose>
33 </xsl:template>
34
35 <xsl:template name="book-text">
36     <div id="book-text">
37         <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" />
38         <xsl:if test="count(descendant::*[self::pe or self::pa or self::pr or self::pt][not(parent::extra)])">
39             <div id="footnotes">
40                 <h3>Przypisy</h3>
41                 <xsl:for-each select="descendant::*[self::pe or self::pa or self::pr or self::pt][not(parent::extra)]">
42                     <div>
43                         <xsl:attribute name="class">fn-<xsl:value-of select="name()" /></xsl:attribute>
44                         <a name="{concat('footnote-', generate-id(.))}" />
45                         <a href="{concat('#anchor-', generate-id(.))}" class="annotation">[<xsl:number value="count(preceding::*[self::pa or self::pe or self::pr or self::pt]) + 1" />]</a>
46                         <xsl:choose>
47                             <xsl:when test="count(akap|akap_cd|strofa) = 0">
48                                 <p><xsl:apply-templates select="text()|*" mode="inline" />
49                                 <xsl:if test="name()='pa'"> [przypis autorski]</xsl:if>
50                                 <xsl:if test="name()='pt'"> [przypis tłumacza]</xsl:if>
51                                 <xsl:if test="name()='pr'"> [przypis redakcyjny]</xsl:if>
52                                 <xsl:if test="name()='pe'"> [przypis edytorski]</xsl:if>
53                                 </p>
54                             </xsl:when>
55                             <xsl:otherwise>
56                                 <xsl:apply-templates select="text()|*" mode="inline" />
57                             </xsl:otherwise>
58                         </xsl:choose>
59                     </div>
60                 </xsl:for-each>
61             </div>
62         </xsl:if>
63     </div>
64 </xsl:template>
65
66
67 <!-- ============================================================================== -->
68 <!-- = MASTER TAG                                                                 = -->
69 <!-- = (can contain block tags, paragraph tags, standalone tags and special tags) = -->
70 <!-- ============================================================================== -->
71 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny">
72     <xsl:apply-templates select="nota_red" mode="special" />
73     <xsl:if test="nazwa_utworu">
74         <h1>
75             <xsl:apply-templates select="autor_utworu|dzielo_nadrzedne|nazwa_utworu|podtytul" mode="header" />
76             <xsl:call-template name="translators" />
77         </h1>
78     </xsl:if>
79     <xsl:apply-templates />
80 </xsl:template>
81
82
83 <!-- ==================================================================================== -->
84 <!-- = BLOCK TAGS                                                                       = -->
85 <!-- = (can contain other block tags, paragraph tags, standalone tags and special tags) = -->
86 <!-- ==================================================================================== -->
87 <xsl:template match="nota">
88     <div class="note"><xsl:apply-templates /></div>
89 </xsl:template>
90
91 <xsl:template match="lista_osob">
92     <xsl:call-template name="section-anchor"/>
93     <div class="person-list">
94         <h3><xsl:value-of select="naglowek_listy" /></h3>
95         <ol>
96             <xsl:apply-templates select="lista_osoba" />
97         </ol>
98     </div>
99 </xsl:template>
100
101 <xsl:template match="dedykacja">
102     <div class="dedication"><xsl:apply-templates /></div>
103 </xsl:template>
104
105 <xsl:template match="ramka">
106     <div class="frame"><xsl:apply-templates /></div>
107 </xsl:template>
108
109 <xsl:template match="kwestia">
110     <div class="kwestia">
111         <xsl:apply-templates select="strofa|akap|didaskalia" />
112     </div>
113 </xsl:template>
114
115 <xsl:template match="dlugi_cytat|poezja_cyt">
116     <blockquote><xsl:apply-templates /></blockquote>
117 </xsl:template>
118
119 <xsl:template match="motto">
120     <xsl:call-template name="section-anchor"/>
121     <div class="motto"><xsl:apply-templates /></div>
122 </xsl:template>
123
124 <xsl:template match="ilustr" mode="inline">
125     <xsl:apply-templates select="."/>
126 </xsl:template>
127
128 <xsl:template match="ilustr">
129     <img>
130         <xsl:attribute name="src">
131             <xsl:value-of select="$gallery" /><xsl:value-of select="@src" />
132         </xsl:attribute>
133         <xsl:attribute name="alt">
134             <xsl:value-of select="@alt" />
135         </xsl:attribute>
136         <xsl:attribute name="title">
137             <xsl:value-of select="@alt" />
138         </xsl:attribute>
139     </img>
140 </xsl:template>
141
142
143 <!-- ========================================== -->
144 <!-- = PARAGRAPH TAGS                         = -->
145 <!-- = (can contain inline and special tags)  = -->
146 <!-- ========================================== -->
147 <!-- Title page -->
148 <xsl:template match="autor_utworu" mode="header">
149     <span class="author"><xsl:apply-templates mode="inline" /></span>
150 </xsl:template>
151
152 <xsl:template match="nazwa_utworu" mode="header">
153     <span class="title"><xsl:apply-templates mode="inline" /></span>
154 </xsl:template>
155
156 <xsl:template match="dzielo_nadrzedne" mode="header">
157     <span class="collection"><xsl:apply-templates mode="inline" /></span>
158 </xsl:template>
159
160 <xsl:template match="podtytul" mode="header">
161     <span class="subtitle"><xsl:apply-templates mode="inline" /></span>
162 </xsl:template>
163
164 <!-- Section headers (included in index)-->
165 <xsl:template match="naglowek_akt|naglowek_czesc|srodtytul">
166   <xsl:call-template name="section-anchor"/>
167     <h2><xsl:apply-templates mode="inline" /></h2>
168 </xsl:template>
169
170 <xsl:template match="naglowek_scena|naglowek_rozdzial">
171     <xsl:call-template name="section-anchor"/>
172     <h3><xsl:apply-templates mode="inline" /></h3>
173 </xsl:template>
174
175 <xsl:template match="naglowek_osoba|naglowek_podrozdzial">
176       <xsl:call-template name="section-anchor"/>
177     <h4><xsl:apply-templates mode="inline" /></h4>
178 </xsl:template>
179
180 <!-- Other paragraph tags -->
181 <xsl:template match="miejsce_czas">
182       <xsl:call-template name="section-anchor"/>
183     <p class="place-and-time"><xsl:apply-templates mode="inline" /></p>
184 </xsl:template>
185
186 <xsl:template match="didaskalia">
187       <xsl:call-template name="section-anchor"/>
188     <div class="didaskalia"><xsl:apply-templates mode="inline" /></div>
189 </xsl:template>
190
191 <xsl:template match="lista_osoba">
192     <li><xsl:apply-templates mode="inline" /></li>
193 </xsl:template>
194
195 <xsl:template match="akap|akap_dialog|akap_cd">
196     <p class="paragraph">
197       <xsl:call-template name="section-anchor"/>
198         <xsl:apply-templates mode="inline" />
199     </p>
200 </xsl:template>
201
202 <xsl:template match="strofa" mode="inline">
203     <xsl:apply-templates select="." />
204 </xsl:template>
205
206 <xsl:template match="strofa">
207     <div class="stanza">
208       <xsl:call-template name="section-anchor"/>
209         <xsl:choose>
210             <xsl:when test="count(br) > 0">
211                 <xsl:call-template name="verse">
212                     <xsl:with-param name="verse-content" select="br[1]/preceding-sibling::text() | br[1]/preceding-sibling::node()" />
213                     <xsl:with-param name="verse-type" select="br[1]/preceding-sibling::*[name() = 'wers_wciety' or name() = 'wers_akap' or name() = 'wers_cd' or name() = 'wers_do_prawej'][1]" />
214                 </xsl:call-template>
215                 <xsl:for-each select="br">              
216                                 <!-- Each BR tag "consumes" text after it -->
217                     <xsl:variable name="lnum" select="count(preceding-sibling::br)" />
218                     <xsl:call-template name="verse">
219                         <xsl:with-param name="verse-content"
220                             select="following-sibling::text()[count(preceding-sibling::br) = $lnum+1] | following-sibling::node()[count(preceding-sibling::br) = $lnum+1]" />
221                         <xsl:with-param name="verse-type" select="following-sibling::*[count(preceding-sibling::br) = $lnum+1 and (name() = 'wers_wciety' or name() = 'wers_akap' or name() = 'wers_cd' or name() = 'wers_do_prawej')][1]" />
222                     </xsl:call-template>
223                 </xsl:for-each>
224             </xsl:when>
225             <xsl:otherwise>
226                 <xsl:call-template name="verse">
227                     <xsl:with-param name="verse-content" select="text() | node()" />
228                     <xsl:with-param name="verse-type" select="wers_wciety|wers_akap|wers_cd|wers_do_prawej[1]" />
229                  </xsl:call-template>
230             </xsl:otherwise>
231         </xsl:choose>
232     </div>
233 </xsl:template>
234
235 <xsl:template name="verse">
236     <xsl:param name="verse-content" />
237     <xsl:param name="verse-type" />
238     <p class="verse">
239         <xsl:choose>
240             <xsl:when test="name($verse-type) = 'wers_akap'">
241                 <xsl:attribute name="style">padding-left: 1em</xsl:attribute>
242             </xsl:when>
243             <xsl:when test="name($verse-type) = 'wers_wciety'">
244                 <xsl:choose>
245                     <xsl:when test="$verse-content/@typ">
246                         <xsl:attribute name="style">padding-left: <xsl:value-of select="$verse-content/@typ" />em</xsl:attribute>
247                     </xsl:when>
248                     <xsl:otherwise>
249                         <xsl:attribute name="style">padding-left: 1em</xsl:attribute>
250                     </xsl:otherwise>
251                 </xsl:choose>
252             </xsl:when>
253             <xsl:when test="name($verse-type) = 'wers_cd'">
254                 <xsl:attribute name="style">padding-left: 12em</xsl:attribute>
255             </xsl:when>
256             <xsl:when test="name($verse-type) = 'wers_do_prawej'">
257                 <xsl:attribute name="style">text-align: right</xsl:attribute>
258             </xsl:when>
259         </xsl:choose>
260         <xsl:apply-templates select="$verse-content" mode="inline" />
261     </p>
262 </xsl:template>
263
264 <xsl:template match="motto_podpis">
265     <xsl:call-template name="section-anchor"/>
266     <p class="motto_podpis"><xsl:apply-templates mode="inline" /></p>
267 </xsl:template>
268
269 <xsl:template match="tabela|tabelka">
270     <xsl:call-template name="section-anchor"/>
271     <xsl:choose>
272         <xsl:when test="@ramka = '1'">
273             <table class="border"><xsl:apply-templates /></table>
274         </xsl:when>
275         <xsl:otherwise>
276             <table><xsl:apply-templates /></table>
277         </xsl:otherwise>
278     </xsl:choose>
279 </xsl:template>
280 <xsl:template match="wiersz">
281     <tr><xsl:apply-templates /></tr>
282 </xsl:template>
283 <xsl:template match="kol">
284     <td><xsl:apply-templates mode="inline" /></td>
285 </xsl:template>
286
287 <xsl:template match="mat">
288     <math xmlns="http://www.w3.org/1998/Math/MathML"><xsl:copy-of select="*" /></math>
289 </xsl:template>
290
291
292 <!-- ================================================ -->
293 <!-- = INLINE TAGS                                  = -->
294 <!-- = (contain other inline tags and special tags) = -->
295 <!-- ================================================ -->
296 <!-- Annotations -->
297 <xsl:template match="pa|pe|pr|pt" mode="inline">
298     <a name="{concat('anchor-', generate-id(.))}" />
299     <a href="{concat('#footnote-', generate-id(.))}" class="annotation">[<xsl:number value="count(preceding::*[self::pa or self::pe or self::pr or self::pt]) + 1" />]</a>
300 </xsl:template>
301
302 <!-- Other inline tags -->
303 <xsl:template match="mat" mode="inline">
304     <math xmlns="http://www.w3.org/1998/Math/MathML"><xsl:copy-of select="*" /></math>
305 </xsl:template>
306
307 <xsl:template match="didask_tekst" mode="inline">
308     <em class="didaskalia"><xsl:apply-templates mode="inline" /></em>
309 </xsl:template>
310
311 <xsl:template match="slowo_obce" mode="inline">
312     <em class="foreign-word"><xsl:apply-templates mode="inline" /></em>
313 </xsl:template>
314
315 <xsl:template match="tytul_dziela" mode="inline">
316     <em class="book-title">
317         <xsl:if test="@typ = '1'">„</xsl:if><xsl:apply-templates mode="inline" /><xsl:if test="@typ = '1'">”</xsl:if>
318     </em>
319 </xsl:template>
320
321 <xsl:template match="wyroznienie" mode="inline">
322     <em class="author-emphasis"><xsl:apply-templates mode="inline" /></em>
323 </xsl:template>
324
325 <xsl:template match="wieksze_odstepy" mode="inline">
326     <em class="wieksze-odstepy"><xsl:apply-templates mode="inline" /></em>
327 </xsl:template>
328
329 <xsl:template match="indeks_dolny" mode="inline">
330     <sub><xsl:apply-templates mode="inline" /></sub>
331 </xsl:template>
332
333 <xsl:template match="osoba" mode="inline">
334     <em class="person"><xsl:apply-templates mode="inline" /></em>
335 </xsl:template>
336
337
338 <!-- ============================================== -->
339 <!-- = STANDALONE TAGS                            = -->
340 <!-- = (cannot contain any other tags)            = -->
341 <!-- ============================================== -->
342 <xsl:template match="sekcja_swiatlo">
343     <hr class="spacer" />
344 </xsl:template>
345
346 <xsl:template match="sekcja_asterysk">
347     <p class="spacer-asterisk">*</p>
348 </xsl:template>
349
350 <xsl:template match="separator_linia">
351     <hr class="spacer-line" />
352 </xsl:template>
353
354
355 <!-- ================ -->
356 <!-- = SPECIAL TAGS = -->
357 <!-- ================ -->
358 <!-- Themes -->
359 <xsl:template match="begin" mode="inline">
360     <xsl:variable name="mnum" select="concat('m', substring(@id, 2))" />
361     <a name="m{substring(@id, 2)}" class="theme-begin" fid="{substring(@id, 2)}">
362         <xsl:value-of select="string(following::motyw[@id=$mnum]/text())" />
363     </a>
364 </xsl:template>
365
366 <xsl:template match="end" mode="inline">
367     <span class="theme-end" fid="{substring(@id, 2)}"> </span>
368 </xsl:template>
369
370 <xsl:template match="begin|end">
371     <xsl:apply-templates select='.' mode="inline" />
372 </xsl:template>
373
374 <xsl:template match="motyw" mode="inline" />
375
376
377 <xsl:template match="nota_red" mode="special">
378     <div id="nota_red">
379         <xsl:apply-templates />
380     </div>
381 </xsl:template>
382
383
384 <xsl:template name="translators">
385     <xsl:if test="//dc:contributor.translator">
386         <span class="translator">
387             <xsl:text>tłum. </xsl:text>
388             <xsl:for-each select="//dc:contributor.translator">
389                 <xsl:if test="position() != 1">, </xsl:if>
390                 <xsl:apply-templates mode="person" />
391             </xsl:for-each>
392         </span>
393     </xsl:if>
394 </xsl:template>
395
396 <xsl:template match="text()" mode="person">
397     <xsl:value-of select="wl:person_name(.)" />
398 </xsl:template>
399
400
401 <!-- ================ -->
402 <!-- = IGNORED TAGS = -->
403 <!-- ================ -->
404 <xsl:template match="extra|uwaga" />
405 <xsl:template match="extra|uwaga" mode="inline" />
406
407 <xsl:template match="nota_red" />
408 <xsl:template match="abstrakt" />
409
410 <!-- ======== -->
411 <!-- = TEXT = -->
412 <!-- ======== -->
413 <xsl:template match="text()" />
414 <xsl:template match="text()" mode="inline">
415     <xsl:value-of select="wl:substitute_entities(.)" />
416 </xsl:template>
417
418 <!-- ========= -->
419 <!-- = utils = -->
420 <!-- ========= -->
421 <xsl:template name="section-anchor">
422   <!-- 
423        this formula works as follows:
424        - get all ancestors including self
425        - choose the header (third one from root): utwor/book-type/header
426        - get all preceding siblings
427        - count them
428        - create an <a name="sec123"/> tag.
429   -->
430         <a name="{concat('sec', count(ancestor-or-self::*[last()-2]/preceding-sibling::*) + 1)}" />
431 </xsl:template>
432
433 </xsl:stylesheet>