Add thank-you note in EPUB and PDF.
[librarian.git] / librarian / pdf / wl2tex.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     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
12
13 <xsl:output encoding="utf-8" indent="yes" version="2.0" />
14
15 <xsl:template match="utwor">
16     <TeXML xmlns="http://getfo.sourceforge.net/texml/ns1">
17         <TeXML escape="0">
18         \documentclass[<xsl:value-of select="@customizations"/>]{wl}
19
20         <!-- flags and values set on root -->
21
22         \newif\ifflaglessadvertising
23         <xsl:for-each select="@*[starts-with(name(), 'flag-')]">
24             <cmd>
25                 <xsl:attribute name="name"><xsl:value-of select="wl:texcommand(name())" />true</xsl:attribute>
26             </cmd>
27         </xsl:for-each>
28
29         <xsl:for-each select="@*[starts-with(name(), 'data-')]">
30             <TeXML escape="0">
31                 \def\<xsl:value-of select="wl:texcommand(name())" />{<TeXML escape="1"><xsl:value-of select="."/></TeXML>}
32             </TeXML>
33         </xsl:for-each>
34         </TeXML>
35
36         <xsl:choose>
37             <xsl:when test="@morefloats = 'new'">
38                 <TeXML escape="0">
39                     \usepackage[maxfloats=64]{morefloats}
40                 </TeXML>
41             </xsl:when>
42             <xsl:when test="@morefloats = 'old'">
43                 <TeXML escape="0">
44                     \usepackage{morefloats}
45                 </TeXML>
46             </xsl:when>
47             <xsl:when test="@morefloats = 'none'" />
48             <xsl:otherwise>
49                 <TeXML escape="0">
50                     \IfFileExists{morefloats.sty}{
51                         \usepackage{morefloats}
52                     }{}
53                 </TeXML>
54             </xsl:otherwise>
55         </xsl:choose>
56
57         <xsl:apply-templates select="rdf:RDF" mode="titlepage" />
58         <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='titlepage' />
59
60         <env name="document">
61             <xsl:if test="@data-cover-width">
62                 <cmd name="makecover">
63                     <parm><xsl:value-of select="210 * @data-cover-width div @data-cover-height" />mm</parm>
64                     <parm>210mm</parm>
65                 </cmd>
66             </xsl:if>
67                 <xsl:if test="@thanks">
68                     <TeXML escape="0">
69                         \def\thanknote{<TeXML escape="1"><xsl:value-of select="@thanks" /></TeXML>}
70                     </TeXML>
71                 </xsl:if>
72             <cmd name="maketitle" />
73
74             <cmd name="tytul"><parm>
75               <xsl:choose>
76                 <xsl:when test="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/nazwa_utworu">
77                     <!-- title in master -->
78                     <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/autor_utworu" mode="title" />
79                     <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/dzielo_nadrzedne" mode="title" />
80                     <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/nazwa_utworu" mode="title" />
81                     <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/podtytul" mode="title" />
82                     <!-- dc in master or not -->
83                     <cmd name="translatorsline" />
84                 </xsl:when>
85                 <xsl:otherwise>
86                     <!-- look for author title in dc -->
87                     <xsl:apply-templates select="rdf:RDF" mode="firstdctitle" />
88                     <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='firstdctitle' />
89                 </xsl:otherwise>
90               </xsl:choose>
91             </parm></cmd>
92             <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" />
93             <xsl:apply-templates select="utwor" mode="part" />
94
95             <TeXML escape="0">
96                 <xsl:if test="@data-cover-by">
97                 \def\coverby{Okładka na podstawie:
98                     <xsl:choose>
99                     <xsl:when test="@data-cover-source">
100                         \href{\datacoversource}{\datacoverby}
101                     </xsl:when>
102                     <xsl:otherwise>
103                         \datacoverby{}
104                     </xsl:otherwise>
105                     </xsl:choose>
106                 }
107                 </xsl:if>
108                 \def\editors{<TeXML escape="1"><xsl:call-template name="editors" /></TeXML>}
109                 <xsl:if test="@funders">
110                     \def\funders{Publikację ufundowali i ufundowały:
111                         <TeXML escape="1"><xsl:value-of select="@funders" /></TeXML>.}
112                 </xsl:if>
113             </TeXML>
114
115             <cmd name="editorialsection" />
116
117         </env>
118     </TeXML>
119 </xsl:template>
120
121 <xsl:template match="utwor" mode="part">
122     <cmd name="tytul"><parm>
123       <xsl:choose>
124         <xsl:when test="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/nazwa_utworu">
125             <!-- title in master -->
126             <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/nazwa_utworu" mode="title" />
127             <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/podtytul" mode="title" />
128         </xsl:when>
129         <xsl:otherwise>
130             <!-- look for title in dc -->
131             <xsl:apply-templates select="rdf:RDF" mode="dctitle" />
132             <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='dctitle' />
133         </xsl:otherwise>
134       </xsl:choose>
135     </parm></cmd>
136
137     <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" />
138     <xsl:apply-templates select="utwor" mode="part" />
139 </xsl:template>
140
141 <!-- =================== -->
142 <!-- = MAIN TITLE PAGE = -->
143 <!-- = (from DC)       = -->
144 <!-- =================== -->
145
146 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="titlepage">
147     <xsl:apply-templates select="rdf:RDF" mode="titlepage" />
148 </xsl:template>
149
150 <xsl:template match="rdf:RDF" mode="titlepage">
151     <TeXML escape="0">
152         \def\authors{<xsl:call-template name="authors" />}
153         \author{\authors}
154         \title{<xsl:apply-templates select=".//dc:title" mode="inline" />}
155         \def\translatorsline{<xsl:call-template name="translators" />}
156
157         \def\bookurl{<xsl:value-of select=".//dc:identifier.url" />}
158
159         \def\rightsinfo{Ten utwór nie jest objęty majątkowym prawem autorskim i~znajduje się w~domenie
160             publicznej, co oznacza że możesz go swobodnie wykorzystywać, publikować
161             i~rozpowszechniać. Jeśli utwór opatrzony jest dodatkowymi materiałami
162             (przypisy, motywy literackie etc.), które podlegają prawu autorskiemu, to
163             te dodatkowe materiały udostępnione są na licencji
164             \href{http://creativecommons.org/licenses/by-sa/3.0/}{Creative Commons
165             Uznanie Autorstwa – Na Tych Samych Warunkach 3.0 PL}.}
166         <xsl:if test=".//dc:rights.license">
167             \def\rightsinfo{Ten utwór jest udostepniony na licencji
168             \href{<xsl:value-of select=".//dc:rights.license" />}{<xsl:value-of select=".//dc:rights" />}.}
169         </xsl:if>
170
171         \def\sourceinfo{
172             <xsl:if test=".//dc:source">
173                 Tekst opracowany na podstawie: <xsl:apply-templates select=".//dc:source" mode="inline" />
174                 \vspace{.6em}
175             </xsl:if>}
176         \def\description{<xsl:apply-templates select=".//dc:description" mode="inline" />}
177     </TeXML>
178 </xsl:template>
179
180
181 <!-- ============== -->
182 <!-- = BOOK TITLE = -->
183 <!-- = (from DC)  = -->
184 <!-- ============== -->
185
186 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="dctitle">
187     <xsl:apply-templates select="rdf:RDF" mode="dctitle" />
188 </xsl:template>
189
190 <xsl:template match="rdf:RDF" mode="dctitle">
191     <cmd name="nazwapodutworu"><parm>
192         <xsl:apply-templates select=".//dc:title/node()" mode="inline" />
193     </parm></cmd>
194 </xsl:template>
195
196 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="firstdctitle">
197     <xsl:apply-templates select="rdf:RDF" mode="firstdctitle" />
198 </xsl:template>
199
200 <xsl:template match="rdf:RDF" mode="firstdctitle">
201     <cmd name="autorutworu"><parm><cmd name="authors"/></parm></cmd>
202     <cmd name="nazwautworu"><parm>
203         <xsl:apply-templates select=".//dc:title/node()" mode="inline" />
204     </parm></cmd>
205     <cmd name="translatorsline" />
206 </xsl:template>
207
208
209 <!-- ============================================================================== -->
210 <!-- = MASTER TAG                                                                 = -->
211 <!-- = (can contain block tags, paragraph tags, standalone tags and special tags) = -->
212 <!-- ============================================================================== -->
213 <!-- ==================================================================================== -->
214 <!-- = BLOCK TAGS                                                                       = -->
215 <!-- = (can contain other block tags, paragraph tags, standalone tags and special tags) = -->
216 <!-- ==================================================================================== -->
217
218 <xsl:template
219     match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny|nota|dedykacja|dlugi_cytat|poezja_cyt|motto">
220     <cmd>
221         <xsl:attribute name="name">
222             <xsl:value-of select="wl:texcommand(name())" />
223         </xsl:attribute>
224         <parm><xsl:apply-templates/></parm>
225     </cmd>
226 </xsl:template>
227
228 <xsl:template match="lista_osob">
229     <cmd name="listaosob">
230         <parm><xsl:apply-templates select="naglowek_listy" /></parm>
231         <parm><xsl:apply-templates select="lista_osoba" /></parm>
232     </cmd>
233 </xsl:template>
234
235 <xsl:template match="kwestia">
236     <cmd name="kwestia">
237         <parm><xsl:apply-templates select="strofa|akap|didaskalia" /></parm>
238     </cmd>
239 </xsl:template>
240
241
242 <!-- ========================================== -->
243 <!-- = PARAGRAPH TAGS                         = -->
244 <!-- = (can contain inline and special tags)  = -->
245 <!-- ========================================== -->
246
247 <!-- only in root -->
248 <xsl:template
249     match="autor_utworu|dzielo_nadrzedne|nazwa_utworu|podtytul"
250     mode="title">
251     <cmd>
252         <xsl:attribute name="name">
253             <xsl:value-of select="wl:texcommand(name())" />
254         </xsl:attribute>
255         <parm><xsl:apply-templates mode="inline"/></parm>
256     </cmd>
257 </xsl:template>
258
259
260 <xsl:template
261     match="naglowek_akt|naglowek_czesc|srodtytul|naglowek_osoba|naglowek_podrozdzial|naglowek_scena|naglowek_rozdzial|miejsce_czas|didaskalia|lista_osoba|akap|akap_dialog|akap_cd|motto_podpis|naglowek_listy">
262     <cmd>
263         <xsl:attribute name="name">
264             <xsl:value-of select="wl:texcommand(name())" />
265         </xsl:attribute>
266         <parm><xsl:apply-templates mode="inline"/></parm>
267     </cmd>
268 </xsl:template>
269
270 <xsl:template match="strofa">
271   <cmd name="strofa"><parm>
272     <xsl:choose>
273         <xsl:when test="count(br) > 0">
274             <xsl:call-template name="verse">
275                 <xsl:with-param name="verse-content" select="br[1]/preceding-sibling::text() | br[1]/preceding-sibling::node()" />
276                 <xsl:with-param name="verse-type" select="br[1]/preceding-sibling::*[name() = 'wers_wciety' or name() = 'wers_akap' or name() = 'wers_cd'][1]" />
277             </xsl:call-template>
278             <xsl:for-each select="br">
279                 <TeXML escape="0">\\{}</TeXML>
280                 <!-- Each BR tag "consumes" text after it -->
281                 <xsl:variable name="lnum" select="count(preceding-sibling::br)" />
282                 <xsl:call-template name="verse">
283                     <xsl:with-param name="verse-content"
284                         select="following-sibling::text()[count(preceding-sibling::br) = $lnum+1] | following-sibling::node()[count(preceding-sibling::br) = $lnum+1]" />
285                     <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]" />
286                 </xsl:call-template>
287             </xsl:for-each>
288         </xsl:when>
289         <xsl:otherwise>
290             <xsl:call-template name="verse">
291                 <xsl:with-param name="verse-content" select="text() | node()" />
292                 <xsl:with-param name="verse-type" select="wers_wciety|wers_akap|wers_cd[1]" />
293             </xsl:call-template>
294         </xsl:otherwise>
295     </xsl:choose>
296   </parm></cmd>
297 </xsl:template>
298
299
300 <xsl:template name="verse">
301     <xsl:param name="verse-content" />
302     <xsl:param name="verse-type" />
303
304     <cmd>
305         <xsl:attribute name="name">
306             <xsl:choose>
307                 <xsl:when test="$verse-type != ''">
308                     <xsl:value-of select='wl:texcommand(name($verse-type))' />
309                 </xsl:when>
310                 <xsl:otherwise>wers</xsl:otherwise>
311             </xsl:choose>
312         </xsl:attribute>
313         <xsl:if test="string($verse-content/@typ)">
314             <opt><xsl:value-of select="$verse-content/@typ" />em</opt>
315         </xsl:if>
316         <parm><xsl:apply-templates select="$verse-content" mode="inline"/></parm>
317     </cmd>
318 </xsl:template>
319
320 <!-- ================================================ -->
321 <!-- = INLINE TAGS                                  = -->
322 <!-- = (contain other inline tags and special tags) = -->
323 <!-- ================================================ -->
324
325 <xsl:template mode="inline"
326     match="pa|pe|pr|pt|mat|didask_tekst|slowo_obce|wyroznienie|osoba">
327     <cmd>
328         <xsl:attribute name="name">
329             <xsl:value-of select="wl:texcommand(name())" />
330         </xsl:attribute>
331         <parm><xsl:apply-templates mode="inline"/></parm>
332     </cmd>
333 </xsl:template>
334
335
336
337 <xsl:template match="tytul_dziela" mode="inline">
338     <cmd name="tytuldziela"><parm>
339         <xsl:if test="@typ = '1'">„</xsl:if><xsl:apply-templates mode="inline" /><xsl:if test="@typ = '1'">”</xsl:if>
340     </parm></cmd>
341 </xsl:template>
342
343
344
345 <!-- ============================================== -->
346 <!-- = STANDALONE TAGS                            = -->
347 <!-- = (cannot contain any other tags)            = -->
348 <!-- ============================================== -->
349 <xsl:template
350     match="sekcja_swiatlo|sekcja_asterysk|separator_linia">
351     <cmd>
352         <xsl:attribute name="name">
353             <xsl:value-of select="wl:texcommand(name())" />
354         </xsl:attribute>
355     </cmd>
356 </xsl:template>
357
358
359 <!-- ================ -->
360 <!-- = SPECIAL TAGS = -->
361 <!-- ================ -->
362 <!-- Themes -->
363
364
365 <xsl:template match="begin|end|motyw">
366     <xsl:apply-templates select='.' mode="inline" />
367 </xsl:template>
368
369 <xsl:template match="begin" mode="inline" />
370 <xsl:template match="end" mode="inline" />
371
372 <xsl:template match="motyw" mode="inline">
373     <cmd name="motyw">
374         <xsl:if test="@moved">
375             <opt name="moved"><xsl:value-of select="@moved" /></opt>
376         </xsl:if>
377         <parm><xsl:apply-templates mode="inline" /></parm>
378     </cmd>
379 </xsl:template>
380
381 <xsl:template name="authors">
382     <xsl:for-each select=".//dc:creator_parsed">
383         <xsl:if test="position() != 1">, </xsl:if>
384         <xsl:apply-templates mode="inline" />
385     </xsl:for-each>
386 </xsl:template>
387
388 <xsl:template name="editors">
389     <xsl:if test="@editors">
390         <xsl:text>Opracowanie redakcyjne i przypisy: </xsl:text>
391         <xsl:value-of select="@editors" />
392         <xsl:text>.</xsl:text>
393     </xsl:if>
394 </xsl:template>
395
396 <xsl:template name="translators">
397     <xsl:if test=".//dc:contributor.translator_parsed">
398         <cmd name='translator'><parm>
399             <xsl:for-each select=".//dc:contributor.translator_parsed">
400                 <xsl:if test="position() != 1">, </xsl:if>
401                 <xsl:apply-templates mode="inline" />
402             </xsl:for-each>
403         </parm></cmd>
404     </xsl:if>
405 </xsl:template>
406
407
408 <!-- ============== -->
409 <!-- = ADDED TAGS = -->
410 <!-- ============== -->
411
412
413 <xsl:template match="dywiz" mode="inline">
414     <cmd name="dywiz" />
415 </xsl:template>
416
417 <xsl:template match="nbsp" mode="inline">
418     <spec cat="tilde" />
419 </xsl:template>
420
421 <xsl:template match="alien" mode="inline">
422     <group>
423         <cmd name="alien" />
424         <xsl:apply-templates mode="inline" />
425     </group>
426 </xsl:template>
427
428 <!-- ================ -->
429 <!-- = IGNORED TAGS = -->
430 <!-- ================ -->
431 <xsl:template match="extra|uwaga" />
432 <xsl:template match="extra|uwaga" mode="inline" />
433
434 <xsl:template match="nota_red" />
435
436 <!-- ======== -->
437 <!-- = TEXT = -->
438 <!-- ======== -->
439 <xsl:template match="text()" />
440 <xsl:template match="text()" mode="inline">
441     <xsl:if test="preceding-sibling::node() and wl:starts_white(.)">
442       <xsl:text> </xsl:text>
443     </xsl:if>
444
445     <xsl:value-of select="wl:substitute_entities(wl:strip(.))" />
446
447     <xsl:if test="following-sibling::node() and wl:ends_white(.)">
448       <xsl:text> </xsl:text>
449     </xsl:if>
450 </xsl:template>
451
452
453 </xsl:stylesheet>