Mercurial > genshi > mirror
changeset 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 | f9473bdc93b2 |
children | 61f58e3a123d |
files | examples/transform/README.txt examples/transform/index.html examples/transform/run.py examples/transform/template.xml |
diffstat | 4 files changed, 72 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
new file mode 100644 --- /dev/null +++ b/examples/transform/README.txt @@ -0,0 +1,3 @@ +This example shows how to transform some HTML input, while adding +elements such as headers, and using <em>/<strong> instead of <i>/<b>. +The output is that a proper XTHML document.
new file mode 100644 --- /dev/null +++ b/examples/transform/index.html @@ -0,0 +1,24 @@ +<HTML> + <HEAD> + <TITLE>Aaarrgh</TITLE> + <LINK REL=stylesheet href='badstyle.css'> + </HEAD> + + <BODY> + <H1>Aaargh</H1> + <P> + <B>Lorem <I>ipsum</I></B> dolor sit amet, consectetur<BR> + adipisicing elit, sed do eiusmod tempor incididunt ut<BR> + labore et dolore magna aliqua. Ut enim ad minim veniam,<BR> + quis nostrud exercitation ullamco laboris nisi ut<BR> + aliquip ex ea commodo consequat. + </P> + <P> + Duis aute irure dolor in reprehenderit in voluptate velit<BR> + esse cillum dolore eu fugiat nulla pariatur. Excepteur sint<BR> + occaecat cupidatat non proident, sunt in culpa qui officia<BR> + deserunt mollit anim <I>id est laborum</I>. + </P> + </BODY> + +</HTML>
new file mode 100644 --- /dev/null +++ b/examples/transform/run.py @@ -0,0 +1,23 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import os + +from markup.input import HTMLParser +from markup.template import Context, TemplateLoader + +def run(): + basepath = os.path.dirname(os.path.abspath(__file__)) + loader = TemplateLoader([basepath]) + html_filename = os.path.join(basepath, 'index.html') + html_fileobj = open(html_filename) + try: + html = HTMLParser(html_fileobj, html_filename) + tmpl = loader.load('template.xml') + print tmpl.generate(Context(input=html)).render('xhtml') + finally: + html_fileobj.close() + + +if __name__ == '__main__': + run()
new file mode 100644 --- /dev/null +++ b/examples/transform/template.xml @@ -0,0 +1,22 @@ +<!DOCTYPE html + PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> +<html xmlns:py="http://markup.edgewall.org/" py:strip=""> + + <!--! Add a header DIV on top of every page with a logo image --> + <body py:match="body"> + <div id="header"> + <img src="logo.png" alt="Bad Style"/> + </div> + ${select('*')} + </body> + + <!--! Use semantic instead of presentational tags for emphasis --> + <strong py:match="B|b">${select('*|text()')}</strong> + <em py:match="I|i">${select('*|text()')}</em> + + <!--! Include the actual HTML stream, which will be processed by the rules + defined above --> + ${input} + +</html>