Add xsltproc.py
authorDavid Bartley <dtbartle@csclub.uwaterloo.ca>
Sat, 10 Nov 2007 05:55:15 +0000 (00:55 -0500)
committerDavid Bartley <dtbartle@csclub.uwaterloo.ca>
Sat, 10 Nov 2007 08:08:39 +0000 (03:08 -0500)
* Python frontend to libxslt implementing csc extensions
* All xsl files are now XSLT 1.0
* Faster build times (down from 20s to 8s)

13 files changed:
common.mk.in
scripts/xsltproc.py [new file with mode: 0755]
xsl/atom.xsl
xsl/common.xsl
xsl/directory.xsl
xsl/events.xsl
xsl/exec.xsl
xsl/functions.xsl
xsl/html.xsl
xsl/media.xsl
xsl/members.xsl
xsl/menu.xsl
xsl/news.xsl

index b918b39..61c39fe 100644 (file)
@@ -23,15 +23,15 @@ else
        rm -rf $(OUTDIR)
 endif
 
-XSLTARGS=g_date=`date -I` g_time=`date +%H:%M:%S` g_outdir="$(OUTDIR)" \
-       g_datetime="`date --rfc-3339=seconds`" g_pwd="$$(pwd)" g_root="$(ROOT)"
-SAXON = /users/www/saxon9/saxon9.bin
+XSLTARGS=g_date="`date -I`" g_time="`date +%H:%M:%S`" g_outdir="$(OUTDIR)" \
+       g_datetime="`date --rfc-3339=seconds`" g_pwd="`pwd`" g_root="$(ROOT)"
+XSLTPROC = $(ROOT)/scripts/xsltproc.py
 
 $(OUTDIR)%.html: %.xml $(ROOT)/csc.dtd $(ROOT)/xsl/*.xsl directory.xml $(ROOT)/menu.xml
-       $(SAXON) -o $@ $< $(ROOT)/xsl/html.xsl $(XSLTARGS)
+       $(XSLTPROC) $< $(ROOT)/xsl/html.xsl $@ $(XSLTARGS)
 
 $(OUTDIR)%.atom: %.xml xsl/atom.xsl
-       $(SAXON) -o $@  $< $(ROOT)/xsl/atom.xsl $(XSLTARGS)
+       $(XSLTPROC) $< $(ROOT)/xsl/atom.xsl $@ $(XSLTARGS)
 
 $(OUTDIR)%.ics: %.xml
        perl scripts/xml2ics.pl $< > $@
diff --git a/scripts/xsltproc.py b/scripts/xsltproc.py
new file mode 100755 (executable)
index 0000000..3619a8a
--- /dev/null
@@ -0,0 +1,82 @@
+#!/usr/bin/python2.4
+import os, sys, re, urllib, libxml2, libxslt
+
+#
+# globals
+#
+cscUri = "http://csclub.uwaterloo.ca/xsltproc"
+
+#
+# csc:encode-for-uri
+#
+def cscEncodeForUri(ctx, str):
+    if type(str) == type([]):
+        str = libxml2.xmlNode(str[0]).getContent()
+    print urllib.quote(str)
+    return urllib.quote(str)
+
+#
+# csc:term
+#
+def cscTerm(ctx, str):
+    if type(str) == type([]):
+        str = libxml2.xmlNode(str[0]).getContent()
+
+    try:
+        # YYYY-MM-DD
+        (year, month, day) = re.findall("^([0-9]+)-([0-9]+)-([0-9]+)$", str)[0]
+        month = int(month)
+        if month >= 1 and month <= 4:
+            return "Winter " + year
+        elif month >= 5 and month <= 8:
+            return "Spring " + year
+        elif month >= 9 and month <= 12:
+            return "Fall " + year
+        else:
+            print "Invalid month '" + month + "'"
+    except:
+        print "Invalid term '" + str + "'"
+
+#
+# csc:email
+#
+def cscEmail(ctx, str):
+    return "_EMAIL_TODO_"
+
+#
+# main
+#
+
+# check argv
+if len(sys.argv) < 4:
+    print "Usage: xsltproc.py input-file style-sheet output-file [params...]"
+    sys.exit(1)
+inFile = sys.argv[1]
+xsltFile = sys.argv[2]
+outFile = sys.argv[3]
+rawParams = sys.argv[4:]
+
+# check params
+params = {}
+for p in rawParams:
+    p = p.split("=")
+    if len(p) == 1:
+        print "Missing value for parameter " + p[0]
+        sys.exit(1)
+    params[p[0]] = "'" + p[1] + "'"
+
+try:
+    # register extensions
+    libxslt.registerExtModuleFunction("encode-for-uri", cscUri, cscEncodeForUri)
+    libxslt.registerExtModuleFunction("term", cscUri, cscTerm)
+    libxslt.registerExtModuleFunction("email", cscUri, cscEmail)
+
+    # parse xml/xslt and apply style-sheet
+    style = libxslt.parseStylesheetFile(xsltFile)
+    doc = libxml2.parseFile(inFile)
+    res = style.applyStylesheet(doc, params)
+    style.saveResultToFilename(outFile, res, 0)
+
+except:
+    print "Unexpected error:", sys.exc_info()[0]
+    sys.exit(1)
index d1c0f6b..289e1a7 100644 (file)
@@ -1,11 +1,9 @@
 <?xml version='1.0'?>
 
-<xsl:stylesheet version="2.0"
+<xsl:stylesheet version="1.0"
     xmlns="http://www.w3.org/1999/xhtml"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
-    xmlns:xs="http://www.w3.org/2001/XMLSchema"
-    xmlns:fn="http://www.w3.org/2005/xpath-functions"
-    xmlns:csc="http://csclub.uwaterloo.ca/xslt">
+    xmlns:csc="http://csclub.uwaterloo.ca/xsltproc">
 
 <xsl:param name="g_datetime" required="yes" />
 
index 0a2c406..24b4780 100644 (file)
@@ -1,11 +1,9 @@
 <?xml version='1.0'?>
 
-<xsl:stylesheet version="2.0"
+<xsl:stylesheet version="1.0"
     xmlns="http://www.w3.org/1999/xhtml"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
-    xmlns:xs="http://www.w3.org/2001/XMLSchema"
-    xmlns:fn="http://www.w3.org/2005/xpath-functions"
-    xmlns:csc="http://csclub.uwaterloo.ca/xslt">
+    xmlns:csc="http://csclub.uwaterloo.ca/xsltproc">
 
 <xsl:template match="cscpage">
   <html>
index 2a791f7..585994e 100644 (file)
@@ -1,11 +1,9 @@
 <?xml version='1.0'?>
 
-<xsl:stylesheet version="2.0"
+<xsl:stylesheet version="1.0"
     xmlns="http://www.w3.org/1999/xhtml"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
-    xmlns:xs="http://www.w3.org/2001/XMLSchema"
-    xmlns:fn="http://www.w3.org/2005/xpath-functions"
-    xmlns:csc="http://csclub.uwaterloo.ca/xslt">
+    xmlns:csc="http://csclub.uwaterloo.ca/xsltproc">
 
 <xsl:template match="directory">
   <xsl:variable name="href">
index 5b5c5af..3619473 100644 (file)
@@ -1,11 +1,9 @@
 <?xml version='1.0'?>
 
-<xsl:stylesheet version="2.0"
+<xsl:stylesheet version="1.0"
     xmlns="http://www.w3.org/1999/xhtml"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
-    xmlns:xs="http://www.w3.org/2001/XMLSchema"
-    xmlns:fn="http://www.w3.org/2005/xpath-functions"
-    xmlns:csc="http://csclub.uwaterloo.ca/xslt">
+    xmlns:csc="http://csclub.uwaterloo.ca/xsltproc">
 
 <xsl:template name="events">
   <tr>
@@ -71,7 +69,7 @@
 </xsl:template>
 
 <xsl:template match="eventitem">
-  <xsl:result-document method="xml" encoding="ISO-8859-1"
+  <xsl:document method="xml" encoding="ISO-8859-1"
       doctype-public="-//W3C//DTD XHTML 1.1//EN"
       doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
       href="{translate(concat($g_outdir, @room, '-', @date, '-', @time, '.html'), ' ', '_')}">
@@ -96,7 +94,7 @@
         <xsl:call-template name="footer" />
       </div></body>
     </html>
-  </xsl:result-document>
+  </xsl:document>
 </xsl:template>
 
 <xsl:template match="generate-event-files">
       <p><a href="{translate(concat('old-',csc:term(@date), '.html'), ' ', '_')}">
         <xsl:value-of select="csc:term(@date)" />
       </a></p>
-      <xsl:result-document method="xml" encoding="ISO-8859-1"
+      <xsl:document method="xml" encoding="ISO-8859-1"
             doctype-public="-//W3C//DTD XHTML 1.1//EN"
             doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
             href="{translate(concat($g_outdir, 'old-', csc:term(@date), '.html'), ' ', '_')}">
             <xsl:call-template name="footer" />
           </div></body>
         </html>
-      </xsl:result-document>
+      </xsl:document>
     </xsl:if>
   </xsl:for-each>
 </xsl:template>
index 8357b32..810cf0e 100644 (file)
@@ -1,11 +1,9 @@
 <?xml version='1.0'?>
 
-<xsl:stylesheet version="2.0"
+<xsl:stylesheet version="1.0"
     xmlns="http://www.w3.org/1999/xhtml"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
-    xmlns:xs="http://www.w3.org/2001/XMLSchema"
-    xmlns:fn="http://www.w3.org/2005/xpath-functions"
-    xmlns:csc="http://csclub.uwaterloo.ca/xslt">
+    xmlns:csc="http://csclub.uwaterloo.ca/xsltproc">
 
 <xsl:template match="execlist">
   <h3>Elected positions</h3>
index 22595ef..b9558a6 100644 (file)
@@ -1,37 +1,9 @@
 <?xml version='1.0'?>
 
-<xsl:stylesheet version="2.0"
+<xsl:stylesheet version="1.0"
     xmlns="http://www.w3.org/1999/xhtml"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
-    xmlns:xs="http://www.w3.org/2001/XMLSchema"
-    xmlns:fn="http://www.w3.org/2005/xpath-functions"
-    xmlns:csc="http://csclub.uwaterloo.ca/xslt">
-
-<xsl:function name="csc:term" as="xs:string">
-  <xsl:param name="date" />
-  <xsl:variable name="date_year" as="xs:integer">
-    <xsl:analyze-string select="$date" regex="([0-9]+)-[0-9]+-[0-9]+">
-      <xsl:matching-substring>
-        <xsl:value-of select="regex-group(1)" />
-      </xsl:matching-substring>
-    </xsl:analyze-string>
-  </xsl:variable>
-  <xsl:variable name="date_month" as="xs:integer">
-    <xsl:analyze-string select="$date" regex="[0-9]+-([0-9]+)-[0-9]+">
-      <xsl:matching-substring>
-        <xsl:value-of select="regex-group(1)" />
-      </xsl:matching-substring>
-    </xsl:analyze-string>
-  </xsl:variable>
-  <xsl:variable name="term_text">
-    <xsl:choose>
-      <xsl:when test="$date_month ge 1 and $date_month le 4">Winter </xsl:when>
-      <xsl:when test="$date_month ge 5 and $date_month le 8">Spring </xsl:when>
-      <xsl:when test="$date_month ge 9 and $date_month le 12">Fall </xsl:when>
-    </xsl:choose>
-  </xsl:variable>
-  <xsl:value-of select="concat($term_text, $date_year)" />
-</xsl:function>
+    xmlns:csc="http://csclub.uwaterloo.ca/xsltproc">
 
 <xsl:template name="donate-now">
 <p>Please consider <a href="{$g_pre}about/donations.html">donating to the
index 31baecc..bd0a932 100644 (file)
@@ -1,11 +1,9 @@
 <?xml version='1.0'?>
 
-<xsl:stylesheet version="2.0"
+<xsl:stylesheet version="1.0"
     xmlns="http://www.w3.org/1999/xhtml"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
-    xmlns:xs="http://www.w3.org/2001/XMLSchema"
-    xmlns:fn="http://www.w3.org/2005/xpath-functions"
-    xmlns:csc="http://csclub.uwaterloo.ca/xslt">
+    xmlns:csc="http://csclub.uwaterloo.ca/xsltproc">
 
 <xsl:param name="g_date" required="yes" />
 <xsl:param name="g_time" required="yes" />
index ce1f403..9fbac93 100644 (file)
@@ -1,11 +1,9 @@
 <?xml version='1.0'?>
 
-<xsl:stylesheet version="2.0"
+<xsl:stylesheet version="1.0"
     xmlns="http://www.w3.org/1999/xhtml"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
-    xmlns:xs="http://www.w3.org/2001/XMLSchema"
-    xmlns:fn="http://www.w3.org/2005/xpath-functions"
-    xmlns:csc="http://csclub.uwaterloo.ca/xslt">
+    xmlns:csc="http://csclub.uwaterloo.ca/xsltproc">
 
 <xsl:template match="mediafile">
   <xsl:param name="ext" />
@@ -54,8 +52,8 @@
   <xsl:param name="mirror" />
   <xsl:param name="bittorrent" />
   <xsl:param name="ads" />
-  <xsl:variable name="realurl" select="concat('http://csclub.uwaterloo.ca/media/', fn:encode-for-uri(@title), '.html')" />
-  <xsl:result-document method="xml" encoding="ISO-8859-1"
+  <xsl:variable name="realurl" select="concat('http://csclub.uwaterloo.ca/media/', csc:encode-for-uri(@title), '.html')" />
+  <xsl:document method="xml" encoding="ISO-8859-1"
       doctype-public="-//W3C//DTD XHTML 1.1//EN"
       doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
       href="{concat($g_outdir, @title, $suffix, '.html')}">
             <!-- reddit button -->
             <script type="text/javascript">
               reddit_url = '<xsl:value-of select="$realurl" />';
-              reddit_title = '<xsl:value-of select="fn:encode-for-uri(@title)" />';
+              reddit_title = '<xsl:value-of select="csc:encode-for-uri(@title)" />';
             </script>
             <script src="http://reddit.com/button.js?t=3"
                     type="text/javascript">
         <xsl:call-template name="footer" />
       </div></body>
     </html>
-  </xsl:result-document>
+  </xsl:document>
 </xsl:template>
 
 </xsl:stylesheet>
index 0219d25..1aaebea 100644 (file)
@@ -1,11 +1,9 @@
 <?xml version='1.0'?>
 
-<xsl:stylesheet version="2.0"
+<xsl:stylesheet version="1.0"
     xmlns="http://www.w3.org/1999/xhtml"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
-    xmlns:xs="http://www.w3.org/2001/XMLSchema"
-    xmlns:fn="http://www.w3.org/2005/xpath-functions"
-    xmlns:csc="http://csclub.uwaterloo.ca/xslt">
+    xmlns:csc="http://csclub.uwaterloo.ca/xsltproc">
 
 <xsl:template match="members-this-term">
   <p>The members for <xsl:value-of select="csc:term($g_date)" /> are
index d00421a..7d68d29 100644 (file)
@@ -1,11 +1,9 @@
 <?xml version='1.0'?>
 
-<xsl:stylesheet version="2.0"
+<xsl:stylesheet version="1.0"
     xmlns="http://www.w3.org/1999/xhtml"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
-    xmlns:xs="http://www.w3.org/2001/XMLSchema"
-    xmlns:fn="http://www.w3.org/2005/xpath-functions"
-    xmlns:csc="http://csclub.uwaterloo.ca/xslt">
+    xmlns:csc="http://csclub.uwaterloo.ca/xsltproc">
 
 <xsl:template match="menuitem">
   <xsl:variable name="preabs">
index 6882b63..4b1b26e 100644 (file)
@@ -1,10 +1,9 @@
 <?xml version='1.0'?>
 
-<xsl:stylesheet version="2.0"
+<xsl:stylesheet version="1.0"
     xmlns="http://www.w3.org/1999/xhtml"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
-    xmlns:xs="http://www.w3.org/2001/XMLSchema"
-    xmlns:csc="http://csclub.uwaterloo.ca/xslt">
+    xmlns:csc="http://csclub.uwaterloo.ca/xsltproc">
 
 <xsl:template match="newsdefs">
   <xsl:apply-templates select="newsitem[csc:term(@date) = csc:term($g_date)]">
@@ -65,7 +64,7 @@
       <p><a href="{translate(concat('old-', csc:term(@date), '.html'), ' ', '_')}">
         <xsl:value-of select="csc:term(@date)" />
       </a></p>
-      <xsl:result-document method="xml" encoding="ISO-8859-1"
+      <xsl:document method="xml" encoding="ISO-8859-1"
           doctype-public="-//W3C//DTD XHTML 1.1//EN"
           doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
           href="{translate(concat($g_outdir, 'old-', csc:term(@date), '.html'), ' ', '_')}">
@@ -82,7 +81,7 @@
             <xsl:call-template name="footer" />
           </div></body>
         </html>
-      </xsl:result-document>
+      </xsl:document>
     </xsl:if>
   </xsl:for-each>
 </xsl:template>