annotate genshi/template/plugin.py @ 935:705727288d7e

Merge r1143 from py3k: add support for python 3 to remaining genshi.template components: * minor changes to track encoding=None API change in core genshi modules. * genshi/template/directives: * slightly odd syntax changes to make the 2to3 .next() fixer pick up *stream.next() * minor test fix for change in behaviour of division (/) in Python 3. * genshi/template/loader: * add 'b' to file modes to ensure it's loaded as bytes in Python 3. * use not isinstance(s, unicode) instead of isinstance(s, str) since the former is correctly converted by 2to3.
author hodgestar
date Fri, 18 Mar 2011 09:17:52 +0000
parents fbe34d12acde
children
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 #
854
0d9e87c6cf6e More work on reducing the size of the diff produced by 2to3.
cmlenz
parents: 793
diff changeset
3 # Copyright (C) 2006-2009 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
935
705727288d7e Merge r1143 from py3k:
hodgestar
parents: 859
diff changeset
47 self.default_encoding = options.get('genshi.default_encoding', None)
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')
854
0d9e87c6cf6e More work on reducing the size of the diff produced by 2to3.
cmlenz
parents: 793
diff changeset
51 search_path = [p for p in
0d9e87c6cf6e More work on reducing the size of the diff produced by 2to3.
cmlenz
parents: 793
diff changeset
52 options.get('genshi.search_path', '').split(':') if p]
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
53 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
54 try:
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
55 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
56 except ValueError:
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
57 raise ConfigurationError('Invalid value for max_cache_size: "%s"' %
353
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents: 352
diff changeset
58 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
59
538
19c7dc1e4dd5 Add `loader_callback` option to plugin interface as requested in #130.
cmlenz
parents: 464
diff changeset
60 loader_callback = options.get('genshi.loader_callback', None)
793
7cf2407671c2 Get rid of a couple more -3 warnings.
cmlenz
parents: 771
diff changeset
61 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
62 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
63
606
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 592
diff changeset
64 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
65 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
66 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
67 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
68
545
6e21c89d9255 Support for Python code blocks in templates can now be disabled. Closes #123.
cmlenz
parents: 538
diff changeset
69 try:
6e21c89d9255 Support for Python code blocks in templates can now be disabled. Closes #123.
cmlenz
parents: 538
diff changeset
70 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
71 except ValueError:
6e21c89d9255 Support for Python code blocks in templates can now be disabled. Closes #123.
cmlenz
parents: 538
diff changeset
72 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
73 options.get('genshi.allow_exec'))
6e21c89d9255 Support for Python code blocks in templates can now be disabled. Closes #123.
cmlenz
parents: 538
diff changeset
74
859
fbe34d12acde More bits of 2to3 related cleanup.
cmlenz
parents: 854
diff changeset
75 self.loader = TemplateLoader([p for p in search_path if p],
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
76 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
77 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
78 default_class=self.template_class,
538
19c7dc1e4dd5 Add `loader_callback` option to plugin interface as requested in #130.
cmlenz
parents: 464
diff changeset
79 variable_lookup=lookup_errors,
545
6e21c89d9255 Support for Python code blocks in templates can now be disabled. Closes #123.
cmlenz
parents: 538
diff changeset
80 allow_exec=allow_exec,
538
19c7dc1e4dd5 Add `loader_callback` option to plugin interface as requested in #130.
cmlenz
parents: 464
diff changeset
81 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
82
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
83 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
84 """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
85 a string.
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
86 """
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
87 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
88 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
89
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
90 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
91 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
92 if divider >= 0:
769
834e8fd2d822 Includes from templates loaded via an absolute path now include the correct file in nested directories as long if no search path has been configured. Closes #240.
cmlenz
parents: 651
diff changeset
93 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
94 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
95 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
96 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
97
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
98 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
99
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
100 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
101 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
102 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
103 kwargs = {'method': format}
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
104 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
105 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
106 return kwargs
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
107
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
108 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
109 """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
110 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
111 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
112
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
113 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
114 """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
115 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
116 template = self.load_template(template)
428
540dd825d072 Simplify undefined error message.
cmlenz
parents: 425
diff changeset
117 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
118
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
119
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
120 class MarkupTemplateEnginePlugin(AbstractTemplateEnginePlugin):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
121 """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
122
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
123 template_class = MarkupTemplate
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
124 extension = '.html'
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
125
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
126 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
127 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
128
464
dafc6f4c20fb Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents: 448
diff changeset
129 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
130 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
131 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
132 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
133 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
134 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
135 else:
dafc6f4c20fb Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents: 448
diff changeset
136 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
137
464
dafc6f4c20fb Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents: 448
diff changeset
138 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
139 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
140 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
141 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
142
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
143 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
144 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
145 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
146 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
147 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
148 return kwargs
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
149
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
150 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
151 """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
152 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
153 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
154 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
155 data.update(info)
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
156 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
157
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
158
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
159 class TextTemplateEnginePlugin(AbstractTemplateEnginePlugin):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
160 """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
161
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
162 template_class = TextTemplate
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
163 extension = '.txt'
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
164 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
165
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 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
167 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
168 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
169
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 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
171 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
172 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
173 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
174 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
175
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
176 AbstractTemplateEnginePlugin.__init__(self, extra_vars_func, options)
Copyright (C) 2012-2017 Edgewall Software