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