Add swappable CSS for HTML previews.
[librarian.git] / librarian / xslt / book2html.xslt
1 <?xml version="1.0" encoding="utf-8"?>
2 <!--
3
4    This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
5    Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6
7 -->
8 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
9     xmlns:wl="http://wolnelektury.pl/functions"
10     xmlns:dc="http://purl.org/dc/elements/1.1/" >
11
12 <xsl:output encoding="utf-8" indent="yes" omit-xml-declaration = "yes" version="2.0" />
13 <xsl:strip-space  elements="opowiadanie powiesc dramat_wierszowany_l dramat_wierszowany_lp dramat_wspolczesny liryka_l liryka_lp wywiad"/>
14 <xsl:template match="utwor">
15     <xsl:choose>
16         <xsl:when test="@full-page">
17             <html>
18             <head>
19                 <title>Książka z serwisu WolneLektury.pl</title>
20                 <meta charset="utf-8" />
21                 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
22                 <link 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="naglowek_scena|naglowek_rozdzial">
184     <xsl:call-template name="section-anchor"/>
185     <h3><xsl:apply-templates mode="inline" /></h3>
186 </xsl:template>
187
188 <xsl:template match="naglowek_osoba|naglowek_podrozdzial">
189       <xsl:call-template name="section-anchor"/>
190     <h4><xsl:apply-templates mode="inline" /></h4>
191 </xsl:template>
192
193 <!-- Other paragraph tags -->
194 <xsl:template match="miejsce_czas">
195       <xsl:call-template name="section-anchor"/>
196     <p class="place-and-time"><xsl:apply-templates mode="inline" /></p>
197 </xsl:template>
198
199 <xsl:template match="didaskalia">
200       <xsl:call-template name="section-anchor"/>
201     <div class="didaskalia"><xsl:apply-templates mode="inline" /></div>
202 </xsl:template>
203
204 <xsl:template match="lista_osoba">
205     <li><xsl:apply-templates mode="inline" /></li>
206 </xsl:template>
207
208 <xsl:template match="akap|akap_dialog|akap_cd">
209     <p class="paragraph">
210       <xsl:call-template name="section-anchor"/>
211         <xsl:apply-templates mode="inline" />
212     </p>
213 </xsl:template>
214
215 <xsl:template match="strofa" mode="inline">
216     <xsl:apply-templates select="." />
217 </xsl:template>
218
219 <xsl:template match="strofa">
220     <div class="stanza">
221       <xsl:call-template name="section-anchor"/>
222         <xsl:choose>
223             <xsl:when test="count(br) > 0">
224                 <xsl:call-template name="verse">
225                     <xsl:with-param name="verse-content" select="br[1]/preceding-sibling::text() | br[1]/preceding-sibling::node()" />
226                     <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]" />
227                 </xsl:call-template>
228                 <xsl:for-each select="br">              
229                                 <!-- Each BR tag "consumes" text after it -->
230                     <xsl:variable name="lnum" select="count(preceding-sibling::br)" />
231                     <xsl:call-template name="verse">
232                         <xsl:with-param name="verse-content"
233                             select="following-sibling::text()[count(preceding-sibling::br) = $lnum+1] | following-sibling::node()[count(preceding-sibling::br) = $lnum+1]" />
234                         <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]" />
235                     </xsl:call-template>
236                 </xsl:for-each>
237             </xsl:when>
238             <xsl:otherwise>
239                 <xsl:call-template name="verse">
240                     <xsl:with-param name="verse-content" select="text() | node()" />
241                     <xsl:with-param name="verse-type" select="wers_wciety|wers_akap|wers_cd|wers_do_prawej[1]" />
242                  </xsl:call-template>
243             </xsl:otherwise>
244         </xsl:choose>
245     </div>
246 </xsl:template>
247
248 <xsl:template name="verse">
249     <xsl:param name="verse-content" />
250     <xsl:param name="verse-type" />
251     <div class="verse">
252         <xsl:choose>
253             <xsl:when test="name($verse-type) = 'wers_akap'">
254                 <xsl:attribute name="style">padding-left: 1em</xsl:attribute>
255             </xsl:when>
256             <xsl:when test="name($verse-type) = 'wers_wciety'">
257                 <xsl:choose>
258                     <xsl:when test="$verse-content/@typ">
259                         <xsl:attribute name="style">padding-left: <xsl:value-of select="$verse-content/@typ" />em</xsl:attribute>
260                     </xsl:when>
261                     <xsl:otherwise>
262                         <xsl:attribute name="style">padding-left: 1em</xsl:attribute>
263                     </xsl:otherwise>
264                 </xsl:choose>
265             </xsl:when>
266             <xsl:when test="name($verse-type) = 'wers_cd'">
267                 <xsl:attribute name="style">padding-left: 12em</xsl:attribute>
268             </xsl:when>
269             <xsl:when test="name($verse-type) = 'wers_do_prawej'">
270                 <xsl:attribute name="style">text-align: right</xsl:attribute>
271             </xsl:when>
272         </xsl:choose>
273         <xsl:apply-templates select="$verse-content" mode="inline" />
274     </div>
275 </xsl:template>
276
277 <xsl:template match="motto_podpis">
278     <xsl:call-template name="section-anchor"/>
279     <p class="motto_podpis"><xsl:apply-templates mode="inline" /></p>
280 </xsl:template>
281
282 <xsl:template match="tabela|tabelka">
283     <xsl:call-template name="section-anchor"/>
284     <xsl:choose>
285         <xsl:when test="@ramka = '1'">
286             <table class="border"><xsl:apply-templates /></table>
287         </xsl:when>
288         <xsl:otherwise>
289             <table><xsl:apply-templates /></table>
290         </xsl:otherwise>
291     </xsl:choose>
292 </xsl:template>
293 <xsl:template match="wiersz">
294     <tr><xsl:apply-templates /></tr>
295 </xsl:template>
296 <xsl:template match="kol">
297     <td><xsl:apply-templates mode="inline" /></td>
298 </xsl:template>
299
300 <xsl:template match="mat">
301     <math xmlns="http://www.w3.org/1998/Math/MathML"><xsl:copy-of select="*" /></math>
302 </xsl:template>
303
304
305 <!-- ================================================ -->
306 <!-- = INLINE TAGS                                  = -->
307 <!-- = (contain other inline tags and special tags) = -->
308 <!-- ================================================ -->
309 <!-- Annotations -->
310 <xsl:template match="pa|pe|pr|pt" mode="inline">
311     <a name="{concat('anchor-', generate-id(.))}" />
312     <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>
313 </xsl:template>
314
315 <!-- Other inline tags -->
316 <xsl:template match="mat" mode="inline">
317     <math xmlns="http://www.w3.org/1998/Math/MathML"><xsl:copy-of select="*" /></math>
318 </xsl:template>
319
320 <xsl:template match="didask_tekst" mode="inline">
321     <em class="didaskalia"><xsl:apply-templates mode="inline" /></em>
322 </xsl:template>
323
324 <xsl:template match="slowo_obce" mode="inline">
325     <em class="foreign-word"><xsl:apply-templates mode="inline" /></em>
326 </xsl:template>
327
328 <xsl:template match="tytul_dziela" mode="inline">
329     <em class="book-title">
330         <xsl:if test="@typ = '1'">„</xsl:if><xsl:apply-templates mode="inline" /><xsl:if test="@typ = '1'">”</xsl:if>
331     </em>
332 </xsl:template>
333
334 <xsl:template match="wyroznienie" mode="inline">
335     <em class="author-emphasis"><xsl:apply-templates mode="inline" /></em>
336 </xsl:template>
337
338 <xsl:template match="wieksze_odstepy" mode="inline">
339     <em class="wieksze-odstepy"><xsl:apply-templates mode="inline" /></em>
340 </xsl:template>
341
342 <xsl:template match="indeks_dolny" mode="inline">
343     <sub><xsl:apply-templates mode="inline" /></sub>
344 </xsl:template>
345
346 <xsl:template match="osoba" mode="inline">
347     <em class="person"><xsl:apply-templates mode="inline" /></em>
348 </xsl:template>
349
350
351 <!-- ============================================== -->
352 <!-- = STANDALONE TAGS                            = -->
353 <!-- = (cannot contain any other tags)            = -->
354 <!-- ============================================== -->
355 <xsl:template match="sekcja_swiatlo">
356     <hr class="spacer" />
357 </xsl:template>
358
359 <xsl:template match="sekcja_asterysk">
360     <p class="spacer-asterisk">*</p>
361 </xsl:template>
362
363 <xsl:template match="separator_linia">
364     <hr class="spacer-line" />
365 </xsl:template>
366
367
368 <!-- ================ -->
369 <!-- = SPECIAL TAGS = -->
370 <!-- ================ -->
371 <!-- Themes -->
372 <xsl:template match="begin" mode="inline">
373     <xsl:variable name="mnum" select="concat('m', substring(@id, 2))" />
374     <a name="m{substring(@id, 2)}" class="theme-begin" fid="{substring(@id, 2)}">
375         <xsl:value-of select="string(following::motyw[@id=$mnum]/text())" />
376     </a>
377 </xsl:template>
378
379 <xsl:template match="end" mode="inline">
380     <span class="theme-end" fid="{substring(@id, 2)}"> </span>
381 </xsl:template>
382
383 <xsl:template match="begin|end">
384     <xsl:apply-templates select='.' mode="inline" />
385 </xsl:template>
386
387 <xsl:template match="motyw" mode="inline" />
388
389
390 <xsl:template match="nota_red" mode="special">
391     <div id="nota_red">
392         <xsl:apply-templates />
393     </div>
394 </xsl:template>
395
396
397 <xsl:template name="translators">
398     <xsl:if test="//dc:contributor.translator">
399         <span class="translator">
400             <xsl:text>tłum. </xsl:text>
401             <xsl:for-each select="//dc:contributor.translator">
402                 <xsl:if test="position() != 1">, </xsl:if>
403                 <xsl:apply-templates mode="person" />
404             </xsl:for-each>
405         </span>
406     </xsl:if>
407 </xsl:template>
408
409 <xsl:template match="text()" mode="person">
410     <xsl:value-of select="wl:person_name(.)" />
411 </xsl:template>
412
413
414 <!-- ================ -->
415 <!-- = IGNORED TAGS = -->
416 <!-- ================ -->
417 <xsl:template match="extra|uwaga" />
418 <xsl:template match="extra|uwaga" mode="inline" />
419
420 <xsl:template match="nota_red" />
421 <xsl:template match="abstrakt" />
422
423 <!-- ======== -->
424 <!-- = TEXT = -->
425 <!-- ======== -->
426 <xsl:template match="text()" />
427 <xsl:template match="text()" mode="inline">
428     <xsl:value-of select="wl:substitute_entities(.)" />
429 </xsl:template>
430
431 <!-- ========= -->
432 <!-- = utils = -->
433 <!-- ========= -->
434 <xsl:template name="section-anchor">
435   <!-- 
436        this formula works as follows:
437        - get all ancestors including self
438        - choose the header (third one from root): utwor/book-type/header
439        - get all preceding siblings
440        - count them
441        - create an <a name="sec123"/> tag.
442   -->
443         <a name="{concat('sec', count(ancestor-or-self::*[last()-2]/preceding-sibling::*) + 1)}" />
444 </xsl:template>
445
446 </xsl:stylesheet>