root/trunk/demo/w3c/css/demo.py

Revision 404, 5.2 kB (checked in by sholloway, 5 years ago)

Significant refactoring for CSSParser and CSSCascadeStrategy acquisition from SkinModel? instead of xmlSkinner.
Transitioned use of "href" to simply "ref"
Tested the wx skinning demos, fixing some
All tests pass.

Line 
1 #!/usr/bin/env python
2 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 ##~ Copyright (C) 2002-2004  TechGame Networks, LLC.
4 ##~
5 ##~ This library is free software; you can redistribute it and/or
6 ##~ modify it under the terms of the BSD style License as found in the
7 ##~ LICENSE file included with this distribution.
8 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9
10 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 #~ Imports
12 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13
14 from pprint import pprint
15 import xml.dom.minidom
16 from TG.w3c import css, cssDOMElementInterface
17
18 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19 #~ Constants / Variables / Etc.
20 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21
22 stylesheet = """\
23 * {
24     font-size: medium;
25     bgcolor: orange;
26     fgcolor: blue;
27 }
28
29 slideshow {
30     bgcolor: black;
31     fgcolor: white;
32 }
33
34 slideshow * {
35     fgcolor: inherit;
36     bgcolor: inherit;
37 }
38
39 slideshow > title {
40     font-size: 80pt;
41 }
42
43 slide {
44     image: url('page-image.png');
45 }
46
47 slide:last-child {
48     fgcolor: yellow;
49     image: url('last-page-image.png');
50 }
51
52 slide > title {
53     font-size: 40pt;
54 }
55
56 point {
57     font-size: medium;
58 }
59
60 point#special {
61     font-size: xx-large;
62     fgcolor: red;
63     bgcolor: yellow;
64 }
65 """
66
67 document = """\
68 <?xml version='1.0' ?>
69 <!-- Derived from 13.7.2 DOM Example of the Python 2.3 Documentation -->
70 <slideshow>
71     <title>Demo slideshow combined with CSS</title>
72
73     <slide>
74         <title>Intro CSS Slide</title>
75         <point>This is a CSS demo for xml.dom</point>
76         <point>for processing slides with CSS!</point>
77     </slide>
78
79     <slide>
80         <title>Another CSS demo slide</title>
81
82         <point>It is important</point>
83         <point>To have more than</point>
84         <point>one slide</point>
85     </slide>
86
87     <slide>
88         <title>Last CSS demo slide title</title>
89         <point id='special'>Thanks for looking at CSS!</point>
90     </slide>
91
92 </slideshow>
93 """
94
95 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
96 #~ Definitions
97 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
98
99 def getCSSAttr(self, cssCascade, attrName, default=NotImplemented):
100     if attrName in self.cssAttrs:
101         return self.cssAttrs[attrName]
102
103     result = None
104
105     attrValue = self.attributes.get(attrName, None)
106     if attrValue is not None:
107         result = cssCascade.parser.parseSingleAttr(attrValue.value)
108
109     if result is None:
110         result = cssCascade.findStyleFor(self.cssElement, attrName, default)
111
112         print
113         print "SHANE:"
114         for e in cssCascade.findAllCSSRulesFor(self.cssElement):
115             print e
116         print
117
118
119     if result == 'inherit':
120         if hasattr(self.parentNode, 'getCSSAttr'):
121             result = self.parentNode.getCSSAttr(cssCascade, attrName, default)
122         elif default is not NotImplemented:
123             return default
124         else:
125             raise LookupError("Could not find inherited CSS attribute value for '%s'" % (attrName,))
126
127     self.cssAttrs[attrName] = result
128     return result
129 xml.dom.minidom.Element.getCSSAttr = getCSSAttr
130
131 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
132 #~ Demo
133 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
134
135 # this is an informal schema that maps tag names to the css attributes they
136 # should have
137 tagToAttrNames = {
138     'slideshow': 'fgcolor bgcolor'.split(),
139     'title': 'font-size fgcolor bgcolor'.split(),
140     'slide': 'image fgcolor bgcolor'.split(),
141     'point': 'font-size fgcolor bgcolor'.split(),
142     }
143
144 def visitElement(element, children, cssCascade):
145     """Visits individual elements, and sets css attributes on them"""
146     element.cssElement = cssDOMElementInterface.CSSDOMElementInterface(element)
147     element.cssAttrs = {}
148     element.cssElement.onCSSParserVisit(cssCascade.parser)
149
150     cssAttrMap = {}
151     for cssAttrName in tagToAttrNames.get(element.tagName, []):
152         cssAttrMap[cssAttrName] = element.getCSSAttr(cssCascade, cssAttrName)
153
154     result = ["%s: %r" % (element.tagName, cssAttrMap)]
155     result.extend(["    " + r for line in children for r in line])
156     return result
157
158 def visitElementNodes(root, visit=visitElement, **kw):
159     """Visits all ELEMENT_NODEs of a dom recursively"""
160     for node in root.childNodes:
161         if node.nodeType == node.ELEMENT_NODE:
162             children = visitElementNodes(node, visit, **kw)
163             yield visit(node, children, **kw)
164
165 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
166
167 if __name__ == "__main__":
168     # parse the document into a dom
169     dom = xml.dom.minidom.parseString(document)
170
171     # parse the stylesheet
172     cssParser = css.CSSParser()
173     authorSS = cssParser.parse(stylesheet)
174     # create the css cascade from the stylesheet
175     cssCascade = css.CSSCascadeStrategy(authorSS)
176     cssCascade.parser = cssParser
177
178     # Visit all the nodes with the cssParser for inline styles and cssCascade for cssAttribute resolution
179     slideshow = visitElementNodes(dom, cssCascade=cssCascade).next()
180     slideshow = "    "+"\n    ".join(slideshow )
181
182     # print our prepared result!
183     print
184     print
185     print "Results of CSS attributed DOM:"
186     print
187     print slideshow
188     print
189
Note: See TracBrowser for help on using the browser.