[Checkins] SVN: Sandbox/malthe/chameleon.html/ glob style resource matching.
Stefan Eletzhofer
stefan.eletzhofer at inquant.de
Thu Oct 30 09:22:21 EDT 2008
Log message for revision 92698:
glob style resource matching.
Changed:
U Sandbox/malthe/chameleon.html/CHANGES.txt
U Sandbox/malthe/chameleon.html/src/chameleon/html/language.py
U Sandbox/malthe/chameleon.html/src/chameleon/html/template.txt
A Sandbox/malthe/chameleon.html/src/chameleon/html/tests/layouts/example.js
A Sandbox/malthe/chameleon.html/src/chameleon/html/tests/layouts/example2.html
A Sandbox/malthe/chameleon.html/src/chameleon/html/tests/layouts/foo.js
A Sandbox/malthe/chameleon.html/src/chameleon/html/tests/layouts/one.css
A Sandbox/malthe/chameleon.html/src/chameleon/html/tests/layouts/two.css
-=-
Modified: Sandbox/malthe/chameleon.html/CHANGES.txt
===================================================================
--- Sandbox/malthe/chameleon.html/CHANGES.txt 2008-10-30 13:16:04 UTC (rev 92697)
+++ Sandbox/malthe/chameleon.html/CHANGES.txt 2008-10-30 13:22:20 UTC (rev 92698)
@@ -4,6 +4,8 @@
Head
~~~~
+- Added code to enable ``glob`` style matching of resources [seletz]
+
- Resource URLs are now made absolute to dettach them from the
template object. [malthe]
Modified: Sandbox/malthe/chameleon.html/src/chameleon/html/language.py
===================================================================
--- Sandbox/malthe/chameleon.html/src/chameleon/html/language.py 2008-10-30 13:16:04 UTC (rev 92697)
+++ Sandbox/malthe/chameleon.html/src/chameleon/html/language.py 2008-10-30 13:22:20 UTC (rev 92698)
@@ -1,8 +1,11 @@
import cgi
import os
import re
+import glob
import lxml.cssselect
+from copy import deepcopy
+
from zope import component
from chameleon.core import translation
@@ -194,14 +197,41 @@
# attributes lists
self.file_dependencies = []
self.parse(file(filename).read())
-
+
+ def expand_tag(self, original, matches, attribute="href"):
+ tag = original
+ for nr, match in enumerate(matches):
+ match = match[len(self.path)+1:]
+ if nr == 0:
+ tag.attrib[attribute] = match
+ else:
+ new = deepcopy(tag)
+ new.attrib[attribute] = match
+ new.tail = "\n"
+ tag.tail = "\n"
+ tag.addnext(new)
+ tag = new
+
+ def glob_resources(self, root, attribute, xpath, ns={}):
+ """Resource globbing"""
+ tags = root.xpath(xpath, namespaces=ns)
+ for tag in tags:
+ href = tag.attrib.get(attribute)
+ matches = glob.glob(os.path.join(self.path, href))
+ if len(matches):
+ self.expand_tag(tag, matches, attribute)
+
def parse(self, body):
root, doctype = super(DynamicHTMLParser, self).parse(body)
# reset dynamic identifier lists
self.slots = []
self.attributes = []
-
+
+ # process possible resource globbing
+ self.glob_resources(root, "href", './/xmlns:link', ns={'xmlns': config.XHTML_NS})
+ self.glob_resources(root, "src", './/xmlns:script', ns={'xmlns': config.XHTML_NS})
+
# process dynamic rules
links = root.xpath(
'.//xmlns:link[@rel="xss"]', namespaces={'xmlns': config.XHTML_NS})
Modified: Sandbox/malthe/chameleon.html/src/chameleon/html/template.txt
===================================================================
--- Sandbox/malthe/chameleon.html/src/chameleon/html/template.txt 2008-10-30 13:16:04 UTC (rev 92697)
+++ Sandbox/malthe/chameleon.html/src/chameleon/html/template.txt 2008-10-30 13:22:20 UTC (rev 92698)
@@ -86,3 +86,29 @@
<img src="http://host/example.png" />
</body>
</html>
+
+Globbing support for easy resource inclusion
+--------------------------------------------
+
+It's tedious to inlude all the resources for a template manually. Let's use
+simple globbing::
+
+ >>> filename = os.path.join(path, 'layouts', 'example2.html')
+ >>> template = DynamicHTMLFile(filename)
+ >>> print template(context=object(), request=object())
+ <html>
+ <head>
+ <link href="http://host/one.css" rel="stylesheet" type="text/css" />
+ <link href="http://host/two.css" rel="stylesheet" type="text/css" />
+ <script src="http://host/example.js" type="text/javascript">
+ </script>
+ <script src="http://host/foo.js" type="text/javascript">
+ </script>
+ </head>
+ <body>
+ <div id="content">
+ This is the content area.
+ </div>
+ </body>
+ </html>
+
Added: Sandbox/malthe/chameleon.html/src/chameleon/html/tests/layouts/example.js
===================================================================
--- Sandbox/malthe/chameleon.html/src/chameleon/html/tests/layouts/example.js (rev 0)
+++ Sandbox/malthe/chameleon.html/src/chameleon/html/tests/layouts/example.js 2008-10-30 13:22:20 UTC (rev 92698)
@@ -0,0 +1 @@
+/* example.js */
Added: Sandbox/malthe/chameleon.html/src/chameleon/html/tests/layouts/example2.html
===================================================================
--- Sandbox/malthe/chameleon.html/src/chameleon/html/tests/layouts/example2.html (rev 0)
+++ Sandbox/malthe/chameleon.html/src/chameleon/html/tests/layouts/example2.html 2008-10-30 13:22:20 UTC (rev 92698)
@@ -0,0 +1,12 @@
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <link rel="stylesheet" type="text/css" href="*.css" />
+ <script type="text/javascript" src="*.js">
+ </script>
+ </head>
+ <body>
+ <div id="content">
+ This is the content area.
+ </div>
+ </body>
+</html>
Added: Sandbox/malthe/chameleon.html/src/chameleon/html/tests/layouts/foo.js
===================================================================
--- Sandbox/malthe/chameleon.html/src/chameleon/html/tests/layouts/foo.js (rev 0)
+++ Sandbox/malthe/chameleon.html/src/chameleon/html/tests/layouts/foo.js 2008-10-30 13:22:20 UTC (rev 92698)
@@ -0,0 +1 @@
+/* foo.js */
Added: Sandbox/malthe/chameleon.html/src/chameleon/html/tests/layouts/one.css
===================================================================
--- Sandbox/malthe/chameleon.html/src/chameleon/html/tests/layouts/one.css (rev 0)
+++ Sandbox/malthe/chameleon.html/src/chameleon/html/tests/layouts/one.css 2008-10-30 13:22:20 UTC (rev 92698)
@@ -0,0 +1 @@
+/* one.css */
Added: Sandbox/malthe/chameleon.html/src/chameleon/html/tests/layouts/two.css
===================================================================
--- Sandbox/malthe/chameleon.html/src/chameleon/html/tests/layouts/two.css (rev 0)
+++ Sandbox/malthe/chameleon.html/src/chameleon/html/tests/layouts/two.css 2008-10-30 13:22:20 UTC (rev 92698)
@@ -0,0 +1 @@
+/* two.css */
More information about the Checkins
mailing list