Mercurial > genshi > mirror
annotate examples/transform/run.py @ 107:8b6bd2d920c1 trunk
Add example that shows how to transform an HTML document.
author | cmlenz |
---|---|
date | Fri, 28 Jul 2006 16:52:23 +0000 |
parents | |
children | 61f58e3a123d |
rev | line source |
---|---|
107
8b6bd2d920c1
Add example that shows how to transform an HTML document.
cmlenz
parents:
diff
changeset
|
1 #!/usr/bin/python |
8b6bd2d920c1
Add example that shows how to transform an HTML document.
cmlenz
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
8b6bd2d920c1
Add example that shows how to transform an HTML document.
cmlenz
parents:
diff
changeset
|
3 |
8b6bd2d920c1
Add example that shows how to transform an HTML document.
cmlenz
parents:
diff
changeset
|
4 import os |
8b6bd2d920c1
Add example that shows how to transform an HTML document.
cmlenz
parents:
diff
changeset
|
5 |
8b6bd2d920c1
Add example that shows how to transform an HTML document.
cmlenz
parents:
diff
changeset
|
6 from markup.input import HTMLParser |
8b6bd2d920c1
Add example that shows how to transform an HTML document.
cmlenz
parents:
diff
changeset
|
7 from markup.template import Context, TemplateLoader |
8b6bd2d920c1
Add example that shows how to transform an HTML document.
cmlenz
parents:
diff
changeset
|
8 |
8b6bd2d920c1
Add example that shows how to transform an HTML document.
cmlenz
parents:
diff
changeset
|
9 def run(): |
8b6bd2d920c1
Add example that shows how to transform an HTML document.
cmlenz
parents:
diff
changeset
|
10 basepath = os.path.dirname(os.path.abspath(__file__)) |
8b6bd2d920c1
Add example that shows how to transform an HTML document.
cmlenz
parents:
diff
changeset
|
11 loader = TemplateLoader([basepath]) |
8b6bd2d920c1
Add example that shows how to transform an HTML document.
cmlenz
parents:
diff
changeset
|
12 html_filename = os.path.join(basepath, 'index.html') |
8b6bd2d920c1
Add example that shows how to transform an HTML document.
cmlenz
parents:
diff
changeset
|
13 html_fileobj = open(html_filename) |
8b6bd2d920c1
Add example that shows how to transform an HTML document.
cmlenz
parents:
diff
changeset
|
14 try: |
8b6bd2d920c1
Add example that shows how to transform an HTML document.
cmlenz
parents:
diff
changeset
|
15 html = HTMLParser(html_fileobj, html_filename) |
8b6bd2d920c1
Add example that shows how to transform an HTML document.
cmlenz
parents:
diff
changeset
|
16 tmpl = loader.load('template.xml') |
8b6bd2d920c1
Add example that shows how to transform an HTML document.
cmlenz
parents:
diff
changeset
|
17 print tmpl.generate(Context(input=html)).render('xhtml') |
8b6bd2d920c1
Add example that shows how to transform an HTML document.
cmlenz
parents:
diff
changeset
|
18 finally: |
8b6bd2d920c1
Add example that shows how to transform an HTML document.
cmlenz
parents:
diff
changeset
|
19 html_fileobj.close() |
8b6bd2d920c1
Add example that shows how to transform an HTML document.
cmlenz
parents:
diff
changeset
|
20 |
8b6bd2d920c1
Add example that shows how to transform an HTML document.
cmlenz
parents:
diff
changeset
|
21 |
8b6bd2d920c1
Add example that shows how to transform an HTML document.
cmlenz
parents:
diff
changeset
|
22 if __name__ == '__main__': |
8b6bd2d920c1
Add example that shows how to transform an HTML document.
cmlenz
parents:
diff
changeset
|
23 run() |