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