cd31ef1b201c26524a74850c6c685a88c7459ece
[librarian.git] / librarian / wl2html_base.xslt
1
2 <xsl:stylesheet
3     version="1.0"
4     
5     xmlns="http://www.w3.org/1999/xhtml"
6     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   
7     xmlns:wl2o="http://nowoczesnapolska.org.pl/WL/2.0/Overlay"   
8     xmlns:wl="http://wolnelektury.pl/functions"
9
10     exclude-result-prefixes="wl" >
11
12     <xsl:variable name="config" select="document('config.xml')" />
13
14     <xsl:output method="xml"
15         encoding="utf-8"
16         indent="yes"
17         omit-xml-declaration = "yes" />
18
19     <xsl:strip-space elements = "strofa utwor kwestia liryka_l liryka_lp powiesc opowiadanie dramat_wierszowany_lp" />
20     <!--     
21         Dokument ten opisuje podstawowe przeksztaƂcenia potrzebne
22      do zamiany dokumentu WLML 1.0 na poprawnie sformatowany
23      dokument XHMTL.
24
25     -->
26
27     <xsl:template name="generic-attributes">
28         <xsl:param name="element" />
29         <xsl:param name="mypath" />
30         <xsl:variable name="tag" select="name($element)" />
31
32         <xsl:if test="$with-paths">
33         <xsl:attribute name="wl2o:path">
34             <xsl:value-of select="$mypath" />
35         </xsl:attribute>
36         </xsl:if>
37
38         <xsl:if test="$config//editable/*[name() = $tag]">
39             <xsl:attribute name="wl2o:editable">editable</xsl:attribute>
40         </xsl:if>
41
42         <xsl:attribute name="class">
43             <xsl:value-of select="$tag"/>
44         </xsl:attribute>
45     </xsl:template>
46
47     <xsl:template name="generic-descent">
48         <xsl:param name="element" />
49         <xsl:param name="mypath" />
50         
51         <xsl:for-each select="child::node()">            
52             <xsl:apply-templates select="." mode="element-tag">
53                 <xsl:with-param name="offset" select="position()" />
54                 <xsl:with-param name="parent-path" select="$mypath" />
55             </xsl:apply-templates>
56         </xsl:for-each>
57     </xsl:template>
58     
59     <xsl:template name="generic-content">
60         <xsl:param name="element" />
61         <xsl:param name="mypath" />
62
63         <xsl:call-template name="generic-attributes">
64             <xsl:with-param name="element" select="$element" />
65             <xsl:with-param name="mypath" select="$mypath" />
66         </xsl:call-template>
67
68         <xsl:call-template name="generic-descent">
69             <xsl:with-param name="element" select="$element" />
70             <xsl:with-param name="mypath" select="$mypath" />
71         </xsl:call-template>
72     </xsl:template>
73     
74     <!-- Generyczne szablony -->
75     <xsl:template name="generic" >
76         <xsl:param name="element" />
77         <xsl:param name="mypath" />
78         <xsl:param name="offset" />
79
80         <!-- <xsl:param name="parent-type" select="'block'" /> -->
81
82         <xsl:variable name="tag" select="name($element)" />        
83             
84         <xsl:choose>            
85             <!-- ignore namespaced elements -->
86             <xsl:when test="namespace-uri()" />
87
88             <xsl:when test="$config//block-elements/*[local-name() = $tag]">
89                 <xsl:element name="div" namespace="http://www.w3.org/1999/xhtml">
90                     <xsl:apply-templates select="$element" mode="element-content" >
91                         <xsl:with-param name="mypath" select="$mypath"/>
92                     </xsl:apply-templates>
93                 </xsl:element>
94             </xsl:when>
95
96             <xsl:when test="$config//paragraph-elements/*[local-name() = $tag]">
97                 <xsl:element name="p" namespace="http://www.w3.org/1999/xhtml">                    
98                         <xsl:apply-templates select="$element" mode="element-content" >
99                         <xsl:with-param name="mypath" select="$mypath"/>
100                     </xsl:apply-templates>
101                 </xsl:element>
102             </xsl:when>
103             
104             <xsl:when test="$config//inline-elements/*[local-name() = $tag]">
105                 <xsl:element name="span" namespace="http://www.w3.org/1999/xhtml">
106                     <xsl:apply-templates select="$element" mode="element-content" >
107                         <xsl:with-param name="mypath" select="$mypath"/>
108                     </xsl:apply-templates>
109                 </xsl:element>
110             </xsl:when>
111
112             <xsl:when test="$config//header-1-elements/*[local-name() = $tag]">
113                 <xsl:element name="h1" namespace="http://www.w3.org/1999/xhtml">
114                     <xsl:apply-templates select="$element" mode="element-content" >
115                         <xsl:with-param name="mypath" select="$mypath"/>
116                     </xsl:apply-templates>
117                 </xsl:element>
118             </xsl:when>
119
120             <xsl:when test="$config//header-2-elements/*[local-name() = $tag]">
121                 <xsl:element name="h2" namespace="http://www.w3.org/1999/xhtml">
122                     <xsl:apply-templates select="$element" mode="element-content" >
123                         <xsl:with-param name="mypath" select="$mypath"/>
124                     </xsl:apply-templates>
125                 </xsl:element>
126             </xsl:when>
127
128             <xsl:when test="$config//header-3-elements/*[local-name() = $tag]">
129                 <xsl:element name="h3" namespace="http://www.w3.org/1999/xhtml">
130                     <xsl:apply-templates select="$element" mode="element-content" >
131                         <xsl:with-param name="mypath" select="$mypath"/>
132                     </xsl:apply-templates>
133                 </xsl:element>
134             </xsl:when>
135
136             <xsl:when test="$config//header-4-elements/*[local-name() = $tag]">
137                 <xsl:element name="h4" namespace="http://www.w3.org/1999/xhtml">
138                     <xsl:apply-templates select="$element" mode="element-content" >
139                         <xsl:with-param name="mypath" select="$mypath"/>
140                     </xsl:apply-templates>
141                 </xsl:element>
142             </xsl:when>
143
144             <xsl:when test="$config//no-show-elements/*[local-name() = $tag]" />
145
146             <xsl:otherwise>
147                 <xsl:message terminate="yes">
148                     Nieznany tag '<xsl:value-of select="$tag" />' :(.
149                 </xsl:message>
150             </xsl:otherwise>
151         </xsl:choose>
152     </xsl:template>
153     
154
155     <!--
156     <special-tags>
157         <strofa />
158         <lista_osob />
159         <sekcja_swiatlo />
160         <sekcja_asterysk />
161         <separator_linia />
162     </special-tags>
163     -->    
164
165     <xsl:template match="dlugi_cytat|poezja_cyt" mode="element-tag">
166         <xsl:param name="offset" />
167         <xsl:param name="parent-path" />
168         <xsl:variable name="mypath"
169             select="concat($parent-path, '/', name(), '[', string($offset),']')" />
170
171         <xsl:element name="blockquote" >
172             <xsl:call-template name="generic-attributes">
173                 <xsl:with-param name="element" select="current()" />
174                 <xsl:with-param name="mypath" select="$mypath" />
175             </xsl:call-template>
176             <xsl:call-template name="generic-descent">
177                 <xsl:with-param name="element" select="current()" />
178                 <xsl:with-param name="mypath" select="$mypath" />
179             </xsl:call-template>
180         </xsl:element>
181     </xsl:template>
182
183
184     <xsl:template match="lista_osob" mode="element-tag">
185         <xsl:param name="offset" />
186         <xsl:param name="parent-path" />
187         <xsl:variable name="mypath"
188             select="concat($parent-path, '/', name(), '[', string($offset),']')" />
189
190         <xsl:element name="div" >
191             <xsl:call-template name="generic-attributes">
192                 <xsl:with-param name="element" select="current()" />
193                 <xsl:with-param name="mypath" select="$mypath" />
194             </xsl:call-template>
195
196             <xsl:apply-templates select="./naglowek-listy" mode="element-tag" />
197             <ul>
198                 <xsl:for-each select="./lista_osoba">
199                 <xsl:apply-templates select="." mode="element-tag">
200                     <xsl:with-param name="offset" select="position()" />
201                     <xsl:with-param name="parent-path" select="$mypath" />
202                 </xsl:apply-templates>
203                 </xsl:for-each>
204             </ul>
205         </xsl:element>
206     </xsl:template>
207
208     <xsl:template match="lista_osoba" mode="element-tag">
209         <xsl:param name="offset" />
210         <xsl:param name="parent-path" />
211         <xsl:variable name="mypath"
212             select="concat($parent-path, '/', name(), '[', string($offset),']')" />
213
214         <xsl:element name="li" >
215             <xsl:call-template name="generic-attributes">
216                 <xsl:with-param name="element" select="current()" />
217                 <xsl:with-param name="mypath" select="$mypath" />
218             </xsl:call-template>
219             <xsl:call-template name="generic-descent">
220                 <xsl:with-param name="element" select="current()" />
221                 <xsl:with-param name="mypath" select="$mypath" />
222             </xsl:call-template>
223         </xsl:element>
224     </xsl:template>
225
226     <xsl:template match="separator_linia" mode="element-tag">
227         <xsl:param name="offset" />
228         <xsl:param name="parent-path" />
229         <xsl:variable name="mypath"
230             select="concat($parent-path, '/', name(), '[', string($offset),']')" />
231
232         <xsl:element name="hr" >
233             <xsl:call-template name="generic-attributes">
234                 <xsl:with-param name="element" select="current()" />
235                 <xsl:with-param name="mypath" select="$mypath" />
236             </xsl:call-template>            
237         </xsl:element>
238     </xsl:template>
239
240     <xsl:template match="sekcja_swiatlo" mode="element-tag">
241         <xsl:param name="offset" />
242         <xsl:param name="parent-path" />
243         <xsl:variable name="mypath"
244             select="concat($parent-path, '/', name(), '[', string($offset),']')" />
245
246         <xsl:element name="br" >
247             <xsl:call-template name="generic-attributes">
248                 <xsl:with-param name="element" select="current()" />
249                 <xsl:with-param name="mypath" select="$mypath" />
250             </xsl:call-template>
251         </xsl:element>
252     </xsl:template>
253
254     <xsl:template match="sekcja_asterysk" mode="element-tag">
255         <xsl:param name="offset" />
256         <xsl:param name="parent-path" />
257         <xsl:variable name="mypath"
258             select="concat($parent-path, '/', name(), '[', string($offset),']')" />
259
260         <xsl:element name="p" >
261             <xsl:call-template name="generic-attributes">
262                 <xsl:with-param name="element" select="current()" />
263                 <xsl:with-param name="mypath" select="$mypath" />
264             </xsl:call-template>
265             *
266         </xsl:element>
267     </xsl:template>
268
269     <xsl:template match="zastepnik_wersu|wers_akap|wers_cd|wers_wciety" mode="element-tag">
270         <xsl:param name="offset" />
271         <xsl:param name="parent-path" />
272
273         <xsl:variable name="mypath"
274             select="concat($parent-path, '/', name(), '[',string($offset),']')" />
275        
276         <xsl:call-template name="generic-descent">
277             <xsl:with-param name="element" select="current()" />
278             <xsl:with-param name="mypath" select="$mypath" />
279         </xsl:call-template>        
280     </xsl:template>
281
282     <!-- strofy -->
283     <xsl:template match="strofa" mode="element-tag">
284         <xsl:param name="offset" />
285         <xsl:param name="parent-path" />
286
287         <xsl:variable name="mypath"
288             select="concat($parent-path, '/', name(), '[', string($offset),']')" />
289
290         <xsl:element name="div" >            
291             <xsl:call-template name="generic-attributes">
292                 <xsl:with-param name="element" select="current()" />
293                 <xsl:with-param name="mypath" select="$mypath" />
294             </xsl:call-template>
295
296             <xsl:choose>
297                 <xsl:when test="count(br) > 0">
298                     <xsl:call-template name="verse">
299                         <xsl:with-param name="verse-content" select="br[1]/preceding-sibling::text() | br[1]/preceding-sibling::node()" />
300                         <xsl:with-param name="verse-type" select="br[1]/preceding-sibling::*[name() = 'wers_wciety' or name() = 'wers_akap' or name() = 'wers_cd'][1]" />
301                         <xsl:with-param name="mypath" select="$mypath" />
302                     </xsl:call-template>
303                     <xsl:for-each select="br">
304                                 <!-- Each BR tag "consumes" text after it -->
305                         <xsl:variable name="lnum" select="count(preceding-sibling::br)" />
306                         <xsl:call-template name="verse">
307                             <xsl:with-param name="verse-content"
308                                 select="following-sibling::text()[count(preceding-sibling::br) = $lnum+1] | following-sibling::node()[count(preceding-sibling::br) = $lnum+1]" />
309                             <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]" />
310                             <xsl:with-param name="mypath" select="$mypath" />
311                         </xsl:call-template>
312                     </xsl:for-each>
313                 </xsl:when>
314                 <xsl:otherwise>
315                     <xsl:call-template name="verse">
316                         <xsl:with-param name="verse-content" select="child::node()" />
317                         <xsl:with-param name="verse-type" select="wers_wciety|wers_akap|wers_cd[1]" />
318                         <xsl:with-param name="mypath" select="$mypath" />
319                     </xsl:call-template>
320                 </xsl:otherwise>
321             </xsl:choose>
322         </xsl:element>
323     </xsl:template>
324
325     <xsl:template name="verse">
326         <xsl:param name="verse-content" />
327         <xsl:param name="verse-type" />
328         <xsl:param name="mypath" />
329
330         <xsl:element name="p">
331             <xsl:attribute name="class">
332                 <xsl:value-of select="name($verse-type)" />
333             </xsl:attribute>
334             <xsl:for-each select="$verse-content">
335                 <xsl:apply-templates select="." mode="element-tag">
336                     <xsl:with-param name="offset" select="position()" />
337                     <xsl:with-param name="parent-path" select="$mypath" />
338                 </xsl:apply-templates>
339             </xsl:for-each>
340         </xsl:element>
341     </xsl:template>
342
343
344 <!-- default content processing -->
345     <xsl:template match="*" mode="element-content">
346         <xsl:param name="mypath" />
347         <xsl:call-template name="generic-content">
348             <xsl:with-param name="element" select="current()"/>
349             <xsl:with-param name="mypath" select="$mypath"/>
350         </xsl:call-template>
351     </xsl:template>
352
353     <xsl:template match="*" mode="element-tag" >
354         <xsl:param name="offset" />
355         <xsl:param name="parent-path" />
356
357         <xsl:variable name="mypath"
358             select="concat($parent-path, '/', name(), '[', string($offset),']')" />
359
360         <xsl:call-template name="generic">
361             <xsl:with-param name="element" select="current()" />
362             <xsl:with-param name="offset" select="$offset" />
363             <xsl:with-param name="mypath" select="$mypath" />
364         </xsl:call-template>
365     </xsl:template>
366
367     <xsl:template match="text()" mode="element-tag">
368         
369         <xsl:value-of select="wl:substitute_entities(.)" />
370         
371         <!--<xsl:value-of select="." /> -->
372     </xsl:template>
373
374     <xsl:template match="node()" />
375     
376 </xsl:stylesheet>