annotate examples/transform/run.py @ 107:5a9b6e0aa3cc

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