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