* Python frontend to libxslt implementing csc extensions * All xsl files are now XSLT 1.0 * Faster build times (down from 20s to 8s)foo
parent
9d2622ba28
commit
41c621d1ee
@ -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) |
Loading…
Reference in new issue