Added XML -> Lua translation transform target to Makefiles
authorSteven Barth <steven@midlink.org>
Fri, 15 Aug 2008 15:50:43 +0000 (15:50 +0000)
committerSteven Barth <steven@midlink.org>
Fri, 15 Aug 2008 15:50:43 +0000 (15:50 +0000)
Makefile
build/config.mk
build/i18n-lua-xhtml1.xsl [new file with mode: 0644]
build/module.mk

index a9e5e1eb238bd6173ccce4b8de62d99543236f8e..558ee352bf9815eef3425be311b99d0d9c9ac825 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -15,7 +15,7 @@ gccbuild:
        for i in $(MODULES); do make -C$$i compile; done        
 
 luabuild:
-       for i in $(MODULES); do make -C$$i lua$(LUA_TARGET); done
+       for i in $(MODULES); do make -C$$i luabuild; done
 
 clean:
        rm -rf docs
index 5a8f22f89964b2bba303240b33cc15b9985a669e..6f54ce591ebb51c15521c9df477b74cf76f10cf7 100644 (file)
@@ -1,6 +1,6 @@
 LUAC = luac
 LUAC_OPTIONS = -s
-LUA_TARGET = source
+LUA_TARGET ?= source
 
 LUA_MODULEDIR = /usr/lib/lua
 LUA_LIBRARYDIR = /usr/lib/lua
@@ -10,3 +10,4 @@ LUCI_LIBRARYDIR = $(LUA_LIBRARYDIR)/luci
 
 HTDOCS = /www
 
