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