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