+XSLTPROC=$(shell which xsltproc)
diff --git a/build/i18n-lua-xhtml1.xsl b/build/i18n-lua-xhtml1.xsl
new file mode 100644 (file)
index 0000000..82dc604
--- /dev/null
@@ -0,0 +1,208 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+Copyright (C) 2008  Alina Friedrichsen <x-alina@gmx.net>
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in the
+   documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+-->
+
+<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">
+<xsl:output method="text" encoding="utf-8" media-type="text/x-lua" indent="no"/>
+
+<xsl:template match="/i18n:msgs">
+       <xsl:apply-templates select="i18n:msg" />
+</xsl:template>
+
+<xsl:template match="i18n:msg">
+       <xsl:if test="@xml:id != '' and translate(@xml:id, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz', '') = ''">
+               <xsl:if test="translate(substring(@xml:id, 0, 2), 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', '') = ''">
+                       <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'">
+                               <xsl:value-of select="@xml:id"/>
+                               <xsl:text> = [[</xsl:text>
+                               <xsl:apply-templates select="node()"/>
+                               <xsl:text>]]&#10;</xsl:text>
+                       </xsl:if>
+               </xsl:if>
+       </xsl:if>
+</xsl:template>
+
+<xsl:template match="h:*" priority="-1">
+       <xsl:text>&lt;</xsl:text>
+       <xsl:value-of select="local-name(.)"/>
+       <xsl:apply-templates select="@*"/>
+       <xsl:text>&gt;</xsl:text>
+       <xsl:apply-templates/>
+       <xsl:text>&lt;/</xsl:text>
+       <xsl:value-of select="local-name(.)"/>
+       <xsl:text>&gt;</xsl:text>
+</xsl:template>
+
+<xsl:template match="*" priority="-2">
+       <xsl:text>&lt;span class=&quot;</xsl:text>
+       <xsl:value-of select="local-name(.)"/>
+       <xsl:text>&quot;</xsl:text>
+       <xsl:apply-templates select="@*"/>
+       <xsl:text>&gt;</xsl:text>
+       <xsl:apply-templates/>
+       <xsl:text>&lt;/span&gt;</xsl:text>
+</xsl:template>
+
+<xsl:template match="h:br">
+       <xsl:text>&lt;br</xsl:text>
+       <xsl:apply-templates select="@*"/>
+       <xsl:text> /&gt;</xsl:text>
+</xsl:template>
+
+<xsl:template match="h:img">
+       <xsl:text>&lt;img</xsl:text>
+       <xsl:apply-templates select="@*"/>
+       <xsl:text> /&gt;</xsl:text>
+</xsl:template>
+
+<xsl:template match="@*" priority="-1">
+       <xsl:if test="namespace-uri(.) = '' and local-name(.) != 'lang' or namespace-uri(.) = 'http://www.w3.org/XML/1998/namespace'">
+               <xsl:if test="namespace-uri(..) = 'http://www.w3.org/1999/xhtml' or local-name(.) != 'class'">
+                       <xsl:text> </xsl:text>
+                       <xsl:if test="namespace-uri(.) = 'http://www.w3.org/XML/1998/namespace'">
+                               <xsl:text>xml:</xsl:text>
+                       </xsl:if>
+                       <xsl:value-of select="local-name(.)"/>
+                       <xsl:text>=&quot;</xsl:text>
+                       <xsl:variable name="escaped">
+                               <xsl:call-template name="escape-lua-xhtml1">
+                                       <xsl:with-param name="string" select="."/>
+                               </xsl:call-template>
+                       </xsl:variable>
+                       <xsl:value-of select="$escaped"/>
+                       <xsl:text>&quot;</xsl:text>
+               </xsl:if>
+       </xsl:if>
+</xsl:template>
+
+<xsl:template match="@xml:lang">
+       <xsl:variable name="escaped">
+               <xsl:call-template name="escape-lua-xhtml1">
+                       <xsl:with-param name="string" select="."/>
+               </xsl:call-template>
+       </xsl:variable>
+       <xsl:text> xml:lang=&quot;</xsl:text>
+       <xsl:value-of select="$escaped"/>
+       <xsl:text>&quot;</xsl:text>
+       <xsl:text> lang=&quot;</xsl:text>
+       <xsl:value-of select="$escaped"/>
+       <xsl:text>&quot;</xsl:text>
+</xsl:template>
+
+<xsl:template match="text()" priority="-1">
+       <xsl:variable name="escaped">
+               <xsl:call-template name="escape-lua-xhtml1">
+                       <xsl:with-param name="string" select="."/>
+               </xsl:call-template>
+       </xsl:variable>
+       <xsl:value-of select="$escaped"/>
+</xsl:template>
+
+<xsl:template name="escape-xhtml1">
+       <xsl:param name="string"/>
+       <xsl:variable name="escaped">
+               <xsl:call-template name="replace">
+                       <xsl:with-param name="string" select="$string"/>
+                       <xsl:with-param name="search" select="'&amp;'"/>
+                       <xsl:with-param name="replace" select="'&amp;amp;'"/>
+               </xsl:call-template>
+       </xsl:variable>
+       <xsl:variable name="escaped1">
+               <xsl:call-template name="replace">
+                       <xsl:with-param name="string" select="$escaped"/>
+                       <xsl:with-param name="search" select="'&quot;'"/>
+                       <xsl:with-param name="replace" select="'&amp;quot;'"/>
+               </xsl:call-template>
+       </xsl:variable>
+       <xsl:variable name="escaped2">
+               <xsl:call-template name="replace">
+                       <xsl:with-param name="string" select="$escaped1"/>
+                       <xsl:with-param name="search" select='"&#39;"'/>
+                       <xsl:with-param name="replace" select="'&amp;#39;'"/>
+               </xsl:call-template>
+       </xsl:variable>
+       <xsl:variable name="escaped3">
+               <xsl:call-template name="replace">
+                       <xsl:with-param name="string" select="$escaped2"/>
+                       <xsl:with-param name="search" select="'&lt;'"/>
+                       <xsl:with-param name="replace" select="'&amp;lt;'"/>
+               </xsl:call-template>
+       </xsl:variable>
+       <xsl:call-template name="replace">
+               <xsl:with-param name="string" select="$escaped3"/>
+               <xsl:with-param name="search" select="'&gt;'"/>
+               <xsl:with-param name="replace" select="'&amp;gt;'"/>
+       </xsl:call-template>
+</xsl:template>
+
+<xsl:template name="escape-lua-xhtml1">
+       <xsl:param name="string"/>
+       <xsl:variable name="escaped">
+               <xsl:call-template name="escape-xhtml1">
+                       <xsl:with-param name="string" select="$string"/>
+               </xsl:call-template>
+       </xsl:variable>
+       <xsl:variable name="escaped1">
+               <xsl:call-template name="replace">
+                       <xsl:with-param name="string" select="$escaped"/>
+                       <xsl:with-param name="search" select="'='"/>
+                       <xsl:with-param name="replace" select="'&amp;#61;'"/>
+               </xsl:call-template>
+       </xsl:variable>
+       <xsl:variable name="escaped2">
+               <xsl:call-template name="replace">
+                       <xsl:with-param name="string" select="$escaped1"/>
+                       <xsl:with-param name="search" select="'['"/>
+                       <xsl:with-param name="replace" select="'&amp;#91;'"/>
+               </xsl:call-template>
+       </xsl:variable>
+       <xsl:call-template name="replace">
+               <xsl:with-param name="string" select="$escaped2"/>
+               <xsl:with-param name="search" select="']'"/>
+               <xsl:with-param name="replace" select="'&amp;#93;'"/>
+       </xsl:call-template>
+</xsl:template>
+
+<xsl:template name="replace">
+       <xsl:param name="string"/>
+       <xsl:param name="search"/>
+       <xsl:param name="replace"/>
+       <xsl:choose>
+               <xsl:when test="contains($string, $search)">
+                       <xsl:value-of select="substring-before($string, $search)"/>
+                       <xsl:value-of select="$replace"/>
+                       <xsl:call-template name="replace">
+                               <xsl:with-param name="string" select="substring-after($string, $search)"/>
+                               <xsl:with-param name="search" select="$search"/>
+                               <xsl:with-param name="replace" select="$replace"/>
+                       </xsl:call-template>
+               </xsl:when>
+               <xsl:otherwise>
+                       <xsl:value-of select="$string"/>
+               </xsl:otherwise>
+       </xsl:choose>
+</xsl:template>
+
+</xsl:transform>
index 20fdb43767bde0044910fa1eab87434330b7b2ed..3a947546b0a6eca7f79708f0916432f406aac832 100644 (file)
@@ -1,16 +1,24 @@
+MAKEPATH:=$(dir $(lastword $(MAKEFILE_LIST)))
+-include $(MAKEPATH)config.mk
+-include $(MAKEPATH)gccconfig.mk
+
 .PHONY: all build compile luacompile luasource clean luaclean
 
 all: build
 
 build: luabuild gccbuild
 
