Preliminary math and tables support.
[librarian.git] / librarian / xslt / book2txt.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" method="text" />
13
14 <xsl:param name="wrapping" select="0" />
15
16 <!-- ============================================================================== -->
17 <!-- = MASTER TAG                                                                 = -->
18 <!-- = (can contain block tags, paragraph tags, standalone tags and special tags) = -->
19 <!-- ============================================================================== -->
20 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny">
21 <xsl:if test="not (../@raw-text) and nazwa_utworu">
22     <xsl:apply-templates select="autor_utworu|dzielo_nadrzedne|nazwa_utworu|podtytul" mode="header" />
23     <xsl:call-template name="translators" />
24     <xsl:text>
25 </xsl:text>
26 </xsl:if>
27 <xsl:apply-templates />
28 <xsl:text>
29
30 </xsl:text>
31 </xsl:template>
32
33
34 <!-- ==================================================================================== -->
35 <!-- = BLOCK TAGS                                                                       = -->
36 <!-- = (can contain other block tags, paragraph tags, standalone tags and special tags) = -->
37 <!-- ==================================================================================== -->
38 <xsl:template match="nota">
39 <xsl:apply-templates />
40 </xsl:template>
41
42 <xsl:template match="lista_osob">
43 <xsl:text>
44
45
46 </xsl:text>
47 <xsl:value-of select="naglowek_listy" />
48 <xsl:apply-templates select="lista_osoba" />
49 <xsl:text>
50 </xsl:text>
51 </xsl:template>
52
53 <xsl:template match="dedykacja">
54 <xsl:text>
55
56 </xsl:text>
57 <xsl:apply-templates />
58 </xsl:template>
59
60 <xsl:template match="kwestia">
61 <xsl:apply-templates select="strofa|akap|didaskalia" />
62 </xsl:template>
63
64 <xsl:template match="dlugi_cytat|poezja_cyt">
65 <xsl:text>
66 </xsl:text>
67 <xsl:apply-templates />
68 </xsl:template>
69
70 <xsl:template match="motto">
71 <xsl:text>
72
73
74
75 </xsl:text>
76 <xsl:apply-templates /><xsl:text>
77
78 </xsl:text>
79 </xsl:template>
80
81
82 <!-- ========================================== -->
83 <!-- = PARAGRAPH TAGS                         = -->
84 <!-- = (can contain inline and special tags)  = -->
85 <!-- ========================================== -->
86 <!-- Title page -->
87 <xsl:template match="autor_utworu" mode="header">
88 <xsl:apply-templates mode="inline" />
89 <xsl:text>
90
91 </xsl:text>
92 </xsl:template>
93
94 <xsl:template match="nazwa_utworu" mode="header">
95 <xsl:apply-templates mode="inline" />
96 <xsl:text>
97 </xsl:text>
98 </xsl:template>
99
100 <xsl:template match="dzielo_nadrzedne" mode="header">
101 <xsl:apply-templates mode="inline" />
102 <xsl:text>
103 </xsl:text>
104 </xsl:template>
105
106 <xsl:template match="podtytul" mode="header">
107 <xsl:apply-templates mode="inline" />
108 <xsl:text>
109 </xsl:text>
110 </xsl:template>
111
112 <!-- Section headers (included in index)-->
113 <xsl:template match="naglowek_akt|naglowek_czesc|srodtytul">
114 <xsl:text>
115
116
117
118
119 </xsl:text>
120 <xsl:apply-templates mode="inline" />
121 </xsl:template>
122
123 <xsl:template match="naglowek_scena|naglowek_rozdzial">
124 <xsl:text>
125
126
127
128 </xsl:text>
129 <xsl:apply-templates mode="inline" />
130 </xsl:template>
131
132 <xsl:template match="naglowek_osoba|naglowek_podrozdzial">
133 <xsl:text>
134
135
136 </xsl:text>
137 <xsl:apply-templates mode="inline" />
138 </xsl:template>
139
140 <!-- Other paragraph tags -->
141 <xsl:template match="miejsce_czas">
142 <xsl:text>
143
144
145
146 </xsl:text>
147 <xsl:apply-templates mode="inline" />
148 </xsl:template>
149
150 <xsl:template match="didaskalia">
151 <xsl:variable name="content">
152     <xsl:apply-templates select="*|text()" mode="inline" />
153 </xsl:variable>
154 <xsl:text>
155
156 / </xsl:text><xsl:value-of select="wl:wrap_words(wl:strip($content), $wrapping)" /><xsl:text> /</xsl:text>
157 </xsl:template>
158
159 <xsl:template match="lista_osoba">
160 <xsl:text>
161  * </xsl:text>
162 <xsl:apply-templates mode="inline" />
163 </xsl:template>
164
165 <xsl:template match="akap|akap_dialog|akap_cd">
166 <xsl:variable name="content">
167     <xsl:apply-templates select="*|text()" mode="inline" />
168 </xsl:variable>
169 <xsl:text>
170
171 </xsl:text>
172 <xsl:value-of select="wl:wrap_words(wl:strip($content), $wrapping)" />
173 </xsl:template>
174
175 <xsl:template match="strofa">
176 <xsl:text>
177 </xsl:text>
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 </xsl:template>
202
203 <xsl:template name="verse">
204     <xsl:param name="verse-content" />
205     <xsl:param name="verse-type" />
206 <xsl:text>
207 </xsl:text>
208     <xsl:variable name="content">
209         <xsl:apply-templates select="$verse-content" mode="inline" />
210     </xsl:variable>
211     <xsl:choose>
212         <xsl:when test="name($verse-type) = 'wers_akap'">
213             <xsl:text>  </xsl:text>
214         </xsl:when>
215         <xsl:when test="name($verse-type) = 'wers_wciety'">
216             <xsl:choose>
217                 <xsl:when test="$verse-content/@typ">
218                     <xsl:text>    </xsl:text>
219                 </xsl:when>
220                 <xsl:otherwise>
221                     <xsl:text>  </xsl:text>
222                 </xsl:otherwise>
223             </xsl:choose>
224         </xsl:when>
225         <xsl:when test="name($verse-type) = 'wers_cd'">
226             <xsl:text>                        </xsl:text>
227         </xsl:when>
228     </xsl:choose>
229 <xsl:value-of select="wl:strip($content)" />
230 </xsl:template>
231
232 <xsl:template match="motto_podpis">
233 <xsl:apply-templates mode="inline" />
234 </xsl:template>
235
236 <xsl:template match="tabela">
237         <xsl:text>
238
239 </xsl:text>
240 <xsl:apply-templates/>
241 <xsl:text>
242 </xsl:text>
243 </xsl:template>
244 <xsl:template match="wiersz">
245         <xsl:apply-templates />
246         <xsl:text>
247 </xsl:text>
248 </xsl:template>
249 <xsl:template match="kol">
250         <xsl:apply-templates mode="inline" />
251         <xsl:text>    </xsl:text>
252 </xsl:template>
253
254
255 <!-- ================================================ -->
256 <!-- = INLINE TAGS                                  = -->
257 <!-- = (contain other inline tags and special tags) = -->
258 <!-- ================================================ -->
259 <!-- Annotations -->
260 <xsl:template match="pa|pe|pr|pt" mode="inline" />
261
262 <!-- Other inline tags -->
263 <xsl:template match="mat" mode="inline"><xsl:apply-templates mode="inline" /></xsl:template>
264
265 <xsl:template match="didask_tekst" mode="inline"><xsl:apply-templates mode="inline" /></xsl:template>
266
267 <xsl:template match="slowo_obce" mode="inline"><xsl:apply-templates mode="inline" /></xsl:template>
268
269 <xsl:template match="tytul_dziela" mode="inline">
270 <xsl:if test="@typ = '1'">„</xsl:if><xsl:apply-templates mode="inline" /><xsl:if test="@typ = '1'">”</xsl:if>
271 </xsl:template>
272
273 <xsl:template match="wyroznienie" mode="inline">
274 <xsl:text>*</xsl:text><xsl:apply-templates mode="inline" /><xsl:text>*</xsl:text>
275 </xsl:template>
276
277 <xsl:template match="osoba" mode="inline">
278 <xsl:apply-templates mode="inline" />
279 </xsl:template>
280
281
282 <!-- ============================================== -->
283 <!-- = STANDALONE TAGS                            = -->
284 <!-- = (cannot contain any other tags)            = -->
285 <!-- ============================================== -->
286 <xsl:template match="sekcja_swiatlo">
287 <xsl:text>
288
289
290
291 </xsl:text>
292 </xsl:template>
293
294 <xsl:template match="sekcja_asterysk">
295 <xsl:text>
296
297 *
298
299 </xsl:text>
300 </xsl:template>
301
302 <xsl:template match="separator_linia">
303 <xsl:text>
304
305 ------------------------------------------------
306
307 </xsl:text>
308 </xsl:template>
309
310
311 <!-- ================ -->
312 <!-- = SPECIAL TAGS = -->
313 <!-- ================ -->
314 <!-- Themes -->
315 <xsl:template match="begin" mode="inline" />
316
317 <xsl:template match="end" mode="inline" />
318
319 <xsl:template match="begin|end" />
320
321 <xsl:template match="motyw" mode="inline" />
322
323 <xsl:template name="translators">
324     <xsl:if test="//dc:contributor.translator">
325         <text>tłum. </text>
326         <xsl:for-each select="//dc:contributor.translator/text()">
327             <xsl:if test="position() != 1">
328                 <xsl:text>, </xsl:text>
329             </xsl:if>
330             <xsl:value-of select="wl:person_name(.)" />
331         </xsl:for-each>
332         <xsl:text>
333 </xsl:text>
334     </xsl:if>
335 </xsl:template>
336
337
338 <!-- ================ -->
339 <!-- = IGNORED TAGS = -->
340 <!-- ================ -->
341 <xsl:template match="extra|uwaga" />
342 <xsl:template match="extra|uwaga" mode="inline" />
343
344 <xsl:template match="nota_red" />
345
346 <!-- ======== -->
347 <!-- = TEXT = -->
348 <!-- ======== -->
349 <xsl:template match="text()" />
350 <xsl:template match="text()" mode="inline">
351     <xsl:value-of select="wl:substitute_entities(.)" />
352 </xsl:template>
353
354
355 </xsl:stylesheet>
356