some cleaning
[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[a4paper, oneside, 11pt]{book}
19         \usepackage{wl}
20         </TeXML>
21
22         <xsl:choose>
23             <xsl:when test="@morefloats = 'new'">
24                 <TeXML escape="0">
25                     \usepackage[maxfloats=64]{morefloats}
26                 </TeXML>
27             </xsl:when>
28             <xsl:when test="@morefloats = 'old'">
29                 <TeXML escape="0">
30                     \usepackage{morefloats}
31                 </TeXML>
32             </xsl:when>
33             <xsl:when test="@morefloats = 'none'" />
34             <xsl:otherwise>
35                 <TeXML escape="0">
36                     \IfFileExists{morefloats.sty}{
37                         \usepackage{morefloats}
38                     }{}
39                 </TeXML>
40             </xsl:otherwise>
41         </xsl:choose>
42
43         <xsl:apply-templates select="rdf:RDF" mode="titlepage" />
44         <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='titlepage' />
45
46         <env name="document">
47             <cmd name="maketitle" />
48
49             <xsl:choose>
50                 <xsl:when test="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/nazwa_utworu">
51                     <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/autor_utworu" mode="title" />
52                     <!-- title in master -->
53                 </xsl:when>
54                 <xsl:otherwise>
55                     <!-- look for author title in dc -->
56                     <xsl:apply-templates select="rdf:RDF" mode="firstdctitle" />
57                     <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='firstdctitle' />
58                 </xsl:otherwise>
59             </xsl:choose>
60             <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" />
61             <xsl:apply-templates select="utwor" mode="part" />
62         </env>
63     </TeXML>
64 </xsl:template>
65
66 <xsl:template match="utwor" mode="part">
67     <!-- title for empty dc -->
68     <xsl:choose>
69         <xsl:when test="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/nazwa_utworu">
70             <!-- title in master -->
71         </xsl:when>
72         <xsl:otherwise>
73             <!-- look for title in dc -->
74             <xsl:apply-templates select="rdf:RDF" mode="dctitle" />
75             <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='dctitle' />
76         </xsl:otherwise>
77     </xsl:choose>
78
79     <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" />
80     <xsl:apply-templates select="utwor" mode="part" />
81 </xsl:template>
82
83 <!-- =================== -->
84 <!-- = MAIN TITLE PAGE = -->
85 <!-- = (from DC)       = -->
86 <!-- =================== -->
87
88 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="titlepage">
89     <xsl:apply-templates select="rdf:RDF" mode="titlepage" />
90 </xsl:template>
91
92 <xsl:template match="rdf:RDF" mode="titlepage">
93     <cmd name='title'><parm>
94         <xsl:apply-templates select=".//dc:title/node()" mode="inline" />
95     </parm></cmd>
96     <cmd name='author'><parm>
97         <xsl:apply-templates select=".//dc:creator_parsed/node()" mode="inline" />
98     </parm></cmd>
99     <TeXML escape="0">
100         \def\sourceinfo{<xsl:apply-templates select=".//dc:source" mode="inline" />}
101         \def\bookurl{<xsl:value-of select=".//dc:identifier.url" />}
102         \def\rightsinfo{Ten utwór nie jest chroniony prawem autorskim i~znajduje się w~domenie publicznej,
103             co oznacza, że możesz go swobodnie wykorzystywać, publikować i~rozpowszechniać.}
104         <xsl:if test=".//dc:rights.license">
105             \def\rightsinfo{Ten utwór jest udostepniony na licencji
106             \href{<xsl:value-of select=".//dc:rights.license" />}{<xsl:value-of select=".//dc:rights" />}.}
107         </xsl:if>
108     </TeXML>
109 </xsl:template>
110
111
112 <!-- ============== -->
113 <!-- = BOOK TITLE = -->
114 <!-- = (from DC)  = -->
115 <!-- ============== -->
116
117 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="dctitle">
118     <xsl:apply-templates select="rdf:RDF" mode="dctitle" />
119 </xsl:template>
120
121 <xsl:template match="rdf:RDF" mode="dctitle">
122     <cmd name="nazwapodutworu"><parm>
123         <xsl:apply-templates select=".//dc:title/node()" mode="inline" />
124     </parm></cmd>
125 </xsl:template>
126
127 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="firstdctitle">
128     <xsl:apply-templates select="rdf:RDF" mode="firstdctitle" />
129 </xsl:template>
130
131 <xsl:template match="rdf:RDF" mode="firstdctitle">
132     <cmd name="autorutworu"><parm>
133         <xsl:apply-templates select=".//dc:creator_parsed/node()" mode="inline" />
134     </parm></cmd>
135     <cmd name="nazwautworu"><parm>
136         <xsl:apply-templates select=".//dc:title/node()" mode="inline" />
137     </parm></cmd>
138 </xsl:template>
139
140
141 <!-- ============================================================================== -->
142 <!-- = MASTER TAG                                                                 = -->
143 <!-- = (can contain block tags, paragraph tags, standalone tags and special tags) = -->
144 <!-- ============================================================================== -->
145 <!-- ==================================================================================== -->
146 <!-- = BLOCK TAGS                                                                       = -->
147 <!-- = (can contain other block tags, paragraph tags, standalone tags and special tags) = -->
148 <!-- ==================================================================================== -->
149
150 <xsl:template 
151     match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny|nota|dedykacja|dlugi_cytat|poezja_cyt|motto">
152     <cmd>
153         <xsl:attribute name="name">
154             <xsl:value-of select="wl:texcommand(name())" />
155         </xsl:attribute>
156         <parm><xsl:apply-templates/></parm>
157     </cmd>
158 </xsl:template>
159
160 <xsl:template match="lista_osob">
161     <cmd name="listaosob">
162         <parm><xsl:apply-templates select="naglowek_listy" /></parm>
163         <parm><xsl:apply-templates select="lista_osoba" /></parm>
164     </cmd>
165 </xsl:template>
166
167 <xsl:template match="kwestia">
168     <cmd name="kwestia">
169         <parm><xsl:apply-templates select="strofa|akap|didaskalia" /></parm>
170     </cmd>
171 </xsl:template>
172
173
174 <!-- ========================================== -->
175 <!-- = PARAGRAPH TAGS                         = -->
176 <!-- = (can contain inline and special tags)  = -->
177 <!-- ========================================== -->
178
179 <!-- only in root -->
180 <xsl:template match="autor_utworu" mode="title">
181     <cmd name="autorutworu"><parm>
182         <xsl:apply-templates mode="inline" />
183     </parm></cmd>
184 </xsl:template>
185
186
187 <xsl:template 
188     match="nazwa_utworu|podtytul|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">
189     <cmd>
190         <xsl:attribute name="name">
191             <xsl:value-of select="wl:texcommand(name())" />
192         </xsl:attribute>
193         <parm><xsl:apply-templates mode="inline"/></parm>
194     </cmd>
195 </xsl:template>
196
197 <xsl:template match="strofa">
198   <cmd name="strofa"><parm>
199     <xsl:choose>
200         <xsl:when test="count(br) > 0">
201             <xsl:call-template name="verse">
202                 <xsl:with-param name="verse-content" select="br[1]/preceding-sibling::text() | br[1]/preceding-sibling::node()" />
203                 <xsl:with-param name="verse-type" select="br[1]/preceding-sibling::*[name() = 'wers_wciety' or name() = 'wers_akap' or name() = 'wers_cd'][1]" />
204             </xsl:call-template>
205             <xsl:for-each select="br">
206                 <TeXML escape="0">\\{}</TeXML>
207                 <!-- Each BR tag "consumes" text after it -->
208                 <xsl:variable name="lnum" select="count(preceding-sibling::br)" />
209                 <xsl:call-template name="verse">
210                     <xsl:with-param name="verse-content"
211                         select="following-sibling::text()[count(preceding-sibling::br) = $lnum+1] | following-sibling::node()[count(preceding-sibling::br) = $lnum+1]" />
212                     <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]" />
213                 </xsl:call-template>
214             </xsl:for-each>
215         </xsl:when>
216         <xsl:otherwise>
217             <xsl:call-template name="verse">
218                 <xsl:with-param name="verse-content" select="text() | node()" />
219                 <xsl:with-param name="verse-type" select="wers_wciety|wers_akap|wers_cd[1]" />
220             </xsl:call-template>
221         </xsl:otherwise>
222     </xsl:choose>
223   </parm></cmd>
224 </xsl:template>
225
226
227 <xsl:template name="verse">
228     <xsl:param name="verse-content" />
229     <xsl:param name="verse-type" />
230
231     <cmd>
232         <xsl:attribute name="name">
233             <xsl:choose>
234                 <xsl:when test="$verse-type != ''">
235                     <xsl:value-of select='wl:texcommand(name($verse-type))' />
236                 </xsl:when>
237                 <xsl:otherwise>wers</xsl:otherwise>
238             </xsl:choose>
239         </xsl:attribute>
240         <xsl:if test="string($verse-content/@typ)">
241             <opt><xsl:value-of select="$verse-content/@typ" />em</opt>
242         </xsl:if>
243         <parm><xsl:apply-templates select="$verse-content" mode="inline"/></parm>
244     </cmd>
245 </xsl:template>
246
247 <!-- ================================================ -->
248 <!-- = INLINE TAGS                                  = -->
249 <!-- = (contain other inline tags and special tags) = -->
250 <!-- ================================================ -->
251
252 <xsl:template mode="inline"
253     match="pa|pe|pr|pt|mat|didask_tekst|slowo_obce|wyroznienie|osoba">
254     <cmd>
255         <xsl:attribute name="name">
256             <xsl:value-of select="wl:texcommand(name())" />
257         </xsl:attribute>
258         <parm><xsl:apply-templates mode="inline"/></parm>
259     </cmd>
260 </xsl:template>
261
262
263
264 <xsl:template match="tytul_dziela" mode="inline">
265     <cmd name="tytuldziela"><parm>
266         <xsl:if test="@typ = '1'">„</xsl:if><xsl:apply-templates mode="inline" /><xsl:if test="@typ = '1'">”</xsl:if>
267     </parm></cmd>
268 </xsl:template>
269
270
271
272 <!-- ============================================== -->
273 <!-- = STANDALONE TAGS                            = -->
274 <!-- = (cannot contain any other tags)            = -->
275 <!-- ============================================== -->
276 <xsl:template
277     match="sekcja_swiatlo|sekcja_asterysk|separator_linia">
278     <cmd>
279         <xsl:attribute name="name">
280             <xsl:value-of select="wl:texcommand(name())" />
281         </xsl:attribute>
282     </cmd>
283 </xsl:template>
284
285
286 <!-- ================ -->
287 <!-- = SPECIAL TAGS = -->
288 <!-- ================ -->
289 <!-- Themes -->
290
291
292 <xsl:template match="begin|end|motyw">
293     <xsl:apply-templates select='.' mode="inline" />
294 </xsl:template>
295
296 <xsl:template match="begin" mode="inline" />
297 <xsl:template match="end" mode="inline" />
298
299 <xsl:template match="motyw" mode="inline">
300     <cmd name="motyw">
301         <xsl:if test="@moved">
302             <opt name="moved"><xsl:value-of select="@moved" /></opt>
303         </xsl:if>
304         <parm><xsl:apply-templates mode="inline" /></parm>
305     </cmd>
306 </xsl:template>
307
308
309 <!-- ============== -->
310 <!-- = ADDED TAGS = -->
311 <!-- ============== -->
312
313
314 <xsl:template match="dywiz" mode="inline">
315     <cmd name="dywiz" />
316 </xsl:template>
317
318 <xsl:template match="nbsp" mode="inline">
319     <spec cat="tilde" />
320 </xsl:template>
321
322 <xsl:template match="alien" mode="inline">
323     <group>
324         <cmd name="alien" />
325         <xsl:apply-templates mode="inline" />
326     </group>
327 </xsl:template>
328
329 <!-- ================ -->
330 <!-- = IGNORED TAGS = -->
331 <!-- ================ -->
332 <xsl:template match="extra|uwaga" />
333 <xsl:template match="extra|uwaga" mode="inline" />
334
335 <xsl:template match="nota_red" />
336
337 <!-- ======== -->
338 <!-- = TEXT = -->
339 <!-- ======== -->
340 <xsl:template match="text()" />
341 <xsl:template match="text()" mode="inline">
342     <xsl:if test="preceding-sibling::node() and wl:starts_white(.)">
343       <xsl:text> </xsl:text>
344     </xsl:if>
345
346     <xsl:value-of select="wl:substitute_entities(wl:strip(.))" />
347
348     <xsl:if test="following-sibling::node() and wl:ends_white(.)">
349       <xsl:text> </xsl:text>
350     </xsl:if>
351 </xsl:template>
352
353
354 </xsl:stylesheet>