-luabuild: lua$(LUA_TARGET)
+luabuild: lua$(LUA_TARGET) i18n
 
 gccbuild: compile
 compile:
 
 clean: luaclean
 
+i18n: luasource
+       [ -n "$(XSLTPROC)" ] && for i in dist$(LUCI_MODULEDIR)/i18n/*.xml; do [ -f "$$i" ]\
+       && { $(XSLTPROC) $(MAKEPATH)i18n-lua-xhtml1.xsl $$i > $${i/.xml/.lua}; rm $$i; }; done || true
+
 luasource:
        mkdir -p dist$(LUA_MODULEDIR)
        mkdir -p dist$(LUCI_MODULEDIR)
@@ -19,7 +27,8 @@ luasource:
        cp -a luasrc/* dist$(LUCI_MODULEDIR) -R 2>/dev/null || true
        cp -a lua/* dist$(LUA_MODULEDIR) -R 2>/dev/null || true
        cp -a htdocs/* dist$(HTDOCS) -R 2>/dev/null || true
-       for i in $$(find dist -name .svn); do rm $$i -rf; done
+       for i in $$(find dist -name .svn); do rm $$i -rf || true; done
+
 
 luastrip: luasource
        for i in $$(find dist -type f -name '*.lua'); do perl -e 'undef $$/; open( F, "< $$ARGV[0]" ) || die $$!; $$src = <F>; close F; $$src =~ s/--\[\[.*?\]\](--)?//gs; $$src =~ s/^\s*--.*?\n//gm; open( F, "> $$ARGV[0]" ) || die $$!; print F $$src; close F' $$i; done