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