annotate examples/transform/run.py @ 554:161aa8a43b1f experimental-newctxt

New experimental branch for trying a different approach at context data stack management and simplified code evaluation/execution.
author cmlenz
date Mon, 02 Jul 2007 17:24:32 +0000
parents 7a426ab6407a
children
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
108
9cf42fb6b21e Minor improvements to transform example.
cmlenz
parents: 107
diff changeset
5 import sys
107
5a9b6e0aa3cc Add example that shows how to transform an HTML document.
cmlenz
parents:
diff changeset
6
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 108
diff changeset
7 from genshi.input import HTMLParser
233
7a426ab6407a * Added implementation of a simple text-based template engine. Closes #47.
cmlenz
parents: 230
diff changeset
8 from genshi.template import Context, MarkupTemplate
107
5a9b6e0aa3cc Add example that shows how to transform an HTML document.
cmlenz
parents:
diff changeset
9
108
9cf42fb6b21e Minor improvements to transform example.
cmlenz
parents: 107
diff changeset
10 def transform(html_filename, tmpl_filename):
9cf42fb6b21e Minor improvements to transform example.
cmlenz
parents: 107
diff changeset
11 tmpl_fileobj = open(tmpl_filename)
233
7a426ab6407a * Added implementation of a simple text-based template engine. Closes #47.
cmlenz
parents: 230
diff changeset
12 tmpl = MarkupTemplate(tmpl_fileobj, tmpl_filename)
108
9cf42fb6b21e Minor improvements to transform example.
cmlenz
parents: 107
diff changeset
13 tmpl_fileobj.close()
9cf42fb6b21e Minor improvements to transform example.
cmlenz
parents: 107
diff changeset
14
107
5a9b6e0aa3cc Add example that shows how to transform an HTML document.
cmlenz
parents:
diff changeset
15 html_fileobj = open(html_filename)
108
9cf42fb6b21e Minor improvements to transform example.
cmlenz
parents: 107
diff changeset
16 html = HTMLParser(html_fileobj, html_filename)
9cf42fb6b21e Minor improvements to transform example.
cmlenz
parents: 107
diff changeset
17 print tmpl.generate(Context(input=html)).render('xhtml')
9cf42fb6b21e Minor improvements to transform example.
cmlenz
parents: 107
diff changeset
18 html_fileobj.close()
107
5a9b6e0aa3cc Add example that shows how to transform an HTML document.
cmlenz
parents:
diff changeset
19
5a9b6e0aa3cc Add example that shows how to transform an HTML document.
cmlenz
parents:
diff changeset
20 if __name__ == '__main__':
108
9cf42fb6b21e Minor improvements to transform example.
cmlenz
parents: 107
diff changeset
21 basepath = os.path.dirname(os.path.abspath(__file__))
9cf42fb6b21e Minor improvements to transform example.
cmlenz
parents: 107
diff changeset
22 tmpl_filename = os.path.join(basepath, 'template.xml')
9cf42fb6b21e Minor improvements to transform example.
cmlenz
parents: 107
diff changeset
23 html_filename = os.path.join(basepath, 'index.html')
9cf42fb6b21e Minor improvements to transform example.
cmlenz
parents: 107
diff changeset
24 transform(html_filename, tmpl_filename)
Copyright (C) 2012-2017 Edgewall Software