tabs and centered verses
[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 />
118                               <p>
119                                 <xsl:if test="name()='pa'"> [przypis autorski]</xsl:if>
120                                 <xsl:if test="name()='pt'"> [przypis tłumacza]</xsl:if>
121                                 <xsl:if test="name()='pr'"> [przypis redakcyjny]</xsl:if>
122                                 <xsl:if test="name()='pe'"> [przypis edytorski]</xsl:if>
123                               </p>
124                             </xsl:otherwise>
125                         </xsl:choose>
126                     </div>
127                 </xsl:for-each>
128             </div>
129         </xsl:if>
130     </div>
131 </xsl:template>
132
133
134 <!-- ============================================================================== -->
135 <!-- = MASTER TAG                                                                 = -->
136 <!-- = (can contain block tags, paragraph tags, standalone tags and special tags) = -->
137 <!-- ============================================================================== -->
138 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny">
139     <xsl:apply-templates select="nota_red" mode="special" />
140     <xsl:if test="nazwa_utworu">
141         <h1>
142             <xsl:apply-templates select="autor_utworu|dzielo_nadrzedne|nazwa_utworu|podtytul" mode="header" />
143             <xsl:call-template name="translators" />
144         </h1>
145     </xsl:if>
146     <xsl:apply-templates />
147 </xsl:template>
148
149
150 <!-- ==================================================================================== -->
151 <!-- = BLOCK TAGS                                                                       = -->
152 <!-- = (can contain other block tags, paragraph tags, standalone tags and special tags) = -->
153 <!-- ==================================================================================== -->
154 <xsl:template match="nota">
155     <div class="note"><xsl:apply-templates /></div>
156 </xsl:template>
157
158 <xsl:template match="lista_osob">
159     <xsl:call-template name="section-anchor"/>
160     <div class="person-list">
161         <h3><xsl:value-of select="naglowek_listy" /></h3>
162         <ol>
163             <xsl:apply-templates select="lista_osoba" />
164         </ol>
165     </div>
166 </xsl:template>
167
168 <xsl:template match="dedykacja">
169     <div class="dedication"><xsl:apply-templates /></div>
170 </xsl:template>
171
172 <xsl:template match="ramka">
173     <div class="frame"><xsl:apply-templates /></div>
174 </xsl:template>
175
176 <xsl:template match="kwestia">
177     <div class="kwestia">
178         <xsl:apply-templates select="strofa|akap|didaskalia" />
179     </div>
180 </xsl:template>
181
182 <xsl:template match="dlugi_cytat|poezja_cyt">
183     <blockquote><xsl:apply-templates /></blockquote>
184 </xsl:template>
185
186 <xsl:template match="motto">
187     <xsl:call-template name="section-anchor"/>
188     <div class="motto"><xsl:apply-templates /></div>
189 </xsl:template>
190
191 <xsl:template match="ilustr" mode="inline">
192     <xsl:apply-templates select="."/>
193 </xsl:template>
194
195 <xsl:template match="ilustr">
196   <div>
197
198     <xsl:attribute name="class">
199       <xsl:text>ilustr </xsl:text>
200       <xsl:value-of select="@wyrownanie"/>
201       <xsl:if test="@oblew">
202         <xsl:text> oblew</xsl:text>
203       </xsl:if>
204     </xsl:attribute>
205     
206     <img>
207       <xsl:attribute name="src">
208         <xsl:value-of select="@src" />
209       </xsl:attribute>
210       <xsl:attribute name="srcset">
211         <xsl:value-of select="@srcset" />
212       </xsl:attribute>
213       <xsl:attribute name="sizes">
214         (min-width: 718px) 600px,
215         (min-width: 600px) calc(100vw - 118px),
216         (min-width: 320px) calc(100vw - 75px),
217         (min-width: 15em) calc(100wv - 60px),
218         calc(100wv - 40px)
219       </xsl:attribute>
220       <xsl:attribute name="alt">
221         <xsl:value-of select="@alt" />
222       </xsl:attribute>
223       <xsl:attribute name="title">
224         <xsl:value-of select="@alt" />
225       </xsl:attribute>
226
227       <xsl:if test="@szer">
228         <xsl:attribute name="style">
229           <xsl:text>width: </xsl:text>
230           <xsl:value-of select="@szer"/>
231         </xsl:attribute>
232       </xsl:if>
233     </img>
234
235     <div class="stop"></div>
236   </div>
237 </xsl:template>
238
239 <xsl:template match="animacja">
240   <div class="animacja cycle-slideshow" data-cycle-pause-on-hover="true" data-cycle-next="> img" data-cycle-fx="fadeout" data-cycle-paused="true">
241     <xsl:apply-templates/>
242   </div>
243 </xsl:template>
244
245
246 <!-- ========================================== -->
247 <!-- = PARAGRAPH TAGS                         = -->
248 <!-- = (can contain inline and special tags)  = -->
249 <!-- ========================================== -->
250 <!-- Title page -->
251 <xsl:template match="autor_utworu" mode="header">
252     <span class="author"><xsl:apply-templates mode="inline" /></span>
253 </xsl:template>
254
255 <xsl:template match="nazwa_utworu" mode="header">
256     <span class="title"><xsl:apply-templates mode="inline" /></span>
257 </xsl:template>
258
259 <xsl:template match="dzielo_nadrzedne" mode="header">
260     <span class="collection"><xsl:apply-templates mode="inline" /></span>
261 </xsl:template>
262
263 <xsl:template match="podtytul" mode="header">
264     <span class="subtitle"><xsl:apply-templates mode="inline" /></span>
265 </xsl:template>
266
267 <!-- Section headers (included in index)-->
268 <xsl:template match="naglowek_akt|naglowek_czesc|srodtytul">
269   <xsl:call-template name="section-anchor"/>
270     <h2><xsl:apply-templates mode="inline" /></h2>
271 </xsl:template>
272
273 <xsl:template match="podtytul_akt|podtytul_czesc">
274   <div class="subtitle2">
275     <xsl:apply-templates mode="inline" />
276   </div>
277 </xsl:template>
278
279 <xsl:template match="naglowek_scena|naglowek_rozdzial">
280     <xsl:call-template name="section-anchor"/>
281     <h3><xsl:apply-templates mode="inline" /></h3>
282 </xsl:template>
283
284 <xsl:template match="podtytul_scena|podtytul_rozdzial">
285   <div class="subtitle3">
286     <xsl:apply-templates mode="inline" />
287   </div>
288 </xsl:template>
289
290 <xsl:template match="naglowek_osoba|naglowek_podrozdzial">
291       <xsl:call-template name="section-anchor"/>
292     <h4><xsl:apply-templates mode="inline" /></h4>
293 </xsl:template>
294
295 <xsl:template match="podtytul_podrozdzial">
296   <div class="subtitle4">
297     <xsl:apply-templates mode="inline" />
298   </div>
299 </xsl:template>
300
301 <!-- Other paragraph tags -->
302 <xsl:template match="miejsce_czas">
303       <xsl:call-template name="section-anchor"/>
304     <p class="place-and-time"><xsl:apply-templates mode="inline" /></p>
305 </xsl:template>
306
307 <xsl:template match="didaskalia">
308       <xsl:call-template name="section-anchor"/>
309     <div class="didaskalia"><xsl:apply-templates mode="inline" /></div>
310 </xsl:template>
311
312 <xsl:template match="lista_osoba">
313     <li><xsl:apply-templates mode="inline" /></li>
314 </xsl:template>
315
316 <xsl:template match="akap|akap_dialog|akap_cd">
317     <p class="paragraph">
318       <xsl:call-template name="section-anchor"/>
319         <xsl:apply-templates mode="inline" />
320     </p>
321 </xsl:template>
322
323 <xsl:template match="strofa" mode="inline">
324     <xsl:apply-templates select="." />
325 </xsl:template>
326
327 <xsl:template match="strofa">
328     <div class="stanza">
329       <xsl:call-template name="section-anchor"/>
330         <xsl:choose>
331             <xsl:when test="count(br) > 0">
332                 <xsl:call-template name="verse">
333                     <xsl:with-param name="verse-content" select="br[1]/preceding-sibling::text() | br[1]/preceding-sibling::node()" />
334                     <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' or name() = 'wers_srodek'][1]" />
335                 </xsl:call-template>
336                 <xsl:for-each select="br">              
337                                 <!-- Each BR tag "consumes" text after it -->
338                     <xsl:variable name="lnum" select="count(preceding-sibling::br)" />
339                     <xsl:call-template name="verse">
340                         <xsl:with-param name="verse-content"
341                             select="following-sibling::text()[count(preceding-sibling::br) = $lnum+1] | following-sibling::node()[count(preceding-sibling::br) = $lnum+1]" />
342                         <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' or name() = 'wers_srodek')][1]" />
343                     </xsl:call-template>
344                 </xsl:for-each>
345             </xsl:when>
346             <xsl:otherwise>
347                 <xsl:call-template name="verse">
348                     <xsl:with-param name="verse-content" select="text() | node()" />
349                     <xsl:with-param name="verse-type" select="wers_wciety|wers_akap|wers_cd|wers_do_prawej|wers_srodek[1]" />
350                  </xsl:call-template>
351             </xsl:otherwise>
352         </xsl:choose>
353     </div>
354 </xsl:template>
355
356 <xsl:template name="verse">
357     <xsl:param name="verse-content" />
358     <xsl:param name="verse-type" />
359     <div class="verse">
360         <xsl:choose>
361             <xsl:when test="name($verse-type) = 'wers_akap'">
362                 <xsl:attribute name="style">padding-left: 1em</xsl:attribute>
363             </xsl:when>
364             <xsl:when test="name($verse-type) = 'wers_wciety'">
365                 <xsl:choose>
366                     <xsl:when test="$verse-content/@typ">
367                         <xsl:attribute name="style">padding-left: <xsl:value-of select="$verse-content/@typ" />em</xsl:attribute>
368                     </xsl:when>
369                     <xsl:otherwise>
370                         <xsl:attribute name="style">padding-left: 1em</xsl:attribute>
371                     </xsl:otherwise>
372                 </xsl:choose>
373             </xsl:when>
374             <xsl:when test="name($verse-type) = 'wers_cd'">
375                 <xsl:attribute name="style">padding-left: 12em</xsl:attribute>
376             </xsl:when>
377             <xsl:when test="name($verse-type) = 'wers_do_prawej'">
378                 <xsl:attribute name="style">text-align: right</xsl:attribute>
379             </xsl:when>
380             <xsl:when test="name($verse-type) = 'wers_srodek'">
381                 <xsl:attribute name="style">text-align: center</xsl:attribute>
382             </xsl:when>
383         </xsl:choose>
384         <xsl:apply-templates select="$verse-content" mode="inline" />
385     </div>
386 </xsl:template>
387
388 <xsl:template match="motto_podpis">
389     <xsl:call-template name="section-anchor"/>
390     <p class="motto_podpis"><xsl:apply-templates mode="inline" /></p>
391 </xsl:template>
392
393 <xsl:template match="tabela|tabelka">
394     <xsl:call-template name="section-anchor"/>
395     <xsl:choose>
396         <xsl:when test="@ramka = '1'">
397             <table class="border"><xsl:apply-templates /></table>
398         </xsl:when>
399         <xsl:otherwise>
400             <table><xsl:apply-templates /></table>
401         </xsl:otherwise>
402     </xsl:choose>
403 </xsl:template>
404 <xsl:template match="wiersz">
405     <tr><xsl:apply-templates /></tr>
406 </xsl:template>
407 <xsl:template match="kol">
408     <td><xsl:apply-templates mode="inline" /></td>
409 </xsl:template>
410
411 <xsl:template match="mat">
412     <math xmlns="http://www.w3.org/1998/Math/MathML"><xsl:copy-of select="*" /></math>
413 </xsl:template>
414
415
416 <!-- ================================================ -->
417 <!-- = INLINE TAGS                                  = -->
418 <!-- = (contain other inline tags and special tags) = -->
419 <!-- ================================================ -->
420 <!-- Annotations -->
421 <xsl:template match="pa|pe|pr|pt" mode="inline">
422     <a name="{concat('anchor-', generate-id(.))}" />
423     <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>
424 </xsl:template>
425
426 <xsl:template match="ref" mode="inline">
427   <a class="reference" data-uri="">
428     <xsl:attribute name="data-uri">
429       <xsl:value-of select="@href"/>
430     </xsl:attribute>
431   </a>
432 </xsl:template>
433
434 <!-- Other inline tags -->
435 <xsl:template match="mat" mode="inline">
436     <math xmlns="http://www.w3.org/1998/Math/MathML"><xsl:copy-of select="*|text()" /></math>
437 </xsl:template>
438
439 <xsl:template match="didask_tekst" mode="inline">
440     <em class="didaskalia"><xsl:apply-templates mode="inline" /></em>
441 </xsl:template>
442
443 <xsl:template match="slowo_obce" mode="inline">
444     <em class="foreign-word"><xsl:apply-templates mode="inline" /></em>
445 </xsl:template>
446
447 <xsl:template match="tytul_dziela" mode="inline">
448     <em class="book-title">
449         <xsl:if test="@typ = '1'">„</xsl:if><xsl:apply-templates mode="inline" /><xsl:if test="@typ = '1'">”</xsl:if>
450     </em>
451 </xsl:template>
452
453 <xsl:template match="wyroznienie" mode="inline">
454     <em class="author-emphasis"><xsl:apply-templates mode="inline" /></em>
455 </xsl:template>
456
457 <xsl:template match="wieksze_odstepy" mode="inline">
458     <em class="wieksze-odstepy"><xsl:apply-templates mode="inline" /></em>
459 </xsl:template>
460
461 <xsl:template match="indeks_dolny" mode="inline">
462     <sub><xsl:apply-templates mode="inline" /></sub>
463 </xsl:template>
464
465 <xsl:template match="osoba" mode="inline">
466     <em class="person"><xsl:apply-templates mode="inline" /></em>
467 </xsl:template>
468
469 <xsl:template match="www" mode="inline">
470     <a target="_blank">
471         <xsl:attribute name="href">
472             <xsl:value-of select="text()"/>
473         </xsl:attribute>
474         <xsl:apply-templates mode="inline" />
475     </a>
476 </xsl:template>
477
478 <!-- ============================================== -->
479 <!-- = STANDALONE TAGS                            = -->
480 <!-- = (cannot contain any other tags)            = -->
481 <!-- ============================================== -->
482 <xsl:template match="sekcja_swiatlo">
483     <hr class="spacer" />
484 </xsl:template>
485
486 <xsl:template match="sekcja_asterysk">
487     <p class="spacer-asterisk">*</p>
488 </xsl:template>
489
490 <xsl:template match="separator_linia">
491     <hr class="spacer-line" />
492 </xsl:template>
493
494
495 <!-- ================ -->
496 <!-- = SPECIAL TAGS = -->
497 <!-- ================ -->
498 <!-- Themes -->
499 <xsl:template match="begin" mode="inline">
500     <xsl:variable name="mnum" select="concat('m', substring(@id, 2))" />
501     <a name="m{substring(@id, 2)}" class="theme-begin" fid="{substring(@id, 2)}">
502         <xsl:value-of select="string(following::motyw[@id=$mnum]/text())" />
503     </a>
504 </xsl:template>
505
506 <xsl:template match="end" mode="inline">
507     <span class="theme-end" fid="{substring(@id, 2)}"> </span>
508 </xsl:template>
509
510 <xsl:template match="begin|end">
511     <xsl:apply-templates select='.' mode="inline" />
512 </xsl:template>
513
514 <xsl:template match="motyw" mode="inline" />
515
516
517 <xsl:template match="nota_red" mode="special">
518     <div id="nota_red">
519         <xsl:apply-templates />
520     </div>
521 </xsl:template>
522
523
524 <xsl:template name="translators">
525     <xsl:if test="//dc:contributor.translator">
526         <span class="translator">
527             <xsl:text>tłum. </xsl:text>
528             <xsl:for-each select="//dc:contributor.translator">
529                 <xsl:if test="position() != 1">, </xsl:if>
530                 <xsl:apply-templates mode="person" />
531             </xsl:for-each>
532         </span>
533     </xsl:if>
534 </xsl:template>
535
536 <xsl:template match="text()" mode="person">
537     <xsl:value-of select="wl:person_name(.)" />
538 </xsl:template>
539
540 <xsl:template match="rownolegle">
541     <xsl:apply-templates mode="rownolegle" />
542 </xsl:template>
543 <xsl:template match="*" mode="rownolegle">
544   <!-- is it last? -->
545   <div>
546     <xsl:attribute name="class">
547       <xsl:text>rownolegly-blok</xsl:text>
548       <xsl:if test="not(following-sibling::*)">
549         <xsl:text> last</xsl:text>
550       </xsl:if>
551       <xsl:if test="not(preceding-sibling::*)">
552         <xsl:text> first</xsl:text>
553       </xsl:if>
554     </xsl:attribute>
555     <xsl:attribute name="style">
556       <xsl:text>border-left: 2px solid red; padding-left: .5em;</xsl:text>
557       <xsl:if test="not(following-sibling::*)">
558         <xsl:text> border-radius: 0 0 0 .75em;</xsl:text>
559       </xsl:if>
560       <xsl:if test="not(preceding-sibling::*)">
561         <xsl:text> border-radius: .75em 0 0 0;</xsl:text>
562       </xsl:if>
563     </xsl:attribute>
564     <xsl:apply-templates match="." />
565   </div>
566 </xsl:template>
567
568 <xsl:template match="tab" mode="inline">
569   <span>
570     <xsl:choose>
571       <xsl:when test="@szer">
572         <xsl:attribute name="style">display: inline-block; width: <xsl:value-of select="@szer" />em</xsl:attribute>
573       </xsl:when>
574       <xsl:otherwise>
575         <xsl:attribute name="style">display: inline-block; width: 1em</xsl:attribute>
576       </xsl:otherwise>
577     </xsl:choose>
578   </span>
579 </xsl:template>
580
581
582 <!-- ================ -->
583 <!-- = IGNORED TAGS = -->
584 <!-- ================ -->
585 <xsl:template match="extra|uwaga" />
586 <xsl:template match="extra|uwaga" mode="inline" />
587
588 <xsl:template match="nota_red" />
589 <xsl:template match="abstrakt" />
590
591 <!-- ======== -->
592 <!-- = TEXT = -->
593 <!-- ======== -->
594 <xsl:template match="text()" />
595 <xsl:template match="text()" mode="inline">
596     <xsl:value-of select="wl:substitute_entities(.)" />
597 </xsl:template>
598
599 <!-- ========= -->
600 <!-- = utils = -->
601 <!-- ========= -->
602 <xsl:template name="section-anchor">
603   <!-- 
604        this formula works as follows:
605        - get all ancestors including self
606        - choose the header (third one from root): utwor/book-type/header
607        - get all preceding siblings
608        - count them
609        - create an <a name="sec123"/> tag.
610   -->
611         <a name="{concat('sec', count(ancestor-or-self::*[last()-2]/preceding-sibling::*) + 1)}" />
612 </xsl:template>
613
614 <xsl:template match="numeracja">
615         <span class="numeracja">
616                 <xsl:attribute name="data-start">
617                         <xsl:value-of select="@start" />
618                 </xsl:attribute>
619         </span>
620 </xsl:template>
621
622 </xsl:stylesheet>