Mercurial > genshi > mirror
comparison genshi/template/plugin.py @ 355:94639584725a experimental-inline
inline branch: Merged [430:434/trunk].
author | cmlenz |
---|---|
date | Mon, 13 Nov 2006 18:16:57 +0000 |
parents | 7763f7aec949 |
children | a81675590258 |
comparison
equal
deleted
inserted
replaced
350:4a2050e9dcd8 | 355:94639584725a |
---|---|
16 CherryPy/Buffet. | 16 CherryPy/Buffet. |
17 """ | 17 """ |
18 | 18 |
19 from pkg_resources import resource_filename | 19 from pkg_resources import resource_filename |
20 | 20 |
21 from genshi.eval import Undefined | |
22 from genshi.input import ET, HTML, XML | 21 from genshi.input import ET, HTML, XML |
23 from genshi.output import DocType | 22 from genshi.output import DocType |
24 from genshi.template.core import Context, Template | 23 from genshi.template.core import Context, Template |
24 from genshi.template.eval import Undefined | |
25 from genshi.template.loader import TemplateLoader | 25 from genshi.template.loader import TemplateLoader |
26 from genshi.template.markup import MarkupTemplate | 26 from genshi.template.markup import MarkupTemplate |
27 from genshi.template.text import TextTemplate | 27 from genshi.template.text import TextTemplate |
28 | |
29 __all__ = ['ConfigurationError', 'MarkupTemplateEnginePlugin', | |
30 'TextTemplateEnginePlugin'] | |
28 | 31 |
29 | 32 |
30 class ConfigurationError(Exception): | 33 class ConfigurationError(Exception): |
31 """Exception raised when invalid plugin options are encountered.""" | 34 """Exception raised when invalid plugin options are encountered.""" |
32 | 35 |
42 if options is None: | 45 if options is None: |
43 options = {} | 46 options = {} |
44 self.options = options | 47 self.options = options |
45 | 48 |
46 self.default_encoding = options.get('genshi.default_encoding', 'utf-8') | 49 self.default_encoding = options.get('genshi.default_encoding', 'utf-8') |
47 auto_reload = options.get('genshi.auto_reload', '1').lower() \ | 50 auto_reload = options.get('genshi.auto_reload', '1') |
48 in ('1', 'yes', 'true') | 51 if isinstance(auto_reload, basestring): |
52 auto_reload = auto_reload.lower() in ('1', 'on', 'yes', 'true') | |
49 search_path = options.get('genshi.search_path', '').split(':') | 53 search_path = options.get('genshi.search_path', '').split(':') |
50 try: | 54 try: |
51 max_cache_size = int(options.get('genshi.max_cache_size', 25)) | 55 max_cache_size = int(options.get('genshi.max_cache_size', 25)) |
52 except ValueError: | 56 except ValueError: |
53 raise ConfigurationError('Invalid value for max_cache_size: "%s"' % | 57 raise ConfigurationError('Invalid value for max_cache_size: "%s"' % |
54 max_cache_size) | 58 options.get('genshi.max_cache_size')) |
55 | 59 |
56 self.loader = TemplateLoader(filter(None, search_path), | 60 self.loader = TemplateLoader(filter(None, search_path), |
57 auto_reload=auto_reload, | 61 auto_reload=auto_reload, |
58 max_cache_size=max_cache_size) | 62 max_cache_size=max_cache_size) |
59 | 63 |
114 'xhtml-transitional': DocType.XHTML_TRANSITIONAL} | 118 'xhtml-transitional': DocType.XHTML_TRANSITIONAL} |
115 | 119 |
116 def __init__(self, extra_vars_func=None, options=None): | 120 def __init__(self, extra_vars_func=None, options=None): |
117 AbstractTemplateEnginePlugin.__init__(self, extra_vars_func, options) | 121 AbstractTemplateEnginePlugin.__init__(self, extra_vars_func, options) |
118 | 122 |
119 doctype = options.get('genshi.default_doctype') | 123 doctype = self.options.get('genshi.default_doctype') |
120 if doctype and doctype not in self.doctypes: | 124 if doctype and doctype not in self.doctypes: |
121 raise ConfigurationError('Unknown doctype "%s"' % doctype) | 125 raise ConfigurationError('Unknown doctype "%s"' % doctype) |
122 self.default_doctype = self.doctypes.get(doctype) | 126 self.default_doctype = self.doctypes.get(doctype) |
123 | 127 |
124 format = options.get('genshi.default_format', 'html') | 128 format = self.options.get('genshi.default_format', 'html') |
125 if format not in ('html', 'xhtml', 'xml', 'text'): | 129 if format not in ('html', 'xhtml', 'xml', 'text'): |
126 raise ConfigurationError('Unknown output format "%s"' % format) | 130 raise ConfigurationError('Unknown output format "%s"' % format) |
127 self.default_format = format | 131 self.default_format = format |
128 | 132 |
129 def _get_render_options(self, format=None): | 133 def _get_render_options(self, format=None): |
146 """Implementation of the plugin API for text templates.""" | 150 """Implementation of the plugin API for text templates.""" |
147 | 151 |
148 template_class = TextTemplate | 152 template_class = TextTemplate |
149 extension = '.txt' | 153 extension = '.txt' |
150 default_format = 'text' | 154 default_format = 'text' |
151 | |
152 def transform(self, info, template): | |
153 """Render the output to an event stream.""" | |
154 data = {} | |
155 if self.get_extra_vars: | |
156 data.update(self.get_extra_vars()) | |
157 data.update(info) | |
158 return super(TextTemplateEnginePlugin, self).transform(data, template) |