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