7b5df582c607b6a6a80b628a972b8fe9b5e0adad
[librarian.git] / librarian / xslt / 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="@old-morefloats">
24                 <TeXML escape="0">
25                     \IfFileExists{morefloats.sty}{
26                         \usepackage{morefloats}
27                     }{}
28                 </TeXML>
29             </xsl:when>
30             <xsl:otherwise>
31                 <TeXML escape="0">
32                     \usepackage[maxfloats=64]{morefloats}
33                 </TeXML>
34             </xsl:otherwise>
35         </xsl:choose>
36
37         <xsl:apply-templates select="rdf:RDF" mode="titlepage" />
38         <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='titlepage' />
39
40         <env name="document">
41             <cmd name="maketitle" />
42
43             <xsl:choose>
44                 <xsl:when test="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/nazwa_utworu">
45                     <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/autor_utworu" mode="title" />
46                     <!-- title in master -->
47                 </xsl:when>
48                 <xsl:otherwise>
49                     <!-- look for author title in dc -->
50                     <xsl:apply-templates select="rdf:RDF" mode="firstdctitle" />
51                     <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='firstdctitle' />
52                 </xsl:otherwise>
53             </xsl:choose>
54             <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" />
55             <xsl:apply-templates select="utwor" mode="part" />
56         </env>
57     </TeXML>
58 </xsl:template>
59
60 <xsl:template match="utwor" mode="part">
61     <!-- title for empty dc -->
62     <xsl:choose>
63         <xsl:when test="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/nazwa_utworu">
64             <!-- title in master -->
65         </xsl:when>
66         <xsl:otherwise>
67             <!-- look for title in dc -->
68             <xsl:apply-templates select="rdf:RDF" mode="dctitle" />
69             <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='dctitle' />
70         </xsl:otherwise>
71     </xsl:choose>
72
73     <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" />
74     <xsl:apply-templates select="utwor" mode="part" />
75 </xsl:template>
76
77 <!-- =================== -->
78 <!-- = MAIN TITLE PAGE = -->
79 <!-- = (from DC)       = -->
80 <!-- =================== -->
81
82 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="titlepage">
83     <xsl:apply-templates select="rdf:RDF" mode="titlepage" />
84 </xsl:template>
85
86 <xsl:template match="rdf:RDF" mode="titlepage">
87     <cmd name='title'><parm>
88         <xsl:apply-templates select=".//dc:title/node()" mode="inline" />
89     </parm></cmd>
90     <cmd name='author'><parm>
91         <xsl:apply-templates select=".//dc:creator_parsed/node()" mode="inline" />
92     </parm></cmd>
93     <TeXML escape="0">
94         \def\sourceinfo{<TeXML escape="1"><xsl:apply-templates select=".//dc:source/node()" mode="inline" /></TeXML>}
95     </TeXML>
96 </xsl:template>
97
98
99 <!-- ============== -->
100 <!-- = BOOK TITLE = -->
101 <!-- = (from DC)  = -->
102 <!-- ============== -->
103
104 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="dctitle">
105     <xsl:apply-templates select="rdf:RDF" mode="dctitle" />
106 </xsl:template>
107
108 <xsl:template match="rdf:RDF" mode="dctitle">
109     <cmd name="section*"><parm>
110         <xsl:value-of select=".//dc:title/text()" />
111     </parm></cmd>
112 </xsl:template>
113
114
115 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="firstdctitle">
116     <xsl:apply-templates select="rdf:RDF" mode="firstdctitle" />
117 </xsl:template>
118
119 <xsl:template match="rdf:RDF" mode="firstdctitle">
120     <cmd name="subsection*"><parm>
121         <xsl:apply-templates select=".//dc:creator_parsed/node()" mode="inline" />
122     </parm></cmd>
123     <cmd name="section*"><parm>
124         <xsl:apply-templates select=".//dc:title/node()" mode="inline" />
125     </parm></cmd>
126 </xsl:template>
127
128
129 <!-- ============================================================================== -->
130 <!-- = MASTER TAG                                                                 = -->
131 <!-- = (can contain block tags, paragraph tags, standalone tags and special tags) = -->
132 <!-- ============================================================================== -->
133
134 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny">
135     <xsl:apply-templates />
136 </xsl:template>
137
138
139
140 <!-- ==================================================================================== -->
141 <!-- = BLOCK TAGS                                                                       = -->
142 <!-- = (can contain other block tags, paragraph tags, standalone tags and special tags) = -->
143 <!-- ==================================================================================== -->
144 <xsl:template match="nota">
145     <cmd name="par">
146         <parm><xsl:apply-templates /></parm>
147     </cmd>
148 </xsl:template>
149
150 <xsl:template match="lista_osob">
151     <cmd name="par"><parm>
152         <cmd name="textbf">
153             <parm><xsl:value-of select="naglowek_listy" /></parm>
154         </cmd>
155         <env name="itemize">
156             <xsl:apply-templates select="lista_osoba" />
157         </env>
158     </parm></cmd>
159 </xsl:template>
160
161 <xsl:template match="dedykacja">
162     <env name="em">
163         <env name="flushright">
164             <xsl:apply-templates/>
165         </env>
166     </env>
167 </xsl:template>
168
169 <xsl:template match="kwestia">
170     <cmd name="par">
171         <parm><xsl:apply-templates select="strofa|akap|didaskalia" /></parm>
172     </cmd>
173 </xsl:template>
174
175 <xsl:template match="dlugi_cytat">
176     <env name="quotation">
177         <xsl:apply-templates />
178     </env>
179 </xsl:template>
180
181 <xsl:template match="poezja_cyt">
182     <env name="verse">
183         <xsl:apply-templates />
184     </env>
185 </xsl:template>
186
187 <xsl:template match="motto">
188     <env name="em">
189         <xsl:apply-templates mode="inline" />
190     </env>
191 </xsl:template>
192
193
194 <!-- ========================================== -->
195 <!-- = PARAGRAPH TAGS                         = -->
196 <!-- = (can contain inline and special tags)  = -->
197 <!-- ========================================== -->
198
199 <!-- only in root -->
200 <xsl:template match="autor_utworu" mode="title">
201     <cmd name="subsection*"><parm>
202         <xsl:apply-templates mode="inline" />
203     </parm></cmd>
204 </xsl:template>
205
206 <xsl:template match="nazwa_utworu">
207     <cmd name="section*"><parm>
208         <xsl:apply-templates mode="inline" />
209     </parm></cmd>
210 </xsl:template>
211
212 <!-- Section headers (included in index)-->
213 <xsl:template match="naglowek_akt|naglowek_czesc|srodtytul">
214     <cmd name="subsection*">
215         <parm><xsl:apply-templates mode="inline" /></parm>
216     </cmd>
217 </xsl:template>
218
219 <xsl:template match="naglowek_scena|naglowek_rozdzial">
220     <cmd name="subsubsection*">
221         <parm><xsl:apply-templates mode="inline" /></parm>
222     </cmd>
223 </xsl:template>
224
225 <xsl:template match="naglowek_osoba|naglowek_podrozdzial">
226     <cmd name="par">
227         <parm><xsl:apply-templates mode="inline" /></parm>
228     </cmd><cmd name="nopagebreak" />
229 </xsl:template>
230
231 <!-- Other paragraph tags -->
232 <xsl:template match="miejsce_czas">
233     <cmd name="par"><parm>
234         <cmd name="emph">
235             <parm><xsl:apply-templates mode="inline" /></parm>
236         </cmd>
237     </parm></cmd>
238 </xsl:template>
239
240 <xsl:template match="didaskalia">
241     <cmd name="par"><parm>
242         <cmd name="emph">
243             <parm><xsl:apply-templates mode="inline" /></parm>
244         </cmd>
245     </parm></cmd>
246 </xsl:template>
247
248 <xsl:template match="lista_osoba">
249     <cmd name="item"><parm>
250         <xsl:apply-templates mode="inline" />
251     </parm></cmd>
252 </xsl:template>
253
254 <xsl:template match="akap|akap_dialog|akap_cd">
255     <cmd name="par"><parm>
256         <xsl:apply-templates mode="inline" />
257     </parm></cmd>
258 </xsl:template>
259
260 <xsl:template match="strofa">
261 <cmd name="par"><parm><cmd name="noindent"><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     <cmd name="vspace"><parm>1em</parm></cmd>
287 </parm></cmd></parm></cmd>
288 </xsl:template>
289
290
291 <xsl:template name="verse">
292     <xsl:param name="verse-content" />
293     <xsl:param name="verse-type" />
294         <xsl:choose><xsl:when test="name($verse-type) = 'wers_akap'"><cmd name="hspace" ><parm>1em</parm></cmd></xsl:when>
295             <xsl:when test="name($verse-type) = 'wers_wciety'">
296                 <xsl:choose>
297                     <xsl:when test="string($verse-content/@typ)">
298                         <cmd name="hspace" ><parm><xsl:value-of select="$verse-content/@typ" />em</parm></cmd>
299                     </xsl:when>
300                     <xsl:otherwise>
301                         <cmd name="hspace" ><parm>1em</parm></cmd>
302                     </xsl:otherwise>
303                 </xsl:choose>
304             </xsl:when>
305             <xsl:when test="name($verse-type) = 'wers_cd'">
306                 <cmd name="hspace" ><parm>8em</parm></cmd>
307             </xsl:when>
308         </xsl:choose>
309         <xsl:apply-templates select="$verse-content" mode="inline" />
310 </xsl:template>
311
312 <xsl:template match="motto_podpis">
313     <env name="em">
314         <env name="flushright">
315             <xsl:apply-templates mode="inline"/>
316         </env>
317     </env>
318 </xsl:template>
319
320 <!-- ================================================ -->
321 <!-- = INLINE TAGS                                  = -->
322 <!-- = (contain other inline tags and special tags) = -->
323 <!-- ================================================ -->
324
325 <!-- Annotations -->
326 <xsl:template match="pa|pe|pr|pt" mode="inline">
327     <cmd name="footnote"><parm><xsl:apply-templates mode="inline" /></parm></cmd>
328 </xsl:template>
329
330 <!-- Other inline tags -->
331 <xsl:template match="mat" mode="inline">
332     <math><xsl:apply-templates mode="inline" /></math>
333 </xsl:template>
334
335 <xsl:template match="didask_tekst" mode="inline">
336     <cmd name="emph"><parm><xsl:apply-templates mode="inline" /></parm></cmd>
337 </xsl:template>
338
339 <xsl:template match="slowo_obce" mode="inline">
340     <cmd name="emph"><parm><xsl:apply-templates mode="inline" /></parm></cmd>
341 </xsl:template>
342
343 <xsl:template match="tytul_dziela" mode="inline">
344     <cmd name="emph"><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 <xsl:template match="wyroznienie" mode="inline">
350     <cmd name="emph"><parm><xsl:apply-templates mode="inline" /></parm></cmd>
351 </xsl:template>
352
353 <xsl:template match="osoba" mode="inline">
354     <cmd name="textsc"><parm><xsl:apply-templates mode="inline" /></parm></cmd>
355 </xsl:template>
356
357
358 <!-- ============================================== -->
359 <!-- = STANDALONE TAGS                            = -->
360 <!-- = (cannot contain any other tags)            = -->
361 <!-- ============================================== -->
362 <xsl:template match="sekcja_swiatlo">
363     <cmd name="hspace"><parm>30pt</parm></cmd>
364 </xsl:template>
365
366 <xsl:template match="sekcja_asterysk">
367     <!-- TODO: padding? -->
368     <cmd name="par"><parm>*</parm></cmd>
369 </xsl:template>
370
371 <xsl:template match="separator_linia">
372     <cmd name="hrule" />
373 </xsl:template>
374
375
376
377 <!-- ================ -->
378 <!-- = SPECIAL TAGS = -->
379 <!-- ================ -->
380 <!-- Themes -->
381
382
383 <xsl:template match="begin|end|motyw">
384     <xsl:apply-templates select='.' mode="inline" />
385 </xsl:template>
386
387 <xsl:template match="begin" mode="inline" />
388 <xsl:template match="end" mode="inline" />
389
390 <xsl:template match="motyw" mode="inline">
391     <cmd name="mbox" />
392     <cmd name="marginpar">
393         <parm>
394             <cmd name="vspace"><parm>-8pt</parm></cmd>
395             <xsl:if test="@moved">
396                 <cmd name="vspace"><parm>-<xsl:value-of select="@moved" /><cmd name="baselineskip" /></parm></cmd>
397             </xsl:if>
398             <cmd name="raggedright"><parm>
399                 <cmd name="hspace"><parm>0pt</parm></cmd>
400                 <cmd name="footnotesize"><parm>
401                     <cmd name="color"><parm>theme</parm><parm>
402                         <xsl:value-of select="." />
403                     </parm></cmd>
404                 </parm></cmd>
405             </parm></cmd>
406         </parm>
407     </cmd>
408 </xsl:template>
409
410
411 <!-- ============== -->
412 <!-- = ADDED TAGS = -->
413 <!-- ============== -->
414
415
416 <xsl:template match="dywiz" mode="inline">
417     <cmd name="dywiz" />
418 </xsl:template>
419
420 <xsl:template match="nbsp" mode="inline">
421     <spec cat="tilde" />
422 </xsl:template>
423
424 <!-- ================ -->
425 <!-- = IGNORED TAGS = -->
426 <!-- ================ -->
427 <xsl:template match="extra|uwaga" />
428 <xsl:template match="extra|uwaga" mode="inline" />
429
430
431 <!-- ======== -->
432 <!-- = TEXT = -->
433 <!-- ======== -->
434 <xsl:template match="text()" />
435 <xsl:template match="text()" mode="inline">
436     <xsl:if test="preceding-sibling::node() and wl:starts_white(.)">
437       <xsl:text> </xsl:text>
438     </xsl:if>
439
440     <xsl:value-of select="wl:substitute_entities(wl:strip(.))" />
441
442     <xsl:if test="following-sibling::node() and wl:ends_white(.)">
443       <xsl:text> </xsl:text>
444     </xsl:if>
445 </xsl:template>
446
447
448 </xsl:stylesheet>