Preliminary math and tables support.
[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
116 <!-- ========================================== -->
117 <!-- = PARAGRAPH TAGS                         = -->
118 <!-- = (can contain inline and special tags)  = -->
119 <!-- ========================================== -->
120 <!-- Title page -->
121 <xsl:template match="autor_utworu" mode="header">
122     <span class="author"><xsl:apply-templates mode="inline" /></span>
123 </xsl:template>
124
125 <xsl:template match="nazwa_utworu" mode="header">
126     <span class="title"><xsl:apply-templates mode="inline" /></span>
127 </xsl:template>
128
129 <xsl:template match="dzielo_nadrzedne" mode="header">
130     <span class="collection"><xsl:apply-templates mode="inline" /></span>
131 </xsl:template>
132
133 <xsl:template match="podtytul" mode="header">
134     <span class="subtitle"><xsl:apply-templates mode="inline" /></span>
135 </xsl:template>
136
137 <!-- Section headers (included in index)-->
138 <xsl:template match="naglowek_akt|naglowek_czesc|srodtytul">
139   <xsl:call-template name="section-anchor"/>
140     <h2><xsl:apply-templates mode="inline" /></h2>
141 </xsl:template>
142
143 <xsl:template match="naglowek_scena|naglowek_rozdzial">
144     <xsl:call-template name="section-anchor"/>
145     <h3><xsl:apply-templates mode="inline" /></h3>
146 </xsl:template>
147
148 <xsl:template match="naglowek_osoba|naglowek_podrozdzial">
149       <xsl:call-template name="section-anchor"/>
150     <h4><xsl:apply-templates mode="inline" /></h4>
151 </xsl:template>
152
153 <!-- Other paragraph tags -->
154 <xsl:template match="miejsce_czas">
155       <xsl:call-template name="section-anchor"/>
156     <p class="place-and-time"><xsl:apply-templates mode="inline" /></p>
157 </xsl:template>
158
159 <xsl:template match="didaskalia">
160       <xsl:call-template name="section-anchor"/>
161     <div class="didaskalia"><xsl:apply-templates mode="inline" /></div>
162 </xsl:template>
163
164 <xsl:template match="lista_osoba">
165     <li><xsl:apply-templates mode="inline" /></li>
166 </xsl:template>
167
168 <xsl:template match="akap|akap_dialog|akap_cd">
169     <p class="paragraph">
170       <xsl:call-template name="section-anchor"/>
171         <xsl:apply-templates mode="inline" />
172     </p>
173 </xsl:template>
174
175 <xsl:template match="strofa">
176     <div class="stanza">
177       <xsl:call-template name="section-anchor"/>
178         <xsl:choose>
179             <xsl:when test="count(br) > 0">
180                 <xsl:call-template name="verse">
181                     <xsl:with-param name="verse-content" select="br[1]/preceding-sibling::text() | br[1]/preceding-sibling::node()" />
182                     <xsl:with-param name="verse-type" select="br[1]/preceding-sibling::*[name() = 'wers_wciety' or name() = 'wers_akap' or name() = 'wers_cd'][1]" />
183                 </xsl:call-template>
184                 <xsl:for-each select="br">              
185                                 <!-- Each BR tag "consumes" text after it -->
186                     <xsl:variable name="lnum" select="count(preceding-sibling::br)" />
187                     <xsl:call-template name="verse">
188                         <xsl:with-param name="verse-content"
189                             select="following-sibling::text()[count(preceding-sibling::br) = $lnum+1] | following-sibling::node()[count(preceding-sibling::br) = $lnum+1]" />
190                         <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]" />
191                     </xsl:call-template>
192                 </xsl:for-each>
193             </xsl:when>
194             <xsl:otherwise>
195                 <xsl:call-template name="verse">
196                     <xsl:with-param name="verse-content" select="text() | node()" />
197                     <xsl:with-param name="verse-type" select="wers_wciety|wers_akap|wers_cd[1]" />
198                  </xsl:call-template>
199             </xsl:otherwise>
200         </xsl:choose>
201     </div>
202 </xsl:template>
203
204 <xsl:template name="verse">
205     <xsl:param name="verse-content" />
206     <xsl:param name="verse-type" />
207     <p class="verse">
208         <xsl:choose>
209             <xsl:when test="name($verse-type) = 'wers_akap'">
210                 <xsl:attribute name="style">padding-left: 1em</xsl:attribute>
211             </xsl:when>
212             <xsl:when test="name($verse-type) = 'wers_wciety'">
213                 <xsl:choose>
214                     <xsl:when test="$verse-content/@typ">
215                         <xsl:attribute name="style">padding-left: <xsl:value-of select="$verse-content/@typ" />em</xsl:attribute>
216                     </xsl:when>
217                     <xsl:otherwise>
218                         <xsl:attribute name="style">padding-left: 1em</xsl:attribute>
219                     </xsl:otherwise>
220                 </xsl:choose>
221             </xsl:when>
222             <xsl:when test="name($verse-type) = 'wers_cd'">
223                 <xsl:attribute name="style">padding-left: 12em</xsl:attribute>
224             </xsl:when>
225         </xsl:choose>
226         <xsl:apply-templates select="$verse-content" mode="inline" />
227     </p>
228 </xsl:template>
229
230 <xsl:template match="motto_podpis">
231     <p class="motto_podpis"><xsl:apply-templates mode="inline" /></p>
232 </xsl:template>
233
234 <xsl:template match="tabela">
235     <table><xsl:apply-templates /></table>
236 </xsl:template>
237 <xsl:template match="wiersz">
238     <tr><xsl:apply-templates /></tr>
239 </xsl:template>
240 <xsl:template match="kol">
241     <td><xsl:apply-templates mode="inline" /></td>
242 </xsl:template>
243
244 <xsl:template match="mat">
245     <xsl:copy-of select="*" />
246 </xsl:template>
247
248
249 <!-- ================================================ -->
250 <!-- = INLINE TAGS                                  = -->
251 <!-- = (contain other inline tags and special tags) = -->
252 <!-- ================================================ -->
253 <!-- Annotations -->
254 <xsl:template match="pa|pe|pr|pt" mode="inline">
255     <a name="{concat('anchor-', generate-id(.))}" />
256     <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>
257 </xsl:template>
258
259 <!-- Other inline tags -->
260 <xsl:template match="mat" mode="inline">
261     <xsl:copy-of select="*" />
262 </xsl:template>
263
264 <xsl:template match="didask_tekst" mode="inline">
265     <em class="didaskalia"><xsl:apply-templates mode="inline" /></em>
266 </xsl:template>
267
268 <xsl:template match="slowo_obce" mode="inline">
269     <em class="foreign-word"><xsl:apply-templates mode="inline" /></em>
270 </xsl:template>
271
272 <xsl:template match="tytul_dziela" mode="inline">
273     <em class="book-title">
274         <xsl:if test="@typ = '1'">„</xsl:if><xsl:apply-templates mode="inline" /><xsl:if test="@typ = '1'">”</xsl:if>
275     </em>
276 </xsl:template>
277
278 <xsl:template match="wyroznienie" mode="inline">
279     <em class="author-emphasis"><xsl:apply-templates mode="inline" /></em>
280 </xsl:template>
281
282 <xsl:template match="osoba" mode="inline">
283     <em class="person"><xsl:apply-templates mode="inline" /></em>
284 </xsl:template>
285
286
287 <!-- ============================================== -->
288 <!-- = STANDALONE TAGS                            = -->
289 <!-- = (cannot contain any other tags)            = -->
290 <!-- ============================================== -->
291 <xsl:template match="sekcja_swiatlo">
292     <hr class="spacer" />
293 </xsl:template>
294
295 <xsl:template match="sekcja_asterysk">
296     <p class="spacer-asterisk">*</p>
297 </xsl:template>
298
299 <xsl:template match="separator_linia">
300     <hr class="spacer-line" />
301 </xsl:template>
302
303
304 <!-- ================ -->
305 <!-- = SPECIAL TAGS = -->
306 <!-- ================ -->
307 <!-- Themes -->
308 <xsl:template match="begin" mode="inline">
309     <xsl:variable name="mnum" select="concat('m', substring(@id, 2))" />
310     <a name="m{substring(@id, 2)}" class="theme-begin" fid="{substring(@id, 2)}">
311         <xsl:value-of select="string(following::motyw[@id=$mnum]/text())" />
312     </a>
313 </xsl:template>
314
315 <xsl:template match="end" mode="inline">
316     <span class="theme-end" fid="{substring(@id, 2)}"> </span>
317 </xsl:template>
318
319 <xsl:template match="begin|end">
320     <xsl:apply-templates select='.' mode="inline" />
321 </xsl:template>
322
323 <xsl:template match="motyw" mode="inline" />
324
325
326 <xsl:template match="nota_red" mode="special">
327     <div id="nota_red">
328         <xsl:apply-templates />
329     </div>
330 </xsl:template>
331
332
333 <xsl:template name="translators">
334     <xsl:if test="//dc:contributor.translator">
335         <span class="translator">
336             <xsl:text>tłum. </xsl:text>
337             <xsl:for-each select="//dc:contributor.translator">
338                 <xsl:if test="position() != 1">, </xsl:if>
339                 <xsl:apply-templates mode="person" />
340             </xsl:for-each>
341         </span>
342     </xsl:if>
343 </xsl:template>
344
345 <xsl:template match="text()" mode="person">
346     <xsl:value-of select="wl:person_name(.)" />
347 </xsl:template>
348
349
350 <!-- ================ -->
351 <!-- = IGNORED TAGS = -->
352 <!-- ================ -->
353 <xsl:template match="extra|uwaga" />
354 <xsl:template match="extra|uwaga" mode="inline" />
355
356 <xsl:template match="nota_red" />
357
358 <!-- ======== -->
359 <!-- = TEXT = -->
360 <!-- ======== -->
361 <xsl:template match="text()" />
362 <xsl:template match="text()" mode="inline">
363     <xsl:value-of select="wl:substitute_entities(.)" />
364 </xsl:template>
365
366 <!-- ========= -->
367 <!-- = utils = -->
368 <!-- ========= -->
369 <xsl:template name="section-anchor">
370   <!-- 
371        this formula works as follows:
372        - get all ancestors including self
373        - choose the header (third one from root): utwor/book-type/header
374        - get all preceding siblings
375        - count them
376        - create an <a name="sec123"/> tag.
377   -->
378         <a name="{concat('sec', count(ancestor-or-self::*[last()-2]/preceding-sibling::*) + 1)}" />
379 </xsl:template>
380
381 </xsl:stylesheet>