| 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 |
import sys |
|---|
| 15 |
from pprint import pprint |
|---|
| 16 |
import xml.dom.minidom |
|---|
| 17 |
|
|---|
| 18 |
from TG.w3c import css, cssDOMElementInterface |
|---|
| 19 |
|
|---|
| 20 |
import demo |
|---|
| 21 |
|
|---|
| 22 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 23 |
#~ Definitions |
|---|
| 24 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 25 |
|
|---|
| 26 |
if __name__ == "__main__": |
|---|
| 27 |
print 'Usage: demoEx.py <style.css> <slideshow.xml>' |
|---|
| 28 |
print |
|---|
| 29 |
|
|---|
| 30 |
print |
|---|
| 31 |
cssFilename = ''.join(sys.argv[1:2]) or 'style.css' |
|---|
| 32 |
cssFile = open(cssFilename, 'r') |
|---|
| 33 |
xmlFilename = ''.join(sys.argv[2:3]) or 'slideshow.xml' |
|---|
| 34 |
xmlFile = open(xmlFilename, 'r') |
|---|
| 35 |
|
|---|
| 36 |
# parse the stylesheet |
|---|
| 37 |
cssParser = css.CSSParser() |
|---|
| 38 |
print "Parsing \"%s\" using TG.w3c.css..." % (cssFilename,) |
|---|
| 39 |
authorSS = cssParser.parseFile(cssFile) |
|---|
| 40 |
|
|---|
| 41 |
# create the css cascade from the stylesheet |
|---|
| 42 |
cssCascade = css.CSSCascadeStrategy(authorSS) |
|---|
| 43 |
cssCascade.parser = cssParser |
|---|
| 44 |
|
|---|
| 45 |
print "Parsing \"%s\" using xml.dom.minidom..." % (xmlFilename,) |
|---|
| 46 |
# parse the document into a dom |
|---|
| 47 |
dom = xml.dom.minidom.parse(xmlFile) |
|---|
| 48 |
|
|---|
| 49 |
print "Applying the CSS Cascade to the XML DOM..." |
|---|
| 50 |
# Visit all the nodes with the cssParser for inline styles and cssCascade for cssAttribute resolution |
|---|
| 51 |
slideshow = demo.visitElementNodes(dom, cssCascade=cssCascade).next() |
|---|
| 52 |
slideshow = " "+"\n ".join(slideshow ) |
|---|
| 53 |
|
|---|
| 54 |
# print our prepared result! |
|---|
| 55 |
print |
|---|
| 56 |
print |
|---|
| 57 |
print "Results of CSS attributed DOM:" |
|---|
| 58 |
print |
|---|
| 59 |
print slideshow |
|---|
| 60 |
print |
|---|
| 61 |
|
|---|