[z3-checkins] r55608 - z3/deliverance/sandboxes/paul/dvng

paul at codespeak.net paul at codespeak.net
Thu Jun 5 22:21:17 CEST 2008


Author: paul
Date: Thu Jun  5 22:21:16 2008
New Revision: 55608

Modified:
   z3/deliverance/sandboxes/paul/dvng/contentcustom.xsl
   z3/deliverance/sandboxes/paul/dvng/dvngcompiler.py
   z3/deliverance/sandboxes/paul/dvng/stub.xsl
   z3/deliverance/sandboxes/paul/dvng/themeset.rng
   z3/deliverance/sandboxes/paul/dvng/themeset1.xml
Log:
Support rewriting of links, embedding property information into themesets

Modified: z3/deliverance/sandboxes/paul/dvng/contentcustom.xsl
==============================================================================
--- z3/deliverance/sandboxes/paul/dvng/contentcustom.xsl	(original)
+++ z3/deliverance/sandboxes/paul/dvng/contentcustom.xsl	Thu Jun  5 22:21:16 2008
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
-  <xsl:template match="/">
+  <xsl:template match="/foo">
     <div>I overroad everything!</div>
   </xsl:template>
   <xsl:template match="ul[@id='breadcrumbs']">

Modified: z3/deliverance/sandboxes/paul/dvng/dvngcompiler.py
==============================================================================
--- z3/deliverance/sandboxes/paul/dvng/dvngcompiler.py	(original)
+++ z3/deliverance/sandboxes/paul/dvng/dvngcompiler.py	Thu Jun  5 22:21:16 2008
@@ -16,10 +16,10 @@
 NSMAP = {"dv": DV_NAMESPACE, "xsl": XSL_NAMESPACE}
 
 
-def dump(node, fn):
+def dump(node, fn, vardir):
     """Convenience function for quick saving to var directory """
     
-    fullfn = os.path.join("var", fn)
+    fullfn = os.path.join(vardir, fn)
     f = open(fullfn, "w")
     f.write(tostring(node))
     f.close()
@@ -44,11 +44,17 @@
     moduledir = os.path.dirname(os.path.abspath(__file__))
     heredir = os.path.dirname(moduledir)[1:]
 
+
     def __init__(self, themesetfn):
         # Some paths and filenames
         self.themesetfn = os.path.join(self.moduledir, themesetfn)
         self.themes = []
         self.themeset = etree.ElementTree(file=self.themesetfn)
+        xp = "/dv:themeset/dv:properties/dv:id/text()"
+        self.themesetid = self.themeset.xpath(xp, namespaces=NSMAP)[0]
+
+        # basedir is the directory where themesetfn is located
+        self.basedir = os.path.split(themesetfn)[0]
 
         # If there are custom stylesheets for hacking the theme (on
         # loading) or the content (per-request), load them now.
@@ -59,14 +65,14 @@
             customcontenthref = customizers[0].get("content", None)
 
         if customcontenthref is not None:
-            fn = os.path.join(self.moduledir, customcontenthref)
+            fn = os.path.join(self.basedir, customcontenthref)
             self.customcontent = etree.ElementTree(file=fn)
         else:
             self.customcontent = None
 
         # Now load the theme customizer, if it is there
         if customthemehref is not None:
-            fn = os.path.join(self.moduledir, customthemehref)
+            fn = os.path.join(self.basedir, customthemehref)
             customizetheme = etree.ElementTree(file=fn)
             self.customizetheme = etree.XSLT(customizetheme)
         else:
@@ -76,6 +82,56 @@
         stubfn = os.path.join(self.moduledir, "stub.xsl")
         self.xslt = etree.ElementTree(file=stubfn).getroot()
 
