Edgewall Software

Changes between Version 4 and Version 5 of GenshiRecipes/HtmlTransform


Ignore:
Timestamp:
Jul 4, 2007, 5:59:19 PM (17 years ago)
Author:
athomas
Comment:

Add Transformer filter example

Legend:

Unmodified
Added
Removed
Modified
  • GenshiRecipes/HtmlTransform

    v4 v5  
    123123== Using the Transformer Filter ==
    124124
    125 TODO: write this
     125The Transformer filter equivalent of the above code:
     126
     127{{{
     128#!python
     129import sys
     130from genshi.input import HTMLParser
     131from genshi.builder import tag
     132from genshi.filters.transform import Transformer
     133
     134
     135header = tag.div(tag.img(src="logo.png", alt="Bad Style"), id="header")
     136
     137insert_header = Transformer('body').prepend(header).end()
     138apply_strong = Transformer('body//b').unwrap().wrap('strong').end()
     139apply_em = Transformer('body//i').unwrap().wrap('em').end()
     140
     141
     142def transform(html_filename):
     143    html_fileobj = open(html_filename)
     144    html_parser = HTMLParser(html_fileobj, html_filename)
     145    html_stream = html_parser.parse()
     146    transformed_stream = html_stream | insert_header | apply_strong | apply_em
     147    print transformed_stream.render('xhtml', doctype='html')
     148    html_fileobj.close()
     149
     150
     151if __name__ == '__main__':
     152    transform(sys.argv[1])
     153}}}
    126154
    127155----