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