Merge branch '__images'
[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 http-equiv="content-type" content="text/html;charset=utf-8"/>
21             </head>
22             <style>
23                 body {
24                     font-size: 16px;
25                     font: Georgia, "Times New Roman", serif;
26                     line-height: 1.5em;
27                     margin: 0;
28                 }
29
30                 a {
31                     color: blue;
32                     text-decoration: none;
33                 }
34
35                 #book-text {
36                     margin: 3em;
37                     max-width: 36em;
38                 }
39
40                 /* ================================== */
41                 /* = Header with logo and menu      = */
42                 /* ================================== */
43                 #header {
44                     margin: 3.4em 0 0 1.4em;
45                 }
46
47                 img {
48                     border: none;
49                 }
50
51
52                 #menu {
53                     position: fixed;
54                     left: 0em;
55                     top: 0em;
56                     width: 100%;
57                     height: 1.5em;
58                     background: #333;
59                     color: #FFF;
60                     opacity: 0.9;
61                     z-index: 99;
62                 }
63
64                 #menu ul {
65                     list-style: none;
66                     padding: 0;
67                     margin: 0;
68                 }
69
70                 #menu li a {
71                     display: block;
72                     float: left;
73                     width: 7.5em;
74                     height: 1.5em;
75                     margin-left: 0.5em;
76                     text-align: center;
77                     color: #FFF;
78                 }
79
80                 #menu li a:hover, #menu li a:active {
81                     color: #000;
82                     background: #FFF url(/static/img/arrow-down.png) no-repeat center right;
83                 }
84
85                 #menu li a.selected {
86                     color: #000;
87                     background: #FFF url(/static/img/arrow-up.png) no-repeat center right;
88                 }
89                 #menu a.menu-link {
90                     display: block;
91                     float: left;
92                     height: 1.5em;
93                     margin-left: 0.5em;
94                     text-align: center;
95                     color: #FFF;
96                 }
97                 #menu span {
98                     color: #888;
99                     font-style: italic;
100                     font-size: .75em;
101                     margin-right: 0.5em;
102                 }
103
104
105                 #toc, #themes, #nota_red, #info {
106                     position: fixed;
107                     left: 0em;
108                     top: 1.5em;
109                     width: 37em;
110                     padding: 1.5em;
111                     background: #FFF;
112                     border-bottom: 0.25em solid #DDD;
113                     border-right: 0.25em solid #DDD;
114                     display: none;
115                     height: 16em;
116                     overflow-x: hidden;
117                     overflow-y: auto;
118                     opacity: 0.9;
119                     z-index: 99;
120                 }
121
122                 #toc ol, #themes ol {
123                     list-style: none;
124                     padding: 0;
125                     margin: 0;
126                 }
127
128                 #toc ol li {
129                     font-weight: bold;
130                 }
131
132                 #toc ol ol {
133                     padding: 0 0 1.5em 1.5em;
134                     margin: 0;
135                 }
136
137                 #toc ol ol li {
138                     font-weight: normal;
139                 }
140
141                 #toc h2 {
142                     display: none;
143                 }
144
145                 #toc .anchor {
146                     float: none;
147                     margin: 0;
148                     color: blue;
149                     font-size: 16px;
150                     position: inherit;
151                 }
152
153                 #info p {
154                     text-align: justify;
155                     margin: 1.5em 0 0;
156                 }
157
158                 /* =================================================== */
159                 /* = Common elements: headings, paragraphs and lines = */
160                 /* =================================================== */
161                 h1 {
162                     font-size: 3em;
163                     margin: 1.5em 0;
164                     text-align: center;
165                     line-height: 1.5em;
166                     font-weight: bold;
167                 }
168
169                 h2 {
170                     font-size: 2em;
171                     margin: 1.5em 0 0;
172                     font-weight: bold;
173                     line-height: 1.5em;
174                 }
175
176                 h3 {
177                     font-size: 1.5em;
178                     margin: 1.5em 0 0;
179                     font-weight: normal;
180                     line-height: 1.5em;
181                 }
182
183                 h4 {
184                     font-size: 1em;
185                     margin: 1.5em 0 0;
186                     line-height: 1.5em;
187                 }
188
189                 p {
190                     margin: 0;
191                 }
192
193                 /* ======================== */
194                 /* = Footnotes and themes = */
195                 /* ======================== */
196                 .theme-begin {
197                     border-left: 0.1em solid #DDDDDD;
198                     color: #777;
199                     padding: 0 0.5em;
200                     width: 7.5em;
201
202                     font-style: normal;
203                     font-weight: normal;
204                     font-variant: normal;
205                     letter-spacing: 0;
206                     text-transform: none;
207                     text-decoration: none;
208
209                     font-size: 16px;
210                     float: right;
211                     margin-right: -9.5em;
212                     margin-bottom: 0.5em;
213                     clear: both;
214                     left: 40em;
215                     line-height: 1.5em;
216                     text-align: left;
217                 }
218
219                 .annotation {
220                     font-style: normal;
221                     font-weight: normal;
222                     font-size: 12px;
223                     padding-left: 2px;
224                     position: relative;
225                     top: -4px;
226                 }
227
228                 #footnotes .annotation {
229                     display: block;
230                     float: left;
231                     width: 2.5em;
232                     clear: both;
233                 }
234
235                 #footnotes div {
236                     margin: 1.5em 0 0 0;
237                 }
238
239                 #footnotes p {
240                     margin-left: 2.5em;
241                     font-size: 0.875em;
242                 }
243
244                 blockquote {
245                     font-size: 0.875em;
246                 }
247
248                 /* ============= */
249                 /* = Numbering = */
250                 /* ============= */
251                 .verse, .paragraph {
252                     position:relative;
253                 }
254                 .anchor {
255                     position: absolute;
256                     margin: -0.25em -0.5em;
257                     left: -3em;
258                     color: #777;
259                     font-size: 12px;
260                     width: 2em;
261                     text-align: center;
262                     padding: 0.25em 0.5em;
263                     line-height: 1.5em;
264                 }
265
266                 .anchor:hover, #book-text .anchor:active {
267                     color: #FFF;
268                     background-color: #CCC;
269                 }
270
271                 /* =================== */
272                 /* = Custom elements = */
273                 /* =================== */
274                 span.author {
275                     font-size: 0.5em;
276                     display: block;
277                     line-height: 1.5em;
278                     margin-bottom: 0.25em;
279                 }
280
281                 span.collection {
282                     font-size: 0.375em;
283                     display: block;
284                     line-height: 1.5em;
285                     margin-bottom: -0.25em;
286                 }
287
288                 span.subtitle {
289                     font-size: 0.5em;
290                     display: block;
291                     line-height: 1.5em;
292                     margin-top: -0.25em;
293                 }
294
295                 span.translator {
296                     font-size: 0.375em;
297                     display: block;
298                     line-height: 1.5em;
299                     margin-top: 0.25em;
300                 }
301
302                 div.didaskalia {
303                     font-style: italic;
304                     margin: 0.5em 0 0 1.5em;
305                 }
306
307                 div.kwestia {
308                     margin: 0.5em 0 0;
309                 }
310
311                 div.stanza {
312                     margin: 1.5em 0 0;
313                 }
314
315                 div.kwestia div.stanza {
316                     margin: 0;
317                 }
318
319                 p.paragraph {
320                     text-align: justify;
321                     margin: 1.5em 0 0;
322                 }
323
324                 p.motto {
325                     text-align: justify;
326                     font-style: italic;
327                     margin: 1.5em 0 0;
328                 }
329
330                 p.motto_podpis {
331                     font-size: 0.875em;
332                     text-align: right;
333                 }
334
335                 div.fragment {
336                     border-bottom: 0.1em solid #999;
337                     padding-bottom: 1.5em;
338                 }
339
340                 div.note p, div.dedication p, div.note p.paragraph, div.dedication p.paragraph {
341                     text-align: right;
342                     font-style: italic;
343                 }
344
345                 hr.spacer {
346                     height: 3em;
347                     visibility: hidden;
348                 }
349
350                 hr.spacer-line {
351                     margin: 1.5em 0;
352                     border: none;
353                     border-bottom: 0.1em solid #000;
354                 }
355
356                 p.spacer-asterisk {
357                     padding: 0;
358                     margin: 1.5em 0;
359                     text-align: center;
360                 }
361
362                 div.person-list ol {
363                     list-style: none;
364                     padding: 0 0 0 1.5em;
365                 }
366
367                 p.place-and-time {
368                     font-style: italic;
369                 }
370
371                 em.math, em.foreign-word, em.book-title, em.didaskalia {
372                     font-style: italic;
373                 }
374
375                 em.author-emphasis {
376                     letter-spacing: 0.1em;
377                 }
378
379                 em.person {
380                     font-style: normal;
381                     font-variant: small-caps;
382                 }
383
384                 .verse:after {
385                     content: "\feff";
386                 }
387
388
389                 /* =================================== */
390                 /* = Hide some elements for printing = */
391                 /* =================================== */
392
393                 @media print {
394                     #menu {display: none;}
395                 }
396             </style>
397             <body>
398                 <xsl:call-template name="book-text" />
399             </body>
400             </html>
401         </xsl:when>
402         <xsl:otherwise>
403             <xsl:call-template name="book-text" />
404         </xsl:otherwise>
405     </xsl:choose>
406 </xsl:template>
407
408 <xsl:template name="book-text">
409     <div id="book-text">
410         <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" />
411         <xsl:if test="count(descendant::*[self::pe or self::pa or self::pr or self::pt][not(parent::extra)])">
412             <div id="footnotes">
413                 <h3>Przypisy</h3>
414                 <xsl:for-each select="descendant::*[self::pe or self::pa or self::pr or self::pt][not(parent::extra)]">
415                     <div>
416                         <a name="{concat('footnote-', generate-id(.))}" />
417                         <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>
418                         <xsl:choose>
419                             <xsl:when test="count(akap|akap_cd|strofa) = 0">
420                                 <p><xsl:apply-templates select="text()|*" mode="inline" />
421                                 <xsl:if test="name()='pa'"> [przypis autorski]</xsl:if>
422                                 </p>
423                             </xsl:when>
424                             <xsl:otherwise>
425                                 <xsl:apply-templates select="text()|*" mode="inline" />
426                             </xsl:otherwise>
427                         </xsl:choose>
428                     </div>
429                 </xsl:for-each>
430             </div>
431         </xsl:if>
432     </div>
433 </xsl:template>
434
435
436 <!-- ============================================================================== -->
437 <!-- = MASTER TAG                                                                 = -->
438 <!-- = (can contain block tags, paragraph tags, standalone tags and special tags) = -->
439 <!-- ============================================================================== -->
440 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny">
441     <xsl:apply-templates select="nota_red" mode="special" />
442     <xsl:if test="nazwa_utworu">
443         <h1>
444             <xsl:apply-templates select="autor_utworu|dzielo_nadrzedne|nazwa_utworu|podtytul" mode="header" />
445             <xsl:call-template name="translators" />
446         </h1>
447     </xsl:if>
448     <xsl:apply-templates />
449 </xsl:template>
450
451
452 <!-- ==================================================================================== -->
453 <!-- = BLOCK TAGS                                                                       = -->
454 <!-- = (can contain other block tags, paragraph tags, standalone tags and special tags) = -->
455 <!-- ==================================================================================== -->
456 <xsl:template match="nota">
457     <div class="note"><xsl:apply-templates /></div>
458 </xsl:template>
459
460 <xsl:template match="lista_osob">
461     <div class="person-list">
462         <h3><xsl:value-of select="naglowek_listy" /></h3>
463         <ol>
464             <xsl:apply-templates select="lista_osoba" />
465         </ol>
466     </div>
467 </xsl:template>
468
469 <xsl:template match="dedykacja">
470     <div class="dedication"><xsl:apply-templates /></div>
471 </xsl:template>
472
473 <xsl:template match="kwestia">
474     <div class="kwestia">
475         <xsl:apply-templates select="strofa|akap|didaskalia" />
476     </div>
477 </xsl:template>
478
479 <xsl:template match="dlugi_cytat|poezja_cyt">
480     <blockquote><xsl:apply-templates /></blockquote>
481 </xsl:template>
482
483 <xsl:template match="motto">
484     <div class="motto"><xsl:apply-templates mode="inline" /></div>
485 </xsl:template>
486
487
488 <!-- ========================================== -->
489 <!-- = PARAGRAPH TAGS                         = -->
490 <!-- = (can contain inline and special tags)  = -->
491 <!-- ========================================== -->
492 <!-- Title page -->
493 <xsl:template match="autor_utworu" mode="header">
494     <span class="author"><xsl:apply-templates mode="inline" /></span>
495 </xsl:template>
496
497 <xsl:template match="nazwa_utworu" mode="header">
498     <span class="title"><xsl:apply-templates mode="inline" /></span>
499 </xsl:template>
500
501 <xsl:template match="dzielo_nadrzedne" mode="header">
502     <span class="collection"><xsl:apply-templates mode="inline" /></span>
503 </xsl:template>
504
505 <xsl:template match="podtytul" mode="header">
506     <span class="subtitle"><xsl:apply-templates mode="inline" /></span>
507 </xsl:template>
508
509 <!-- Section headers (included in index)-->
510 <xsl:template match="naglowek_akt|naglowek_czesc|srodtytul">
511   <xsl:call-template name="section-anchor"/>
512     <h2><xsl:apply-templates mode="inline" /></h2>
513 </xsl:template>
514
515 <xsl:template match="naglowek_scena|naglowek_rozdzial">
516     <xsl:call-template name="section-anchor"/>
517     <h3><xsl:apply-templates mode="inline" /></h3>
518 </xsl:template>
519
520 <xsl:template match="naglowek_osoba|naglowek_podrozdzial">
521       <xsl:call-template name="section-anchor"/>
522     <h4><xsl:apply-templates mode="inline" /></h4>
523 </xsl:template>
524
525 <!-- Other paragraph tags -->
526 <xsl:template match="miejsce_czas">
527       <xsl:call-template name="section-anchor"/>
528     <p class="place-and-time"><xsl:apply-templates mode="inline" /></p>
529 </xsl:template>
530
531 <xsl:template match="didaskalia">
532       <xsl:call-template name="section-anchor"/>
533     <div class="didaskalia"><xsl:apply-templates mode="inline" /></div>
534 </xsl:template>
535
536 <xsl:template match="lista_osoba">
537     <li><xsl:apply-templates mode="inline" /></li>
538 </xsl:template>
539
540 <xsl:template match="akap|akap_dialog|akap_cd">
541     <p class="paragraph">
542       <xsl:call-template name="section-anchor"/>
543         <xsl:apply-templates mode="inline" />
544     </p>
545 </xsl:template>
546
547 <xsl:template match="strofa">
548     <div class="stanza">
549       <xsl:call-template name="section-anchor"/>
550         <xsl:choose>
551             <xsl:when test="count(br) > 0">
552                 <xsl:call-template name="verse">
553                     <xsl:with-param name="verse-content" select="br[1]/preceding-sibling::text() | br[1]/preceding-sibling::node()" />
554                     <xsl:with-param name="verse-type" select="br[1]/preceding-sibling::*[name() = 'wers_wciety' or name() = 'wers_akap' or name() = 'wers_cd'][1]" />
555                 </xsl:call-template>
556                 <xsl:for-each select="br">              
557                                 <!-- Each BR tag "consumes" text after it -->
558                     <xsl:variable name="lnum" select="count(preceding-sibling::br)" />
559                     <xsl:call-template name="verse">
560                         <xsl:with-param name="verse-content"
561                             select="following-sibling::text()[count(preceding-sibling::br) = $lnum+1] | following-sibling::node()[count(preceding-sibling::br) = $lnum+1]" />
562                         <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')][1]" />
563                     </xsl:call-template>
564                 </xsl:for-each>
565             </xsl:when>
566             <xsl:otherwise>
567                 <xsl:call-template name="verse">
568                     <xsl:with-param name="verse-content" select="text() | node()" />
569                     <xsl:with-param name="verse-type" select="wers_wciety|wers_akap|wers_cd[1]" />
570                  </xsl:call-template>
571             </xsl:otherwise>
572         </xsl:choose>
573     </div>
574 </xsl:template>
575
576 <xsl:template name="verse">
577     <xsl:param name="verse-content" />
578     <xsl:param name="verse-type" />
579     <p class="verse">
580         <xsl:choose>
581             <xsl:when test="name($verse-type) = 'wers_akap'">
582                 <xsl:attribute name="style">padding-left: 1em</xsl:attribute>
583             </xsl:when>
584             <xsl:when test="name($verse-type) = 'wers_wciety'">
585                 <xsl:choose>
586                     <xsl:when test="$verse-content/@typ">
587                         <xsl:attribute name="style">padding-left: <xsl:value-of select="$verse-content/@typ" />em</xsl:attribute>
588                     </xsl:when>
589                     <xsl:otherwise>
590                         <xsl:attribute name="style">padding-left: 1em</xsl:attribute>
591                     </xsl:otherwise>
592                 </xsl:choose>
593             </xsl:when>
594             <xsl:when test="name($verse-type) = 'wers_cd'">
595                 <xsl:attribute name="style">padding-left: 12em</xsl:attribute>
596             </xsl:when>
597         </xsl:choose>
598         <xsl:apply-templates select="$verse-content" mode="inline" />
599     </p>
600 </xsl:template>
601
602 <xsl:template match="motto_podpis">
603     <p class="motto_podpis"><xsl:apply-templates mode="inline" /></p>
604 </xsl:template>
605
606
607 <!-- ================================================ -->
608 <!-- = INLINE TAGS                                  = -->
609 <!-- = (contain other inline tags and special tags) = -->
610 <!-- ================================================ -->
611 <!-- Annotations -->
612 <xsl:template match="pa|pe|pr|pt" mode="inline">
613     <a name="{concat('anchor-', generate-id(.))}" />
614     <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>
615 </xsl:template>
616
617 <!-- Other inline tags -->
618 <xsl:template match="mat" mode="inline">
619     <em class="math"><xsl:apply-templates mode="inline" /></em>
620 </xsl:template>
621
622 <xsl:template match="didask_tekst" mode="inline">
623     <em class="didaskalia"><xsl:apply-templates mode="inline" /></em>
624 </xsl:template>
625
626 <xsl:template match="slowo_obce" mode="inline">
627     <em class="foreign-word"><xsl:apply-templates mode="inline" /></em>
628 </xsl:template>
629
630 <xsl:template match="tytul_dziela" mode="inline">
631     <em class="book-title">
632         <xsl:if test="@typ = '1'">„</xsl:if><xsl:apply-templates mode="inline" /><xsl:if test="@typ = '1'">”</xsl:if>
633     </em>
634 </xsl:template>
635
636 <xsl:template match="wyroznienie" mode="inline">
637     <em class="author-emphasis"><xsl:apply-templates mode="inline" /></em>
638 </xsl:template>
639
640 <xsl:template match="osoba" mode="inline">
641     <em class="person"><xsl:apply-templates mode="inline" /></em>
642 </xsl:template>
643
644
645 <!-- ============================================== -->
646 <!-- = STANDALONE TAGS                            = -->
647 <!-- = (cannot contain any other tags)            = -->
648 <!-- ============================================== -->
649 <xsl:template match="sekcja_swiatlo">
650     <hr class="spacer" />
651 </xsl:template>
652
653 <xsl:template match="sekcja_asterysk">
654     <p class="spacer-asterisk">*</p>
655 </xsl:template>
656
657 <xsl:template match="separator_linia">
658     <hr class="spacer-line" />
659 </xsl:template>
660
661
662 <!-- ================ -->
663 <!-- = SPECIAL TAGS = -->
664 <!-- ================ -->
665 <!-- Themes -->
666 <xsl:template match="begin" mode="inline">
667     <xsl:variable name="mnum" select="concat('m', substring(@id, 2))" />
668     <a name="m{substring(@id, 2)}" class="theme-begin" fid="{substring(@id, 2)}">
669         <xsl:value-of select="string(following::motyw[@id=$mnum]/text())" />
670     </a>
671 </xsl:template>
672
673 <xsl:template match="end" mode="inline">
674     <span class="theme-end" fid="{substring(@id, 2)}"> </span>
675 </xsl:template>
676
677 <xsl:template match="begin|end">
678     <xsl:apply-templates select='.' mode="inline" />
679 </xsl:template>
680
681 <xsl:template match="motyw" mode="inline" />
682
683
684 <xsl:template match="nota_red" mode="special">
685     <div id="nota_red">
686         <xsl:apply-templates />
687     </div>
688 </xsl:template>
689
690
691 <xsl:template name="translators">
692     <xsl:if test="//dc:contributor.translator">
693         <span class="translator">
694             <xsl:text>tłum. </xsl:text>
695             <xsl:for-each select="//dc:contributor.translator">
696                 <xsl:if test="position() != 1">, </xsl:if>
697                 <xsl:apply-templates mode="person" />
698             </xsl:for-each>
699         </span>
700     </xsl:if>
701 </xsl:template>
702
703 <xsl:template match="text()" mode="person">
704     <xsl:value-of select="wl:person_name(.)" />
705 </xsl:template>
706
707
708 <!-- ================ -->
709 <!-- = IGNORED TAGS = -->
710 <!-- ================ -->
711 <xsl:template match="extra|uwaga" />
712 <xsl:template match="extra|uwaga" mode="inline" />
713
714 <xsl:template match="nota_red" />
715
716 <!-- ======== -->
717 <!-- = TEXT = -->
718 <!-- ======== -->
719 <xsl:template match="text()" />
720 <xsl:template match="text()" mode="inline">
721     <xsl:value-of select="wl:substitute_entities(.)" />
722 </xsl:template>
723
724 <!-- ========= -->
725 <!-- = utils = -->
726 <!-- ========= -->
727 <xsl:template name="section-anchor">
728   <!-- 
729        this formula works as follows:
730        - get all ancestors including self
731        - choose the header (third one from root): utwor/book-type/header
732        - get all preceding siblings
733        - count them
734        - create an <a name="sec123"/> tag.
735   -->
736         <a name="{concat('sec', count(ancestor-or-self::*[last()-2]/preceding-sibling::*) + 1)}" />
737 </xsl:template>
738
739 </xsl:stylesheet>