epub: remove reduntant toc entry, fix validation
[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
16 <xsl:template match="utwor">
17     <TeXML xmlns="http://getfo.sourceforge.net/texml/ns1">
18         <TeXML escape="0">
19         \documentclass[a4paper, oneside, 11pt]{book}
20         \usepackage[MeX]{polski}
21         \usepackage[utf8]{inputenc}
22         \pagestyle{plain}
23         \usepackage{antpolt}
24         \usepackage[bottom]{footmisc}
25
26         \usepackage{color}
27         \definecolor{theme-gray}{gray}{.3}
28
29
30         \setlength{\marginparsep}{2em}
31         \setlength{\marginparwidth}{8.5em}
32         \setlength{\oddsidemargin}{0pt}
33         \clubpenalty=10000
34         \widowpenalty=10000 
35         </TeXML>
36
37         <xsl:apply-templates select="rdf:RDF" mode='title' />
38         <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='title' />
39
40         <env name="document">
41             <cmd name="maketitle" />
42             <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" />
43             <xsl:apply-templates select="utwor" mode="part" />
44         </env>
45     </TeXML>
46 </xsl:template>
47
48 <xsl:template match="utwor" mode="part">
49     <xsl:if test="utwor">
50         <xsl:apply-templates select="rdf:RDF" mode='subtitle' />
51         <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='subtitle' />
52     </xsl:if>
53     <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" />
54     <xsl:apply-templates select="utwor" mode="part" />
55 </xsl:template>
56
57
58 <!-- ============================================================================== -->
59 <!-- = MASTER TAG                                                                 = -->
60 <!-- = (can contain block tags, paragraph tags, standalone tags and special tags) = -->
61 <!-- ============================================================================== -->
62
63 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="title">
64     <xsl:apply-templates select="rdf:RDF" mode='title' />
65 </xsl:template>
66
67 <xsl:template match="rdf:RDF" mode="title">
68
69 <!-- TODO!
70             <cmd name='title'><parm>
71                 <xsl:apply-templates select="dzielo_nadrzedne|podtytul" mode="header" />
72             </parm></cmd>
73             czytanie z tagów nazwa_utworu, autor_utworu
74 -->
75
76                 <cmd name='title'><parm>
77                     <xsl:apply-templates select=".//dc:title" mode="header" />
78                 </parm></cmd>
79                 <cmd name='author'><parm>
80                     <xsl:apply-templates select=".//dc:author" mode="header" />
81                 </parm></cmd>
82 </xsl:template>
83
84 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="subtitle">
85     <xsl:apply-templates select="rdf:RDF" mode='subtitle' />
86 </xsl:template>
87
88 <xsl:template match="rdf:RDF" mode="subtitle">
89                 <cmd name='part*'><parm>
90                     <xsl:apply-templates select=".//dc:title" mode="header" />
91                 </parm></cmd>
92 </xsl:template>
93
94
95
96 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny">
97     <xsl:apply-templates />
98 </xsl:template>
99
100
101
102 <!-- ==================================================================================== -->
103 <!-- = BLOCK TAGS                                                                       = -->
104 <!-- = (can contain other block tags, paragraph tags, standalone tags and special tags) = -->
105 <!-- ==================================================================================== -->
106 <xsl:template match="nota">
107     <cmd name="par">
108         <parm><xsl:apply-templates /></parm>
109     </cmd>
110 </xsl:template>
111
112 <xsl:template match="lista_osob">
113     <cmd name="par">
114         <cmd name="textbf">
115             <parm><xsl:value-of select="naglowek_listy" /></parm>
116         </cmd>
117         <env name="itemize">
118             <xsl:apply-templates select="lista_osoba" />
119         </env>
120     </cmd>
121 </xsl:template>
122
123 <xsl:template match="dedykacja">
124     <cmd name="raggedleft"><parm>
125         <env name="em">
126             <xsl:apply-templates />
127         </env>
128     </parm></cmd>
129 </xsl:template>
130
131 <xsl:template match="kwestia">
132     <cmd name="par">
133         <parm><xsl:apply-templates select="strofa|akap|didaskalia" /></parm>
134     </cmd>
135 </xsl:template>
136
137 <xsl:template match="dlugi_cytat">
138     <env name="quotation">
139         <xsl:apply-templates />
140     </env>
141 </xsl:template>
142
143 <xsl:template match="poezja_cyt">
144     <env name="verse">
145         <xsl:apply-templates />
146     </env>
147 </xsl:template>
148
149 <xsl:template match="motto">
150     <env name="em">
151         <xsl:apply-templates mode="inline" />
152     </env>
153 </xsl:template>
154
155
156 <!-- ========================================== -->
157 <!-- = PARAGRAPH TAGS                         = -->
158 <!-- = (can contain inline and special tags)  = -->
159 <!-- ========================================== -->
160 <!-- Title page -->
161 <xsl:template match="autor_utworu" mode="header">
162     <xsl:apply-templates mode="inline" />
163 </xsl:template>
164
165 <xsl:template match="nazwa_utworu" mode="header">
166     <xsl:apply-templates mode="inline" />
167 </xsl:template>
168
169 <xsl:template match="dzielo_nadrzedne" mode="header">
170     <xsl:apply-templates mode="inline" />
171 </xsl:template>
172
173 <xsl:template match="podtytul" mode="header">
174     <xsl:apply-templates mode="inline" />
175 </xsl:template>
176
177
178 <xsl:template match="nazwa_utworu">
179     <cmd name="pagebreak" />
180     <cmd name="section*"><parm>
181         <xsl:apply-templates mode="inline" />
182     </parm></cmd>
183 </xsl:template>
184
185 <!-- Section headers (included in index)-->
186 <xsl:template match="naglowek_akt|naglowek_czesc|srodtytul">
187     <cmd name="subsection*">
188         <parm><xsl:apply-templates mode="inline" /></parm>
189     </cmd>
190 </xsl:template>
191
192 <xsl:template match="naglowek_scena|naglowek_rozdzial">
193     <cmd name="subsubsection*">
194         <parm><xsl:apply-templates mode="inline" /></parm>
195     </cmd>
196 </xsl:template>
197
198 <xsl:template match="naglowek_osoba|naglowek_podrozdzial">
199     <cmd name="paragraph*">
200         <parm><xsl:apply-templates mode="inline" /></parm>
201     </cmd>
202 </xsl:template>
203
204 <!-- Other paragraph tags -->
205 <xsl:template match="miejsce_czas">
206     <cmd name="par"><parm>
207         <cmd name="emph">
208             <parm><xsl:apply-templates mode="inline" /></parm>
209         </cmd>
210     </parm></cmd>
211 </xsl:template>
212
213 <xsl:template match="didaskalia">
214     <cmd name="par"><parm>
215         <cmd name="emph">
216             <parm><xsl:apply-templates mode="inline" /></parm>
217         </cmd>
218     </parm></cmd>
219 </xsl:template>
220
221 <xsl:template match="lista_osoba">
222     <cmd name="item"><parm>
223         <xsl:apply-templates mode="inline" />
224     </parm></cmd>
225 </xsl:template>
226
227 <xsl:template match="akap|akap_dialog|akap_cd">
228     <cmd name="par"><parm>
229         <xsl:apply-templates mode="inline" />
230     </parm></cmd>
231 </xsl:template>
232
233 <xsl:template match="strofa">
234 <cmd name="par"><parm><cmd name="noindent"><parm>
235             <xsl:choose>
236             <xsl:when test="count(br) > 0">
237                 <xsl:call-template name="verse">
238                     <xsl:with-param name="verse-content" select="br[1]/preceding-sibling::text() | br[1]/preceding-sibling::node()" />
239                     <xsl:with-param name="verse-type" select="br[1]/preceding-sibling::*[name() = 'wers_wciety' or name() = 'wers_akap' or name() = 'wers_cd'][1]" />
240                 </xsl:call-template>
241                 <xsl:for-each select="br">
242                     <cmd name="newline" />
243                     <!-- Each BR tag "consumes" text after it -->
244                     <xsl:variable name="lnum" select="count(preceding-sibling::br)" />
245                     <xsl:call-template name="verse">
246                         <xsl:with-param name="verse-content"
247                             select="following-sibling::text()[count(preceding-sibling::br) = $lnum+1] | following-sibling::node()[count(preceding-sibling::br) = $lnum+1]" />
248                         <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]" />
249                     </xsl:call-template>
250                 </xsl:for-each>
251             </xsl:when>
252             <xsl:otherwise>
253                 <xsl:call-template name="verse">
254                     <xsl:with-param name="verse-content" select="text() | node()" />
255                     <xsl:with-param name="verse-type" select="wers_wciety|wers_akap|wers_cd[1]" />
256                  </xsl:call-template>
257             </xsl:otherwise>
258         </xsl:choose>
259     <cmd name="vspace"><parm>1em</parm></cmd>
260 </parm></cmd></parm></cmd>
261 </xsl:template>
262
263
264 <xsl:template name="verse">
265     <xsl:param name="verse-content" />
266     <xsl:param name="verse-type" />
267         <xsl:choose><xsl:when test="name($verse-type) = 'wers_akap'"><cmd name="hspace" ><parm><xsl:value-of select="$firet" />pt</parm></cmd></xsl:when>
268             <xsl:when test="name($verse-type) = 'wers_wciety'">
269                 <xsl:choose>
270                     <xsl:when test="$verse-content/@typ">
271                         <cmd name="hspace" ><parm><xsl:value-of select="$verse-content/@typ" />em</parm></cmd>
272                     </xsl:when>
273                     <xsl:otherwise>
274                         <cmd name="hspace" ><parm>1em</parm></cmd>
275                     </xsl:otherwise>
276                 </xsl:choose>
277             </xsl:when>
278             <xsl:when test="name($verse-type) = 'wers_cd'">
279                 <cmd name="hspace" ><parm>12em</parm></cmd>
280             </xsl:when>
281         </xsl:choose>
282         <xsl:apply-templates select="$verse-content" mode="inline" />
283 </xsl:template>
284
285 <xsl:template match="motto_podpis">
286     <cmd name="raggedleft"><parm>
287         <xsl:apply-templates mode="inline" />
288     </parm></cmd>
289 </xsl:template>
290
291
292 <!-- ================================================ -->
293 <!-- = INLINE TAGS                                  = -->
294 <!-- = (contain other inline tags and special tags) = -->
295 <!-- ================================================ -->
296 <!-- Annotations -->
297 <xsl:template match="pa|pe|pr|pt" mode="inline">
298     <cmd name="footnote"><parm><xsl:apply-templates mode="inline" /></parm></cmd>
299 </xsl:template>
300
301 <!-- Other inline tags -->
302 <xsl:template match="mat" mode="inline">
303     <math><xsl:apply-templates mode="inline" /></math>
304 </xsl:template>
305
306 <xsl:template match="didask_tekst" mode="inline">
307     <cmd name="emph"><parm><xsl:apply-templates mode="inline" /></parm></cmd>
308 </xsl:template>
309
310 <xsl:template match="slowo_obce" mode="inline">
311     <cmd name="emph"><parm><xsl:apply-templates mode="inline" /></parm></cmd>
312 </xsl:template>
313
314 <xsl:template match="tytul_dziela" mode="inline">
315     <cmd name="emph"><parm>
316         <xsl:if test="@typ = '1'">„</xsl:if><xsl:apply-templates mode="inline" /><xsl:if test="@typ = '1'">”</xsl:if>
317     </parm></cmd>
318 </xsl:template>
319
320 <xsl:template match="wyroznienie" mode="inline">
321     <!-- TODO: letterspacing 1pt -->
322     <cmd name="emph"><parm><xsl:apply-templates mode="inline" /></parm></cmd>
323 </xsl:template>
324
325 <xsl:template match="osoba" mode="inline">
326     <cmd name="textsc"><parm><xsl:apply-templates mode="inline" /></parm></cmd>
327 </xsl:template>
328
329
330 <!-- ============================================== -->
331 <!-- = STANDALONE TAGS                            = -->
332 <!-- = (cannot contain any other tags)            = -->
333 <!-- ============================================== -->
334 <xsl:template match="sekcja_swiatlo">
335     <cmd name="hspace"><parm>30pt</parm></cmd>
336 </xsl:template>
337
338 <xsl:template match="sekcja_asterysk">
339     <!-- TODO: padding? -->
340     <cmd name="par"><parm>*</parm></cmd>
341 </xsl:template>
342
343 <xsl:template match="separator_linia">
344     <cmd name="hrule" />
345 </xsl:template>
346
347
348
349 <!-- ================ -->
350 <!-- = SPECIAL TAGS = -->
351 <!-- ================ -->
352 <!-- Themes -->
353
354
355 <xsl:template match="begin" mode="inline">
356     <xsl:variable name="mnum" select="concat('m', substring(@id, 2))" />
357     <cmd name="mbox" />
358     <cmd name="marginpar">
359         <parm><cmd name="raggedright"><parm>
360             <cmd name="hspace"><parm>0pt</parm></cmd>
361             <cmd name="footnotesize"><parm>
362                 <cmd name="color"><parm>theme-gray</parm><parm>
363                     <xsl:value-of select="string(following::motyw[@id=$mnum]/text())" />
364                 </parm></cmd>
365             </parm></cmd>
366         </parm></cmd></parm>
367     </cmd>
368 </xsl:template>
369
370 <xsl:template match="begin|end">
371     <xsl:apply-templates select='.' mode="inline" />
372 </xsl:template>
373
374 <xsl:template match="end" mode="inline" />
375 <xsl:template match="motyw" mode="inline" />
376
377
378 <!-- ============== -->
379 <!-- = ADDED TAGS = -->
380 <!-- ============== -->
381
382
383 <xsl:template match="dywiz" mode="inline">
384     <cmd name="dywiz" />
385 </xsl:template>
386
387 <xsl:template match="nbsp" mode="inline">
388     <spec cat="tilde" />
389 </xsl:template>
390
391 <!-- ================ -->
392 <!-- = IGNORED TAGS = -->
393 <!-- ================ -->
394 <xsl:template match="extra|uwaga" />
395 <xsl:template match="extra|uwaga" mode="inline" />
396
397
398 <!-- ======== -->
399 <!-- = TEXT = -->
400 <!-- ======== -->
401 <xsl:template match="text()" />
402 <xsl:template match="text()" mode="inline">
403     <xsl:value-of select="wl:substitute_entities(.)" />
404 </xsl:template>
405
406
407 </xsl:stylesheet>