+        # Now do the work
+        self.makeThemes()
+        self.assembleStylesheet()
+
+    #dvng.debug()
+
+
+    def _cleanupLinks(self):
+        """Try to change links to be absolute"""
+
+        # This strategy, like the rest of DVNG, supposes that things
+        # are done in chunks.  Namely, something will have run before
+        # this step which makes all the CSS relative to a directory
+        # called "static".  This lets the theme.html file work from
+        # disk.
+        #
+        # This step then "compiles" those links into a form like this:
+        #   <link rel="stylesheet" href="{$staticdir}"/>
+        # An xsltparameter is then created to default $staticdir to:
+        #   /++dv++/themesets/plone/
+        # ...where "plone" is the ID of the themeset.  A trailing slash 
+        # is required.  A processor could choose to override this by 
+        # passing in an xsl:param.
+
+        #print "in _cleanupLinks"
+        basexp = "/*/xsl:template[@match='/']/xsl:choose//"
+        xp1 = basexp + "head/link[starts-with(@href, 'static/')]"
+        xp2 = basexp + "head/script[starts-with(@src, 'static/')]"
+        xp3 = basexp + "body//img[starts-with(@src, 'static/')]"
+        staticprefix = "{$staticprefix}"
+        staticpath = "/++dv++/themesets/%s/static/" % self.themesetid
+
+        for link in self.xslt.xpath(xp1, namespaces=NSMAP):
+            newvalue = staticprefix + link.get("href")[7:]
+            link.set("href", newvalue)
+
+        for script in self.xslt.xpath(xp2, namespaces=NSMAP):
+            newvalue = staticprefix + link.get("src")[7:]
+            link.set("src", newvalue)
+
+        for img in self.xslt.xpath(xp3, namespaces=NSMAP):
+            newvalue = staticprefix + link.get("src")[7:]
+            link.set("src", newvalue)
+
+        # Finally, add the xsl:param at the top
+        xslparam = etree.SubElement(self.xslt, XSL + "param", 
+                                    name="staticprefix",
+                                    select="'%s'" % staticpath)
+
+
     def makeThemes(self):
         """For each theme and page type, make a new Theme"""
 		
@@ -90,7 +146,7 @@
         for ptt in pt + t:
             i = i + 1
             pttid = get_tag(ptt) + "-" + str(i)
-            theme = Theme(ptt, pttid)
+            theme = Theme(ptt, pttid, basedir=self.basedir)
 
             # If the themeset says to customize the theme HTML, 
             # do so.  For example, some crazy breadcrumbs thing.
@@ -133,18 +189,23 @@
             for rule in self.customcontent.xpath("/*/*"):
                 self.xslt.insert(0, rule)
 
+        # Just for fun, take a stab at cleaning up the <style> and
+        # <link> references in the theme html
+        self._cleanupLinks()
+
 
-    def debug(self):
+
+    def debug(self, vardir="var"):
         """Dump the list of themes and the compiled themeset"""
         
         for theme in self.themes:
             print "----------\nTheme:", theme.pttid, "at", theme.href
             print tostring(theme.htmlroot)
-            dump(theme.htmlroot, theme.pttid + "-html.xml")
-            dump(theme.xslwhen, theme.pttid + "-xsl.xml")
+            dump(theme.htmlroot, theme.pttid + "-html.xml", vardir)
+            dump(theme.xslwhen, theme.pttid + "-xsl.xml", vardir)
 
         # Now dump the final, assembled XSLT to disk
-        dump(self.xslt, "dvfinalstage.xsl")
+        dump(self.xslt, "dvfinalstage.xsl", vardir)
 
     def asString(self):
         """Serialize the xslt as the string representation"""
@@ -154,11 +215,12 @@
 
 class Theme:
 	
-    def __init__(self, ptt, pttid):
+    def __init__(self, ptt, pttid, basedir):
         """Given a <pagetype> or a <theme>, make a theme"""
 
         self.ptt = ptt
         self.pttid = pttid
+        self.basedir = basedir
 
         # We are currently sitting on a <pagetype> or a 
         # <theme>.  The latter has the @href.  Thus, 
@@ -167,8 +229,9 @@
         self.href = ptt.xpath("ancestor-or-self::*/@href")[0]
 
         # Load the HTML doc and make a backup copy
-        self.orightmldoc = lxml.html.parse(self.href)
-        self.htmlroot = lxml.html.parse(self.href).getroot()
+        fullfn = os.path.join(self.basedir, self.href)
+        self.orightmldoc = lxml.html.parse(fullfn)
+        self.htmlroot = lxml.html.parse(fullfn).getroot()
 
         # Place to hold <xsl:param> nodes that will go at the
         # top
@@ -272,14 +335,10 @@
 def main():
     themesetfn = sys.argv[1]
     dvng = DVNG(themesetfn)
-    dvng.makeThemes()
-    dvng.assembleStylesheet()
-
     #dvng.debug()
-
     result = dvng.asString()
     return result
-	
+
 if __name__ == "__main__":
     result = main()
     print result

Modified: z3/deliverance/sandboxes/paul/dvng/stub.xsl
==============================================================================
--- z3/deliverance/sandboxes/paul/dvng/stub.xsl	(original)
+++ z3/deliverance/sandboxes/paul/dvng/stub.xsl	Thu Jun  5 22:21:16 2008
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
-  <xsl:template match="/">
+  <xsl:template match="/" priority="-1">
     <!-- This is the "entry point".  All the themes and pagetypes will be 
       compiled and appended in here-->
   </xsl:template>

