Some text
- Navigation- |
-
-
- |
-
+
+
+
+
+
+ Link
+
+
Modified: z3/xdv/trunk/lib/xdv/tests/tests.py
==============================================================================
--- z3/xdv/trunk/lib/xdv/tests/tests.py (original)
+++ z3/xdv/trunk/lib/xdv/tests/tests.py Sun Feb 7 13:12:48 2010
@@ -32,7 +32,14 @@
xpathsfn = os.path.join(self.testdir, "xpaths.txt")
xslfn = os.path.join(self.testdir, "compiled.xsl")
outputfn = os.path.join(self.testdir, "output.html")
-
+
+ if (not os.path.exists(themefn) or
+ not os.path.exists(contentfn) or
+ not os.path.exists(rulesfn) or
+ not os.path.exists(outputfn)
+ ):
+ return
+
contentdoc = etree.parse(source=contentfn, base_url=contentfn,
parser=etree.HTMLParser())
@@ -43,7 +50,7 @@
# Serialize / parse the theme - this can catch problems with escaping.
cts = etree.tostring(ct)
parser = etree.XMLParser()
- ct2 = etree.fromstring(cts, parser=parser)
+ etree.fromstring(cts, parser=parser)
# Compare to previous version
if os.path.exists(xslfn):
@@ -99,7 +106,102 @@
# Write the compiled xsl out to catch unexpected changes
if self.writefiles:
open(outputfn, 'w').write(self.themed_string)
-
+
+class TestAbsolutePrefix(unittest.TestCase):
+
+ def testEnabled(self):
+ testdir = os.path.join(HERE, 'absolute')
+
+ themefn = os.path.join(testdir, "theme.html")
+ rulesfn = os.path.join(testdir, "rules.xml")
+
+ compiled = xdv.compiler.compile_theme(rules=rulesfn, theme=themefn, absolute_prefix="/abs")
+
+ styleTag = compiled.xpath('//style')[0]
+ styleLines = [x.strip() for x in styleTag.getchildren()[0].text.split('\n') if x.strip()]
+
+ self.assertEquals([
+ '@import url("/abs/foo.css");',
+ '@import url("/abs/./foo.css");',
+ "@import url('../foo.css');",
+ "@import url('/foo.css');",
+ "@import url('http://site.com/foo.css');"
+ ], styleLines)
+
+ linkTags = compiled.xpath('//link')
+ self.assertEquals([
+ '/abs/foo.css',
+ '/abs/foo.css',
+ '/abs/../foo.css',
+ '/foo.css',
+ 'http://site.com/foo.css'
+ ], [x.get('href') for x in linkTags])
+
+ scriptTags = compiled.xpath('//script')
+ self.assertEquals([
+ '/abs/foo.js',
+ '/abs/foo.js',
+ '/abs/../foo.js',
+ '/foo.js',
+ 'http://site.com/foo.js'
+ ], [x.get('src') for x in scriptTags])
+
+ imgTags = compiled.xpath('//img')
+ self.assertEquals([
+ '/abs/foo.jpg',
+ '/abs/foo.jpg',
+ '/abs/../foo.jpg',
+ '/foo.jpg',
+ 'http://site.com/foo.jpg'
+ ], [x.get('src') for x in imgTags])
+
+ def testDisabled(self):
+ testdir = os.path.join(HERE, 'absolute')
+
+ themefn = os.path.join(testdir, "theme.html")
+ rulesfn = os.path.join(testdir, "rules.xml")
+
+ compiled = xdv.compiler.compile_theme(rules=rulesfn, theme=themefn)
+
+ styleTag = compiled.xpath('//style')[0]
+ styleLines = [x.strip() for x in styleTag.getchildren()[0].text.split('\n') if x.strip()]
+
+ self.assertEquals([
+ "@import url('foo.css');",
+ "@import url('./foo.css');",
+ "@import url('../foo.css');",
+ "@import url('/foo.css');",
+ "@import url('http://site.com/foo.css');"
+ ], styleLines)
+
+ linkTags = compiled.xpath('//link')
+ self.assertEquals([
+ 'foo.css',
+ './foo.css',
+ '../foo.css',
+ '/foo.css',
+ 'http://site.com/foo.css'
+ ], [x.get('href') for x in linkTags])
+
+ scriptTags = compiled.xpath('//script')
+ self.assertEquals([
+ 'foo.js',
+ './foo.js',
+ '../foo.js',
+ '/foo.js',
+ 'http://site.com/foo.js'
+ ], [x.get('src') for x in scriptTags])
+
+ imgTags = compiled.xpath('//img')
+ self.assertEquals([
+ 'foo.jpg',
+ './foo.jpg',
+ '../foo.jpg',
+ '/foo.jpg',
+ 'http://site.com/foo.jpg'
+ ], [x.get('src') for x in imgTags])
+
+
def main(*args, **kwargs):
try:
test_num = sys.argv[1]
@@ -138,9 +240,9 @@
continue
cls = type('Test%s'%name, (XDVTestCase,), dict(testdir=path))
suite.addTest(unittest.makeSuite(cls))
+ suite.addTest(unittest.makeSuite(TestAbsolutePrefix))
return suite
-
if __name__ == "__main__":
debug = '--debug' in sys.argv
if debug:
From ldr at codespeak.net Sun Feb 7 22:39:16 2010
From: ldr at codespeak.net (ldr at codespeak.net)
Date: Sun, 7 Feb 2010 22:39:16 +0100 (CET)
Subject: [z3-checkins] r71153 - in z3/xdv/trunk: . lib/xdv
Message-ID: <20100207213916.E8C25282BDC@codespeak.net>
Author: ldr
Date: Sun Feb 7 22:39:12 2010
New Revision: 71153
Modified:
z3/xdv/trunk/lib/xdv/compiler.py
z3/xdv/trunk/nginx.cfg
Log:
you pass in a compiler instance, not a class
Modified: z3/xdv/trunk/lib/xdv/compiler.py
==============================================================================
--- z3/xdv/trunk/lib/xdv/compiler.py (original)
+++ z3/xdv/trunk/lib/xdv/compiler.py Sun Feb 7 22:39:12 2010
@@ -91,8 +91,8 @@
* ``trace`` can be set to True to enable compiler trace information
* ``includemode`` can be set to 'document' or 'ssi' to change the way in
which includes are processed
- * ``parser`` can be set to an lxml parser class; the default is HTMLParser
- * ``compiler_parser``` can be set to an lxml parser class; the default is
+ * ``parser`` can be set to an lxml parser instance; the default is an HTMLParser
+ * ``compiler_parser``` can be set to an lxml parser instance; the default is a
XMLParser
"""
rules_doc = etree.parse(rules)
Modified: z3/xdv/trunk/nginx.cfg
==============================================================================
--- z3/xdv/trunk/nginx.cfg (original)
+++ z3/xdv/trunk/nginx.cfg Sun Feb 7 22:39:12 2010
@@ -20,7 +20,7 @@
--with-http_xslt_module
--with-libxml2=${buildout:directory}/parts/lxml/libxml2
--with-libxslt=${buildout:directory}/parts/lxml/libxslt
- --with-debug --with-cc-opt="-O0" # helps debugging with gdb
+# --with-debug --with-cc-opt="-O0" # helps debugging with gdb
[nginx-conf]
recipe = collective.recipe.template
From ldr at codespeak.net Mon Feb 8 15:24:49 2010
From: ldr at codespeak.net (ldr at codespeak.net)
Date: Mon, 8 Feb 2010 15:24:49 +0100 (CET)
Subject: [z3-checkins] r71166 - in z3/xdv/trunk/lib/xdv: . tests
tests/absolute
Message-ID: <20100208142449.C5F7F282BD8@codespeak.net>
Author: ldr
Date: Mon Feb 8 15:24:46 2010
New Revision: 71166
Added:
z3/xdv/trunk/lib/xdv/tests/absolute_rules.xml
- copied unchanged from r71165, z3/xdv/trunk/lib/xdv/tests/absolute/rules.xml
z3/xdv/trunk/lib/xdv/tests/absolute_theme.html
- copied, changed from r71165, z3/xdv/trunk/lib/xdv/tests/absolute/theme.html
Removed:
z3/xdv/trunk/lib/xdv/tests/absolute/
Modified:
z3/xdv/trunk/lib/xdv/compiler.py
z3/xdv/trunk/lib/xdv/run.py
z3/xdv/trunk/lib/xdv/tests/tests.py
Log:
improve the absolute path machinary
Modified: z3/xdv/trunk/lib/xdv/compiler.py
==============================================================================
--- z3/xdv/trunk/lib/xdv/compiler.py (original)
+++ z3/xdv/trunk/lib/xdv/compiler.py Mon Feb 8 15:24:46 2010
@@ -25,7 +25,7 @@
HERE = os.path.dirname(__file__)
-IMPORT_STYLESHEET_PATTERN = '@import url\\([\'"](.+)[\'"]\\);'
+IMPORT_STYLESHEET = re.compile(r'''(@import\s+(?:url\(['"]?|['"]))(.+)(['"]?\)|['"])''', re.IGNORECASE)
COMPILER_PATH = os.path.join(HERE, 'compiler.xsl')
@@ -57,14 +57,15 @@
"""
if src.startswith('/') or '://' in src:
return src
-
if src.startswith('./'):
return "%s/%s" % (prefix, src[2:])
else:
return "%s/%s" % (prefix, src)
def apply_absolute_prefix(theme_doc, absolute_prefix):
- for node in theme_doc.xpath('*//style | *//script | *//img | *//link'):
+ if absolute_prefix.endswith('/'):
+ absolute_prefix = absolute_prefix[:-1]
+ for node in theme_doc.xpath('*//style | *//script | *//img | *//link | *//comment()'):
if node.tag == 'img' or node.tag == 'script':
src = node.get('src')
if src:
@@ -73,8 +74,8 @@
href = node.get('href')
if href:
node.set('href', to_absolute(href, absolute_prefix))
- elif node.tag == 'style':
- node.text = re.sub(IMPORT_STYLESHEET_PATTERN, r'@import url("%s/\1");' % absolute_prefix, node.text, re.I)
+ elif node.tag == 'style' or node.tag == etree.Comment and node.text.startswith("[if IE"):
+ node.text = IMPORT_STYLESHEET.sub(lambda match: match.group(1) + to_absolute(match.group(2), absolute_prefix) + match.group(3), node.text)
def compile_theme(rules, theme, extra=None, css=True, xinclude=False, absolute_prefix=None, update=True, trace=False, includemode=None, parser=None, compiler_parser=None):
"""Invoke the xdv compiler.
@@ -95,7 +96,8 @@
* ``compiler_parser``` can be set to an lxml parser instance; the default is a
XMLParser
"""
- rules_doc = etree.parse(rules)
+ rules_parser = etree.XMLParser(recover=False)
+ rules_doc = etree.parse(rules, parser=rules_parser)
if xinclude:
rules_doc.xinclude()
if update:
@@ -145,13 +147,13 @@
"""
parser = OptionParser(usage=usage)
parser.add_option("-e", "--extra", metavar="extra.xsl",
- help="XDV extraurl XSLT file",
+ help="Extra XSL to be included in the transform",
dest="extra", default=None)
parser.add_option("-o", "--output", metavar="output.xsl",
help="Output filename (instead of stdout)",
dest="output", default=sys.stdout)
parser.add_option("-p", "--pretty-print", action="store_true",
- help="Pretty print output (can alter rendering on the browser)",
+ help="Pretty print output (may alter rendering in browser)",
dest="pretty_print", default=False)
parser.add_option("--trace", action="store_true",
help="Compiler trace logging",
@@ -159,6 +161,9 @@
parser.add_option("--xinclude", action="store_true",
help="Run XInclude on rules.xml",
dest="xinclude", default=False)
+ parser.add_option("-a", "--absolute-prefix", metavar="/",
+ help="relative urls in the theme file will be made into absolute links with this prefix.",
+ dest="absolute_prefix", default=None)
parser.add_option("-i", "--includemode", metavar="INC",
help="include mode (document or ssi)",
dest="includemode", default=None)
@@ -171,7 +176,7 @@
if options.trace:
logger.setLevel(logging.DEBUG)
- output_xslt = compile_theme(rules=rules, theme=theme, extra=options.extra, trace=options.trace, xinclude=options.xinclude, includemode=options.includemode)
+ output_xslt = compile_theme(rules=rules, theme=theme, extra=options.extra, trace=options.trace, xinclude=options.xinclude, absolute_prefix=options.absolute_prefix, includemode=options.includemode)
output_xslt.write(options.output, encoding='utf-8', pretty_print=options.pretty_print)
if __name__ == '__main__':
Modified: z3/xdv/trunk/lib/xdv/run.py
==============================================================================
--- z3/xdv/trunk/lib/xdv/run.py (original)
+++ z3/xdv/trunk/lib/xdv/run.py Mon Feb 8 15:24:46 2010
@@ -69,6 +69,7 @@
parser.resolvers.add(RunResolver(os.path.dirname(content)))
transform = etree.XSLT(output_xslt)
content_doc = etree.parse(content, parser=etree.HTMLParser())
+ import pdb; pdb.set_trace()
output_html = transform(content_doc)
output_html.write(options.output, encoding='utf-8', pretty_print=options.pretty_print)
for msg in transform.error_log:
Copied: z3/xdv/trunk/lib/xdv/tests/absolute_theme.html (from r71165, z3/xdv/trunk/lib/xdv/tests/absolute/theme.html)
==============================================================================
--- z3/xdv/trunk/lib/xdv/tests/absolute/theme.html (original)
+++ z3/xdv/trunk/lib/xdv/tests/absolute_theme.html Mon Feb 8 15:24:46 2010
@@ -4,7 +4,8 @@
Some text