annotate genshi/template/plugin.py @ 798:c690de5abafd stable-0.5.x

Ported [914], [970], and [971] to 0.5.x branch.
author cmlenz
date Thu, 15 Jan 2009 23:50:09 +0000
parents cf0a0a066aec
children 0d9e87c6cf6e
rev   line source
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
2 #
772
cf0a0a066aec Ported [897] back to 0.5.x branch.
cmlenz
parents: 770
diff changeset
3 # Copyright (C) 2006-2008 Edgewall Software
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
4 # Copyright (C) 2006 Matthew Good
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
5 # All rights reserved.
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
6 #
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
7 # This software is licensed as described in the file COPYING, which
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
8 # you should have received as part of this distribution. The terms
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
9 # are also available at http://genshi.edgewall.org/wiki/License.
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
10 #
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
11 # This software consists of voluntary contributions made by many
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
12 # individuals. For the exact contribution history, see the revision
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
13 # history and logs, available at http://genshi.edgewall.org/log/.
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
14
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
15 """Basic support for the template engine plugin API used by TurboGears and
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
16 CherryPy/Buffet.
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
17 """
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
18
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
19 from genshi.input import ET, HTML, XML
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
20 from genshi.output import DocType
442
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 435
diff changeset
21 from genshi.template.base import Template
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
22 from genshi.template.loader import TemplateLoader
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
23 from genshi.template.markup import MarkupTemplate
592
7145e4eba2ec Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 545
diff changeset
24 from genshi.template.text import TextTemplate, NewTextTemplate
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
25
435
7800a61eae43 More API doc enhancements.
cmlenz
parents: 428
diff changeset
26 __all__ = ['ConfigurationError', 'AbstractTemplateEnginePlugin',
7800a61eae43 More API doc enhancements.
cmlenz
parents: 428
diff changeset
27 'MarkupTemplateEnginePlugin', 'TextTemplateEnginePlugin']
425
5b248708bbed Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents: 417
diff changeset
28 __docformat__ = 'restructuredtext en'
353
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents: 352
diff changeset
29
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
30
464
dafc6f4c20fb Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents: 448
diff changeset
31 class ConfigurationError(ValueError):
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
32 """Exception raised when invalid plugin options are encountered."""
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
33
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
34
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
35 class AbstractTemplateEnginePlugin(object):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
36 """Implementation of the plugin API."""
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
37
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
38 template_class = None
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
39 extension = None
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
40
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
41 def __init__(self, extra_vars_func=None, options=None):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
42 self.get_extra_vars = extra_vars_func
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
43 if options is None:
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
44 options = {}
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
45 self.options = options
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
46
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
47 self.default_encoding = options.get('genshi.default_encoding', 'utf-8')
353
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents: 352
diff changeset
48 auto_reload = options.get('genshi.auto_reload', '1')
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents: 352
diff changeset
49 if isinstance(auto_reload, basestring):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents: 352
diff changeset
50 auto_reload = auto_reload.lower() in ('1', 'on', 'yes', 'true')
417
0ea632cea849 filter empty values from the plugin search path
mgood
parents: 416
diff changeset
51 search_path = filter(None, options.get('genshi.search_path', '').split(':'))
416
41f2091e59e4 if a search path is provided to the template plugin use it instead of the package-style naming
mgood
parents: 409
diff changeset
52 self.use_package_naming = not search_path
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
53 try:
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
54 max_cache_size = int(options.get('genshi.max_cache_size', 25))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
55 except ValueError:
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
56 raise ConfigurationError('Invalid value for max_cache_size: "%s"' %
353
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents: 352
diff changeset
57 options.get('genshi.max_cache_size'))
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
58
538
19c7dc1e4dd5 Add `loader_callback` option to plugin interface as requested in #130.
cmlenz
parents: 464
diff changeset
59 loader_callback = options.get('genshi.loader_callback', None)
798
c690de5abafd Ported [914], [970], and [971] to 0.5.x branch.
cmlenz
parents: 772
diff changeset
60 if loader_callback and not hasattr(loader_callback, '__call__'):
538
19c7dc1e4dd5 Add `loader_callback` option to plugin interface as requested in #130.
cmlenz
parents: 464
diff changeset
61 raise ConfigurationError('loader callback must be a function')
19c7dc1e4dd5 Add `loader_callback` option to plugin interface as requested in #130.
cmlenz
parents: 464
diff changeset
62
606
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 592
diff changeset
63 lookup_errors = options.get('genshi.lookup_errors', 'strict')
442
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 435
diff changeset
64 if lookup_errors not in ('lenient', 'strict'):
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 435
diff changeset
65 raise ConfigurationError('Unknown lookup errors mode "%s"' %
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 435
diff changeset
66 lookup_errors)
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 435
diff changeset
67
545
6e21c89d9255 Support for Python code blocks in templates can now be disabled. Closes #123.
cmlenz
parents: 538
diff changeset
68 try:
6e21c89d9255 Support for Python code blocks in templates can now be disabled. Closes #123.
cmlenz
parents: 538
diff changeset
69 allow_exec = bool(options.get('genshi.allow_exec', True))
6e21c89d9255 Support for Python code blocks in templates can now be disabled. Closes #123.
cmlenz
parents: 538
diff changeset
70 except ValueError:
6e21c89d9255 Support for Python code blocks in templates can now be disabled. Closes #123.
cmlenz
parents: 538
diff changeset
71 raise ConfigurationError('Invalid value for allow_exec "%s"' %
6e21c89d9255 Support for Python code blocks in templates can now be disabled. Closes #123.
cmlenz
parents: 538
diff changeset
72 options.get('genshi.allow_exec'))
6e21c89d9255 Support for Python code blocks in templates can now be disabled. Closes #123.
cmlenz
parents: 538
diff changeset
73
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
74 self.loader = TemplateLoader(filter(None, search_path),
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
75 auto_reload=auto_reload,
375
5b859df8b184 Tiny simplification of the template engine plugin, taking advantage of the `default_class` parameter added to the `TemplateLoader` in [443].
cmlenz
parents: 354
diff changeset
76 max_cache_size=max_cache_size,
442
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 435
diff changeset
77 default_class=self.template_class,
538
19c7dc1e4dd5 Add `loader_callback` option to plugin interface as requested in #130.
cmlenz
parents: 464
diff changeset
78 variable_lookup=lookup_errors,
545
6e21c89d9255 Support for Python code blocks in templates can now be disabled. Closes #123.
cmlenz
parents: 538
diff changeset
79 allow_exec=allow_exec,
538
19c7dc1e4dd5 Add `loader_callback` option to plugin interface as requested in #130.
cmlenz
parents: 464
diff changeset
80 callback=loader_callback)
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
81
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
82 def load_template(self, templatename, template_string=None):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
83 """Find a template specified in python 'dot' notation, or load one from
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
84 a string.
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
85 """
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
86 if template_string is not None:
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
87 return self.template_class(template_string)
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
88
416
41f2091e59e4 if a search path is provided to the template plugin use it instead of the package-style naming
mgood
parents: 409
diff changeset
89 if self.use_package_naming:
41f2091e59e4 if a search path is provided to the template plugin use it instead of the package-style naming
mgood
parents: 409
diff changeset
90 divider = templatename.rfind('.')
41f2091e59e4 if a search path is provided to the template plugin use it instead of the package-style naming
mgood
parents: 409
diff changeset
91 if divider >= 0:
770
2867f00ec72e Ported [894] and [895] back to 0.5.x branch.
cmlenz
parents: 651
diff changeset
92 from pkg_resources import resource_filename
416
41f2091e59e4 if a search path is provided to the template plugin use it instead of the package-style naming
mgood
parents: 409
diff changeset
93 package = templatename[:divider]
41f2091e59e4 if a search path is provided to the template plugin use it instead of the package-style naming
mgood
parents: 409
diff changeset
94 basename = templatename[divider + 1:] + self.extension
41f2091e59e4 if a search path is provided to the template plugin use it instead of the package-style naming
mgood
parents: 409
diff changeset
95 templatename = resource_filename(package, basename)
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
96
375
5b859df8b184 Tiny simplification of the template engine plugin, taking advantage of the `default_class` parameter added to the `TemplateLoader` in [443].
cmlenz
parents: 354
diff changeset
97 return self.loader.load(templatename)
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
98
651
168dcc0b4782 The template engine plugin no longer adds the `default_doctype` when the `fragment` parameter is `True`. Thanks to dbrattli for the patch!
cmlenz
parents: 606
diff changeset
99 def _get_render_options(self, format=None, fragment=False):
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
100 if format is None:
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
101 format = self.default_format
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
102 kwargs = {'method': format}
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
103 if self.default_encoding:
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
104 kwargs['encoding'] = self.default_encoding
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
105 return kwargs
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
106
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
107 def render(self, info, format=None, fragment=False, template=None):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
108 """Render the template to a string using the provided info."""
651
168dcc0b4782 The template engine plugin no longer adds the `default_doctype` when the `fragment` parameter is `True`. Thanks to dbrattli for the patch!
cmlenz
parents: 606
diff changeset
109 kwargs = self._get_render_options(format=format, fragment=fragment)
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
110 return self.transform(info, template).render(**kwargs)
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
111
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
112 def transform(self, info, template):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
113 """Render the output to an event stream."""
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
114 if not isinstance(template, Template):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
115 template = self.load_template(template)
428
540dd825d072 Simplify undefined error message.
cmlenz
parents: 425
diff changeset
116 return template.generate(**info)
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
117
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
118
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
119 class MarkupTemplateEnginePlugin(AbstractTemplateEnginePlugin):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
120 """Implementation of the plugin API for markup templates."""
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
121
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
122 template_class = MarkupTemplate
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
123 extension = '.html'
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
124
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
125 def __init__(self, extra_vars_func=None, options=None):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
126 AbstractTemplateEnginePlugin.__init__(self, extra_vars_func, options)
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
127
464
dafc6f4c20fb Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents: 448
diff changeset
128 default_doctype = self.options.get('genshi.default_doctype')
dafc6f4c20fb Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents: 448
diff changeset
129 if default_doctype:
dafc6f4c20fb Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents: 448
diff changeset
130 doctype = DocType.get(default_doctype)
dafc6f4c20fb Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents: 448
diff changeset
131 if doctype is None:
dafc6f4c20fb Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents: 448
diff changeset
132 raise ConfigurationError('Unknown doctype %r' % default_doctype)
dafc6f4c20fb Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents: 448
diff changeset
133 self.default_doctype = doctype
dafc6f4c20fb Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents: 448
diff changeset
134 else:
dafc6f4c20fb Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents: 448
diff changeset
135 self.default_doctype = None
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
136
464
dafc6f4c20fb Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents: 448
diff changeset
137 format = self.options.get('genshi.default_format', 'html').lower()
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
138 if format not in ('html', 'xhtml', 'xml', 'text'):
464
dafc6f4c20fb Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents: 448
diff changeset
139 raise ConfigurationError('Unknown output format %r' % format)
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
140 self.default_format = format
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
141
651
168dcc0b4782 The template engine plugin no longer adds the `default_doctype` when the `fragment` parameter is `True`. Thanks to dbrattli for the patch!
cmlenz
parents: 606
diff changeset
142 def _get_render_options(self, format=None, fragment=False):
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
143 kwargs = super(MarkupTemplateEnginePlugin,
651
168dcc0b4782 The template engine plugin no longer adds the `default_doctype` when the `fragment` parameter is `True`. Thanks to dbrattli for the patch!
cmlenz
parents: 606
diff changeset
144 self)._get_render_options(format, fragment)
168dcc0b4782 The template engine plugin no longer adds the `default_doctype` when the `fragment` parameter is `True`. Thanks to dbrattli for the patch!
cmlenz
parents: 606
diff changeset
145 if self.default_doctype and not fragment:
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
146 kwargs['doctype'] = self.default_doctype
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
147 return kwargs
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
148
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
149 def transform(self, info, template):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
150 """Render the output to an event stream."""
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
151 data = {'ET': ET, 'HTML': HTML, 'XML': XML}
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
152 if self.get_extra_vars:
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
153 data.update(self.get_extra_vars())
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
154 data.update(info)
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
155 return super(MarkupTemplateEnginePlugin, self).transform(data, template)
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
156
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
157
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
158 class TextTemplateEnginePlugin(AbstractTemplateEnginePlugin):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
159 """Implementation of the plugin API for text templates."""
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
160
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
161 template_class = TextTemplate
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
162 extension = '.txt'
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
163 default_format = 'text'
592
7145e4eba2ec Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 545
diff changeset
164
7145e4eba2ec Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 545
diff changeset
165 def __init__(self, extra_vars_func=None, options=None):
7145e4eba2ec Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 545
diff changeset
166 if options is None:
7145e4eba2ec Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 545
diff changeset
167 options = {}
7145e4eba2ec Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 545
diff changeset
168
7145e4eba2ec Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 545
diff changeset
169 new_syntax = options.get('genshi.new_text_syntax')
7145e4eba2ec Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 545
diff changeset
170 if isinstance(new_syntax, basestring):
7145e4eba2ec Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 545
diff changeset
171 new_syntax = new_syntax.lower() in ('1', 'on', 'yes', 'true')
7145e4eba2ec Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 545
diff changeset
172 if new_syntax:
7145e4eba2ec Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 545
diff changeset
173 self.template_class = NewTextTemplate
7145e4eba2ec Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 545
diff changeset
174
7145e4eba2ec Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 545
diff changeset
175 AbstractTemplateEnginePlugin.__init__(self, extra_vars_func, options)
Copyright (C) 2012-2017 Edgewall Software