Modified: z3/deliverance/sandboxes/paul/dvng/themeset.rng
==============================================================================
--- z3/deliverance/sandboxes/paul/dvng/themeset.rng	(original)
+++ z3/deliverance/sandboxes/paul/dvng/themeset.rng	Thu Jun  5 22:21:16 2008
@@ -6,45 +6,72 @@
         <element name="dv:themeset">
             <a:documentation>Declare one or more theme definitions along with optional loading of
                 custom XSL stylesheets.</a:documentation>
-            <optional>
-                <element name="dv:customizers">
-                    <a:documentation>(Optional) Point at an XSLT that modifies the theme (on
-                        loading) or the content (on each request.)</a:documentation>
-                    <optional>
-                        <attribute name="theme">
-                            <a:documentation>Path to an XSL file that will modify the theme before
-                                Deliverance compiles it.</a:documentation>
-                            <data type="anyURI"/>
-                        </attribute>
-                        <attribute name="content">
-                            <a:documentation>Path to an XSL file whose rules will get added, at a
-                                higher precedence, to the compiled themeset.</a:documentation>
-                            <data type="anyURI"/>
-                        </attribute>
-                    </optional>
-                </element>
-            </optional>
-            <oneOrMore>
-                <element name="dv:theme">
-                    <attribute name="href"/>
-                    <interleave>
-                        <zeroOrMore>
-                            <ref name="match.element"/>
-                        </zeroOrMore>
-                        <zeroOrMore>
-                            <element name="dv:pagetype">
-                                <a:documentation>Additional rules that get applied to certain
-                                    content under extra match conditions.</a:documentation>
+            <interleave>
+                <optional>
+                    <ref name="customizers.element"/>
+                </optional>
+                <optional>
+                    <ref name="properties.element"/>
+                </optional>
+                <oneOrMore>
+                    <element name="dv:theme">
+                        <attribute name="href"/>
+                        <interleave>
+                            <zeroOrMore>
                                 <ref name="match.element"/>
-                                <ref name="rules.element"/>
-                            </element>
-                        </zeroOrMore>
-                        <ref name="rules.element"/>
-                    </interleave>
-                </element>
-            </oneOrMore>
+                            </zeroOrMore>
+                            <zeroOrMore>
+                                <element name="dv:pagetype">
+                                    <a:documentation>Additional rules that get applied to certain
+                                        content under extra match conditions.</a:documentation>
+                                    <ref name="match.element"/>
+                                    <ref name="rules.element"/>
+                                </element>
+                            </zeroOrMore>
+                            <ref name="rules.element"/>
+                        </interleave>
+                    </element>
+                </oneOrMore>
+            </interleave>
         </element>
     </start>
+    <define name="properties.element">
+        <element name="dv:properties">
+            <a:documentation>Information about the themeset, such as title and version.</a:documentation>
+            <interleave>
+                <element name="dv:id">
+                    <text/>
+                </element>
+                <element name="dv:title">
+                    <text/>
+                </element>
+                <element name="dv:version">
+                    <text/>
+                </element>
+                <element name="dv:modified">
+                    <text/>
+                </element>
+            </interleave>
+        </element>
+    </define>
+    <define name="customizers.element">
+        <element name="dv:customizers">
+            <a:documentation>(Optional) Point at an XSLT that modifies the theme (on loading) or the
+                content (on each request.)</a:documentation>
+            <optional>
+                <attribute name="theme">
+                    <a:documentation>Path to an XSL file that will modify the theme before
+                        Deliverance compiles it.</a:documentation>
+                    <data type="anyURI"/>
+                </attribute>
+                <attribute name="content">
+                    <a:documentation>Path to an XSL file whose rules will get added, at a higher
+                        precedence, to the compiled themeset.</a:documentation>
+                    <data type="anyURI"/>
+                </attribute>
+            </optional>
+        </element>
+    </define>
     <define name="rules.element">
         <element name="dv:rules">
             <interleave>

Modified: z3/deliverance/sandboxes/paul/dvng/themeset1.xml
==============================================================================
--- z3/deliverance/sandboxes/paul/dvng/themeset1.xml	(original)
+++ z3/deliverance/sandboxes/paul/dvng/themeset1.xml	Thu Jun  5 22:21:16 2008
@@ -1,6 +1,12 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <?oxygen RNGSchema="themeset.rng" type="xml"?>
 <themeset xmlns="http://openplans.org/deliverance">
+  <properties>
+    <id>plone</id>
+    <title>Plone</title>
+    <version>3.1</version>
+    <modified>2007/11/13</modified>
+  </properties>
   <customizers theme="themecustom.xsl" content="contentcustom.xsl"/>
   <theme href="theme2.html">
     <!-- A pretty theme with a sidebar -->


More information about the z3-checkins mailing list