Optical improvements
[project/luci.git] / build / i18n-lua-xhtml1.xsl
1 <?xml version="1.0" encoding="utf-8"?>
2 <!--
3 Copyright (C) 2008  Alina Friedrichsen <x-alina@gmx.net>
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 SUCH DAMAGE.
25 -->
26
27 <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:i18n="http://luci.freifunk-halle.net/2008/i18n#" xmlns:h="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml" exclude-result-prefixes="i18n h">
28 <xsl:output method="text" encoding="utf-8" media-type="text/x-lua" indent="no"/>
29
30 <xsl:template match="/i18n:msgs">
31         <xsl:apply-templates select="i18n:msg" />
32 </xsl:template>
33
34 <xsl:template match="i18n:msg">
35         <xsl:if test="@xml:id != '' and translate(@xml:id, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz', '') = ''">
36                 <xsl:if test="translate(substring(@xml:id, 0, 2), 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', '') = ''">
37                         <xsl:if test="@xml:id != 'and' and @xml:id != 'break' and @xml:id != 'do' and @xml:id != 'else' and @xml:id != 'elseif' and @xml:id != 'end' and @xml:id != 'false' and @xml:id != 'for' and @xml:id != 'function' and @xml:id != 'if' and @xml:id != 'in' and @xml:id != 'local' and @xml:id != 'nil' and @xml:id != 'not' and @xml:id != 'or' and @xml:id != 'repeat' and @xml:id != 'return' and @xml:id != 'then' and @xml:id != 'true' and @xml:id != 'until' and @xml:id != 'while'">
38                                 <xsl:value-of select="@xml:id"/>
39                                 <xsl:text> = [[</xsl:text>
40                                 <xsl:apply-templates select="node()"/>
41                                 <xsl:text>]]&#10;</xsl:text>
42                         </xsl:if>
43                 </xsl:if>
44         </xsl:if>
45 </xsl:template>
46
47 <xsl:template match="h:*" priority="-1">
48         <xsl:text>&lt;</xsl:text>
49         <xsl:value-of select="local-name(.)"/>
50         <xsl:apply-templates select="@*"/>
51         <xsl:text>&gt;</xsl:text>
52         <xsl:apply-templates/>
53         <xsl:text>&lt;/</xsl:text>
54         <xsl:value-of select="local-name(.)"/>
55         <xsl:text>&gt;</xsl:text>
56 </xsl:template>
57
58 <xsl:template match="*" priority="-2">
59         <xsl:text>&lt;span class=&quot;</xsl:text>
60         <xsl:value-of select="local-name(.)"/>
61         <xsl:text>&quot;</xsl:text>
62         <xsl:apply-templates select="@*"/>
63         <xsl:text>&gt;</xsl:text>
64         <xsl:apply-templates/>
65         <xsl:text>&lt;/span&gt;</xsl:text>
66 </xsl:template>
67
68 <xsl:template match="h:br">
69         <xsl:text>&lt;br</xsl:text>
70         <xsl:apply-templates select="@*"/>
71         <xsl:text> /&gt;</xsl:text>
72 </xsl:template>
73
74 <xsl:template match="h:img">
75         <xsl:text>&lt;img</xsl:text>
76         <xsl:apply-templates select="@*"/>
77         <xsl:text> /&gt;</xsl:text>
78 </xsl:template>
79
80 <xsl:template match="@*" priority="-1">
81         <xsl:if test="namespace-uri(.) = '' and local-name(.) != 'lang' or namespace-uri(.) = 'http://www.w3.org/XML/1998/namespace'">
82                 <xsl:if test="namespace-uri(..) = 'http://www.w3.org/1999/xhtml' or local-name(.) != 'class'">
83                         <xsl:text> </xsl:text>
84                         <xsl:if test="namespace-uri(.) = 'http://www.w3.org/XML/1998/namespace'">
85                                 <xsl:text>xml:</xsl:text>
86                         </xsl:if>
87                         <xsl:value-of select="local-name(.)"/>
88                         <xsl:text>=&quot;</xsl:text>
89                         <xsl:variable name="escaped">
90                                 <xsl:call-template name="escape-lua-xhtml1">
91                                         <xsl:with-param name="string" select="."/>
92                                 </xsl:call-template>
93                         </xsl:variable>
94                         <xsl:value-of select="$escaped"/>
95                         <xsl:text>&quot;</xsl:text>
96                 </xsl:if>
97         </xsl:if>
98 </xsl:template>
99
100 <xsl:template match="@xml:lang">
101         <xsl:variable name="escaped">
102                 <xsl:call-template name="escape-lua-xhtml1">
103                         <xsl:with-param name="string" select="."/>
104                 </xsl:call-template>
105         </xsl:variable>
106         <xsl:text> xml:lang=&quot;</xsl:text>
107         <xsl:value-of select="$escaped"/>
108         <xsl:text>&quot;</xsl:text>
109         <xsl:text> lang=&quot;</xsl:text>
110         <xsl:value-of select="$escaped"/>
111         <xsl:text>&quot;</xsl:text>
112 </xsl:template>
113
114 <xsl:template match="text()" priority="-1">
115         <xsl:variable name="escaped">
116                 <xsl:call-template name="escape-lua-xhtml1">
117                         <xsl:with-param name="string" select="."/>
118                 </xsl:call-template>
119         </xsl:variable>
120         <xsl:value-of select="$escaped"/>
121 </xsl:template>
122
123 <xsl:template name="escape-xhtml1">
124         <xsl:param name="string"/>
125         <xsl:variable name="escaped">
126                 <xsl:call-template name="replace">
127                         <xsl:with-param name="string" select="$string"/>
128                         <xsl:with-param name="search" select="'&amp;'"/>
129                         <xsl:with-param name="replace" select="'&amp;amp;'"/>
130                 </xsl:call-template>
131         </xsl:variable>
132         <xsl:variable name="escaped1">
133                 <xsl:call-template name="replace">
134                         <xsl:with-param name="string" select="$escaped"/>
135                         <xsl:with-param name="search" select="'&quot;'"/>
136                         <xsl:with-param name="replace" select="'&amp;quot;'"/>
137                 </xsl:call-template>
138         </xsl:variable>
139         <xsl:variable name="escaped2">
140                 <xsl:call-template name="replace">
141                         <xsl:with-param name="string" select="$escaped1"/>
142                         <xsl:with-param name="search" select='"&#39;"'/>
143                         <xsl:with-param name="replace" select="'&amp;#39;'"/>
144                 </xsl:call-template>
145         </xsl:variable>
146         <xsl:variable name="escaped3">
147                 <xsl:call-template name="replace">
148                         <xsl:with-param name="string" select="$escaped2"/>
149                         <xsl:with-param name="search" select="'&lt;'"/>
150                         <xsl:with-param name="replace" select="'&amp;lt;'"/>
151                 </xsl:call-template>
152         </xsl:variable>
153         <xsl:call-template name="replace">
154                 <xsl:with-param name="string" select="$escaped3"/>
155                 <xsl:with-param name="search" select="'&gt;'"/>
156                 <xsl:with-param name="replace" select="'&amp;gt;'"/>
157         </xsl:call-template>
158 </xsl:template>
159
160 <xsl:template name="escape-lua-xhtml1">
161         <xsl:param name="string"/>
162         <xsl:variable name="escaped">
163                 <xsl:call-template name="escape-xhtml1">
164                         <xsl:with-param name="string" select="$string"/>
165                 </xsl:call-template>
166         </xsl:variable>
167         <xsl:variable name="escaped1">
168                 <xsl:call-template name="replace">
169                         <xsl:with-param name="string" select="$escaped"/>
170                         <xsl:with-param name="search" select="'='"/>
171                         <xsl:with-param name="replace" select="'&amp;#61;'"/>
172                 </xsl:call-template>
173         </xsl:variable>
174         <xsl:variable name="escaped2">
175                 <xsl:call-template name="replace">
176                         <xsl:with-param name="string" select="$escaped1"/>
177                         <xsl:with-param name="search" select="'['"/>
178                         <xsl:with-param name="replace" select="'&amp;#91;'"/>
179                 </xsl:call-template>
180         </xsl:variable>
181         <xsl:call-template name="replace">
182                 <xsl:with-param name="string" select="$escaped2"/>
183                 <xsl:with-param name="search" select="']'"/>
184                 <xsl:with-param name="replace" select="'&amp;#93;'"/>
185         </xsl:call-template>
186 </xsl:template>
187
188 <xsl:template name="replace">
189         <xsl:param name="string"/>
190         <xsl:param name="search"/>
191         <xsl:param name="replace"/>
192         <xsl:choose>
193                 <xsl:when test="contains($string, $search)">
194                         <xsl:value-of select="substring-before($string, $search)"/>
195                         <xsl:value-of select="$replace"/>
196                         <xsl:call-template name="replace">
197                                 <xsl:with-param name="string" select="substring-after($string, $search)"/>
198                                 <xsl:with-param name="search" select="$search"/>
199                                 <xsl:with-param name="replace" select="$replace"/>
200                         </xsl:call-template>
201                 </xsl:when>
202                 <xsl:otherwise>
203                         <xsl:value-of select="$string"/>
204                 </xsl:otherwise>
205         </xsl:choose>
206 </xsl:template>
207
208 </xsl:transform>