Add xsltproc.py

* Python frontend to libxslt implementing csc extensions
* All xsl files are now XSLT 1.0
* Faster build times (down from 20s to 8s)
This commit is contained in:
David Bartley 2007-11-10 00:55:15 -05:00
parent 9d2622ba28
commit 41c621d1ee
13 changed files with 119 additions and 84 deletions

View 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 $< > $@

82
scripts/xsltproc.py Executable file
View File

@ -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)

View 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" />

View 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>

View 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">

View 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">
@ -111,7 +109,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'), ' ', '_')}">
@ -128,7 +126,7 @@
<xsl:call-template name="footer" />
</div></body>
</html>
</xsl:result-document>
</xsl:document>
</xsl:if>
</xsl:for-each>
</xsl:template>

View 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>

View 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

View 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" />

View 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')}">
@ -118,7 +116,7 @@
<!-- 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">
@ -150,7 +148,7 @@
<xsl:call-template name="footer" />
</div></body>
</html>
</xsl:result-document>
</xsl:document>
</xsl:template>
</xsl:stylesheet>

View 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

View 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">

View 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>