# HG changeset patch # User cmlenz # Date 1158945773 0 # Node ID 0f2bc982a2b9c81d8653855a9bd5c3ed6b2c0a3a # Parent 77d92c5c23d34e10a6dd14ef5e6750f4b48f2a08 Split up the plugin implementation into two classes: one for markup templates (?genshi-markup? or just ?genshi?) and one for text templates (?genshi-text?). Also added an example for plain-text templating to the TurboGears example app. diff --git a/examples/turbogears/genshitest/controllers.py b/examples/turbogears/genshitest/controllers.py --- a/examples/turbogears/genshitest/controllers.py +++ b/examples/turbogears/genshitest/controllers.py @@ -23,6 +23,11 @@ "Genshi", rows=5, cols=40)) + @expose(template="genshi-text:genshitest.templates.plain", + content_type='text/plain; charset=utf-8') + def plain(self): + return dict(name='world') + @expose(template="genshitest.templates.login") def login(self, forward_url=None, previous_url=None, *args, **kw): diff --git a/examples/turbogears/genshitest/templates/plain.txt b/examples/turbogears/genshitest/templates/plain.txt new file mode 100644 --- /dev/null +++ b/examples/turbogears/genshitest/templates/plain.txt @@ -0,0 +1,8 @@ +#choose + #when name + Hello, $name! + #end + #otherwise + Hello, anonymous! + #end +#end diff --git a/examples/turbogears/genshitest/templates/welcome.html b/examples/turbogears/genshitest/templates/welcome.html --- a/examples/turbogears/genshitest/templates/welcome.html +++ b/examples/turbogears/genshitest/templates/welcome.html @@ -33,5 +33,7 @@ ${ET(widget.display())} +

Here's a link to the output of a plain-text template.

+ diff --git a/genshi/plugin.py b/genshi/plugin.py --- a/genshi/plugin.py +++ b/genshi/plugin.py @@ -21,7 +21,8 @@ from genshi.core import Attrs, Stream, QName from genshi.eval import Undefined from genshi.input import HTML, XML -from genshi.template import Context, MarkupTemplate, Template, TemplateLoader +from genshi.template import Context, MarkupTemplate, Template, TemplateLoader, \ + TextTemplate def ET(element): """Converts the given ElementTree element to a markup stream.""" @@ -42,9 +43,12 @@ yield Stream.TEXT, element.tail, (None, -1, -1) -class TemplateEnginePlugin(object): +class AbstractTemplateEnginePlugin(object): """Implementation of the plugin API.""" + template_class = None + extension = None + def __init__(self, extra_vars_func=None, options=None): if options is None: options = {} @@ -59,15 +63,15 @@ a string. """ if template_string is not None: - return MarkupTemplate(template_string) + return self.template_class(template_string) divider = templatename.rfind('.') if divider >= 0: package = templatename[:divider] - basename = templatename[divider + 1:] + '.html' + basename = templatename[divider + 1:] + self.extension templatename = resource_filename(package, basename) - return self.loader.load(templatename) + return self.loader.load(templatename, cls=self.template_class) def render(self, info, format='html', fragment=False, template=None): """Render the template to a string using the provided info.""" @@ -77,12 +81,7 @@ """Render the output to an event stream.""" if not isinstance(template, Template): template = self.load_template(template) - - data = {'ET': ET, 'HTML': HTML, 'XML': XML} - if self.get_extra_vars: - data.update(self.get_extra_vars()) - data.update(info) - ctxt = Context(**data) + ctxt = Context(**info) # Some functions for Kid compatibility def defined(name): @@ -93,3 +92,33 @@ ctxt['value_of'] = value_of return template.generate(ctxt) + + +class MarkupTemplateEnginePlugin(AbstractTemplateEnginePlugin): + """Implementation of the plugin API for markup templates.""" + + template_class = MarkupTemplate + extension = '.html' + + def transform(self, info, template): + """Render the output to an event stream.""" + data = {'ET': ET, 'HTML': HTML, 'XML': XML} + if self.get_extra_vars: + data.update(self.get_extra_vars()) + data.update(info) + return super(MarkupTemplateEnginePlugin, self).transform(data, template) + + +class TextTemplateEnginePlugin(AbstractTemplateEnginePlugin): + """Implementation of the plugin API for text templates.""" + + template_class = TextTemplate + extension = '.txt' + + def transform(self, info, template): + """Render the output to an event stream.""" + data = {} + if self.get_extra_vars: + data.update(self.get_extra_vars()) + data.update(info) + return super(TextTemplateEnginePlugin, self).transform(data, template) diff --git a/setup.py b/setup.py --- a/setup.py +++ b/setup.py @@ -52,6 +52,8 @@ extras_require = {'plugin': ['setuptools>=0.6a2']}, entry_points = """ [python.templating.engines] - genshi = genshi.plugin:TemplateEnginePlugin[plugin] + genshi = genshi.plugin:MarkupTemplateEnginePlugin[plugin] + genshi-markup = genshi.plugin:MarkupTemplateEnginePlugin[plugin] + genshi-text = genshi.plugin:TextTemplateEnginePlugin[plugin] """, )