Mercurial > genshi > mirror
comparison genshi/plugin.py @ 291:b6684c946a53 trunk
Add support for a range of options to the template engine plugin. See [http://groups.google.com/group/genshi/browse_thread/thread/8727f16edfce4727#3ee98255f84e4dcf this mailing list post].
author | cmlenz |
---|---|
date | Fri, 13 Oct 2006 09:17:57 +0000 |
parents | 94f9f2cc66c8 |
children | 5b1d5460fe25 |
comparison
equal
deleted
inserted
replaced
290:94f9f2cc66c8 | 291:b6684c946a53 |
---|---|
18 | 18 |
19 from pkg_resources import resource_filename | 19 from pkg_resources import resource_filename |
20 | 20 |
21 from genshi.eval import Undefined | 21 from genshi.eval import Undefined |
22 from genshi.input import ET, HTML, XML | 22 from genshi.input import ET, HTML, XML |
23 from genshi.output import DocType | |
23 from genshi.template import Context, MarkupTemplate, Template, TemplateLoader, \ | 24 from genshi.template import Context, MarkupTemplate, Template, TemplateLoader, \ |
24 TextTemplate | 25 TextTemplate |
26 | |
27 | |
28 class ConfigurationError(Exception): | |
29 """Exception raised when invalid plugin options are encountered.""" | |
25 | 30 |
26 | 31 |
27 class AbstractTemplateEnginePlugin(object): | 32 class AbstractTemplateEnginePlugin(object): |
28 """Implementation of the plugin API.""" | 33 """Implementation of the plugin API.""" |
29 | 34 |
30 template_class = None | 35 template_class = None |
31 extension = None | 36 extension = None |
32 | 37 |
33 def __init__(self, extra_vars_func=None, options=None): | 38 def __init__(self, extra_vars_func=None, options=None): |
39 self.get_extra_vars = extra_vars_func | |
34 if options is None: | 40 if options is None: |
35 options = {} | 41 options = {} |
36 # TODO get loader_args from the options dict | 42 self.options = options |
37 | 43 |
38 self.loader = TemplateLoader(auto_reload=True) | 44 self.default_encoding = options.get('genshi.default_encoding', 'utf-8') |
39 self.options = options | 45 auto_reload = options.get('genshi.auto_reload', '').lower() \ |
40 self.get_extra_vars = extra_vars_func | 46 in ('1', 'yes', 'true') |
47 search_path = options.get('genshi.search_path', '').split(':') | |
48 try: | |
49 max_cache_size = int(options.get('genshi.max_cache_size', 25)) | |
50 except ValueError: | |
51 raise ConfigurationError('Invalid value for max_cache_size: "%s"' % | |
52 max_cache_size) | |
53 | |
54 self.loader = TemplateLoader(search_path, auto_reload=auto_reload, | |
55 max_cache_size=max_cache_size) | |
41 | 56 |
42 def load_template(self, templatename, template_string=None): | 57 def load_template(self, templatename, template_string=None): |
43 """Find a template specified in python 'dot' notation, or load one from | 58 """Find a template specified in python 'dot' notation, or load one from |
44 a string. | 59 a string. |
45 """ | 60 """ |
52 basename = templatename[divider + 1:] + self.extension | 67 basename = templatename[divider + 1:] + self.extension |
53 templatename = resource_filename(package, basename) | 68 templatename = resource_filename(package, basename) |
54 | 69 |
55 return self.loader.load(templatename, cls=self.template_class) | 70 return self.loader.load(templatename, cls=self.template_class) |
56 | 71 |
57 def render(self, info, format='html', fragment=False, template=None): | 72 def _get_render_options(self, format=None): |
73 if format is None: | |
74 format = self.default_format | |
75 kwargs = {'method': format} | |
76 if self.default_encoding: | |
77 kwargs['encoding'] = self.default_encoding | |
78 return kwargs | |
79 | |
80 def render(self, info, format=None, fragment=False, template=None): | |
58 """Render the template to a string using the provided info.""" | 81 """Render the template to a string using the provided info.""" |
59 return self.transform(info, template).render(method=format) | 82 kwargs = self._get_render_options() |
83 return self.transform(info, template).render(**kwargs) | |
60 | 84 |
61 def transform(self, info, template): | 85 def transform(self, info, template): |
62 """Render the output to an event stream.""" | 86 """Render the output to an event stream.""" |
63 if not isinstance(template, Template): | 87 if not isinstance(template, Template): |
64 template = self.load_template(template) | 88 template = self.load_template(template) |
79 """Implementation of the plugin API for markup templates.""" | 103 """Implementation of the plugin API for markup templates.""" |
80 | 104 |
81 template_class = MarkupTemplate | 105 template_class = MarkupTemplate |
82 extension = '.html' | 106 extension = '.html' |
83 | 107 |
108 doctypes = {'html': DocType.HTML, 'html-strict': DocType.HTML_STRICT, | |
109 'html-transitional': DocType.HTML_TRANSITIONAL, | |
110 'xhtml': DocType.XHTML, 'xhtml-strict': DocType.XHTML_STRICT, | |
111 'xhtml-transitional': DocType.XHTML_TRANSITIONAL} | |
112 | |
113 def __init__(self, extra_vars_func=None, options=None): | |
114 AbstractTemplateEnginePlugin.__init__(self, extra_vars_func, options) | |
115 | |
116 doctype = options.get('genshi.default_doctype') | |
117 if doctype and doctype not in self.doctypes: | |
118 raise ConfigurationError('Unknown doctype "%s"' % doctype) | |
119 self.default_doctype = self.doctypes.get(doctype) | |
120 | |
121 format = options.get('genshi.default_format', 'html') | |
122 if format not in ('html', 'xhtml', 'xml', 'text'): | |
123 raise ConfigurationError('Unknown output format "%s"' % format) | |
124 self.default_format = format | |
125 | |
126 def _get_render_options(self, format=None): | |
127 kwargs = super(MarkupTemplateEnginePlugin, | |
128 self)._get_render_options(format) | |
129 if self.default_doctype: | |
130 kwargs['doctype'] = self.default_doctype | |
131 return kwargs | |
132 | |
84 def transform(self, info, template): | 133 def transform(self, info, template): |
85 """Render the output to an event stream.""" | 134 """Render the output to an event stream.""" |
86 data = {'ET': ET, 'HTML': HTML, 'XML': XML} | 135 data = {'ET': ET, 'HTML': HTML, 'XML': XML} |
87 if self.get_extra_vars: | 136 if self.get_extra_vars: |
88 data.update(self.get_extra_vars()) | 137 data.update(self.get_extra_vars()) |
93 class TextTemplateEnginePlugin(AbstractTemplateEnginePlugin): | 142 class TextTemplateEnginePlugin(AbstractTemplateEnginePlugin): |
94 """Implementation of the plugin API for text templates.""" | 143 """Implementation of the plugin API for text templates.""" |
95 | 144 |
96 template_class = TextTemplate | 145 template_class = TextTemplate |
97 extension = '.txt' | 146 extension = '.txt' |
147 default_format = 'text' | |
98 | 148 |
99 def transform(self, info, template): | 149 def transform(self, info, template): |
100 """Render the output to an event stream.""" | 150 """Render the output to an event stream.""" |
101 data = {} | 151 data = {} |
102 if self.get_extra_vars: | 152 if self.get_extra_vars: |