Mercurial > hg > contenttransformer
comparison contenttransformer/transformers.py @ 19:afd11b758da0
just require genshi for now
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 27 Oct 2010 09:09:37 -0700 |
parents | 6cbe4172b54b |
children | 9cf78a1c7373 |
comparison
equal
deleted
inserted
replaced
18:7800c6553c77 | 19:afd11b758da0 |
---|---|
1 import docutils.core | 1 import docutils.core |
2 import subprocess | 2 import subprocess |
3 from utils import import_path | 3 from utils import import_path |
4 from webob import Request, Response | 4 from webob import Request, Response |
5 | |
6 import genshi | |
7 from genshi.template import MarkupTemplate | |
5 | 8 |
6 class Transformer(object): | 9 class Transformer(object): |
7 """abstract base class for transformer objects""" | 10 """abstract base class for transformer objects""" |
8 def __init__(self, content, content_type): | 11 def __init__(self, content, content_type): |
9 self.content = content | 12 self.content = content |
58 html = docutils.core.publish_string(self.content, | 61 html = docutils.core.publish_string(self.content, |
59 writer_name='html', | 62 writer_name='html', |
60 settings_overrides=self.settings) | 63 settings_overrides=self.settings) |
61 return ('text/html', html) | 64 return ('text/html', html) |
62 | 65 |
63 try: | |
64 import genshi | |
65 from genshi.template import MarkupTemplate | |
66 | 66 |
67 class GenshiTransformer(Transformer): | 67 class GenshiTransformer(Transformer): |
68 | 68 |
69 def __init__(self, content, content_type, modules=()): | 69 def __init__(self, content, content_type, modules=()): |
70 """ | 70 """ |
71 - modules : strings of modules | 71 - modules : strings of modules |
72 """ | 72 """ |
73 self.variables = {} | 73 self.variables = {} |
74 for path in modules: | 74 for path in modules: |
75 module = import_path(path) | 75 module = import_path(path) |
76 name = path.rsplit('.')[-1] | 76 name = path.rsplit('.')[-1] |
77 self.variables[name] = module | 77 self.variables[name] = module |
78 Transformer.__init__(self, content, content_type) | 78 Transformer.__init__(self, content, content_type) |
79 | 79 |
80 def transform(self, request): | 80 def transform(self, request): |
81 variables = dict(request=request) | 81 variables = dict(request=request) |
82 template = MarkupTemplate(self.content) | 82 template = MarkupTemplate(self.content) |
83 stream = template.generate(**variables) | 83 stream = template.generate(**variables) |
84 return ('text/html', stream.render('html', doctype='html')) | 84 return ('text/html', stream.render('html', doctype='html')) |
85 | 85 |
86 except ImportError: | |
87 pass |