view examples/transform/run.py @ 533:dfb45908fadc

Thanks to Dave Abrahams for pointing out some deficiencies in the transformer filter: - `apply()` has been renamed to `map()`. - `filter()` applies a normal stream filter to the selection. - To reduce confusion with normal stream filter pipelines the `__or__` operator, which previously applied a custom transform, has been renamed to `apply()`. Also added a `substitute()` transformation that applies regex replacement to `TEXT` events.
author athomas
date Fri, 22 Jun 2007 16:42:38 +0000
parents 7a426ab6407a
children
line wrap: on
line source
#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import sys

from genshi.input import HTMLParser
from genshi.template import Context, MarkupTemplate

def transform(html_filename, tmpl_filename):
    tmpl_fileobj = open(tmpl_filename)
    tmpl = MarkupTemplate(tmpl_fileobj, tmpl_filename)
    tmpl_fileobj.close()

    html_fileobj = open(html_filename)
    html = HTMLParser(html_fileobj, html_filename)
    print tmpl.generate(Context(input=html)).render('xhtml')
    html_fileobj.close()

if __name__ == '__main__':
    basepath = os.path.dirname(os.path.abspath(__file__))
    tmpl_filename = os.path.join(basepath, 'template.xml')
    html_filename = os.path.join(basepath, 'index.html')
    transform(html_filename, tmpl_filename)
Copyright (C) 2012-2017 Edgewall Software