Mercurial > genshi > mirror
annotate markup/plugin.py @ 150:d35688d16831 trunk
Removed to many classes from the `__all__` list of `markup.template` in [191].
author | cmlenz |
---|---|
date | Tue, 15 Aug 2006 22:01:01 +0000 |
parents | 537f819c547b |
children | fb71efbd6ad4 |
rev | line source |
---|---|
4
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
2 # |
66
59eb24184e9c
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
29
diff
changeset
|
3 # Copyright (C) 2006 Edgewall Software |
21
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
4
diff
changeset
|
4 # Copyright (C) 2006 Matthew Good |
4
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
5 # All rights reserved. |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
6 # |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
7 # This software is licensed as described in the file COPYING, which |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
8 # you should have received as part of this distribution. The terms |
66
59eb24184e9c
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
29
diff
changeset
|
9 # are also available at http://markup.edgewall.org/wiki/License. |
4
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
10 # |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
11 # This software consists of voluntary contributions made by many |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
12 # individuals. For the exact contribution history, see the revision |
66
59eb24184e9c
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
29
diff
changeset
|
13 # history and logs, available at http://markup.edgewall.org/log/. |
4
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
14 |
29
ab8703fa68b8
* Minor simplification of template directives: they no longer get passed the template instance and the position, as no directive was actually using
cmlenz
parents:
27
diff
changeset
|
15 """Basic support for the template engine plugin API used by TurboGears and |
ab8703fa68b8
* Minor simplification of template directives: they no longer get passed the template instance and the position, as no directive was actually using
cmlenz
parents:
27
diff
changeset
|
16 CherryPy/Buffet. |
ab8703fa68b8
* Minor simplification of template directives: they no longer get passed the template instance and the position, as no directive was actually using
cmlenz
parents:
27
diff
changeset
|
17 """ |
ab8703fa68b8
* Minor simplification of template directives: they no longer get passed the template instance and the position, as no directive was actually using
cmlenz
parents:
27
diff
changeset
|
18 |
4
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
19 from pkg_resources import resource_filename |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
20 |
145
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
77
diff
changeset
|
21 from markup import Stream, QName |
4
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
22 from markup.template import Context, Template, TemplateLoader |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
23 |
72
ee092ccb3af1
add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents:
66
diff
changeset
|
24 def ET(element): |
ee092ccb3af1
add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents:
66
diff
changeset
|
25 tag_name = element.tag |
ee092ccb3af1
add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents:
66
diff
changeset
|
26 if tag_name.startswith('{'): |
ee092ccb3af1
add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents:
66
diff
changeset
|
27 tag_name = tag_name[1:] |
ee092ccb3af1
add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents:
66
diff
changeset
|
28 tag_name = QName(tag_name) |
ee092ccb3af1
add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents:
66
diff
changeset
|
29 |
ee092ccb3af1
add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents:
66
diff
changeset
|
30 yield (Stream.START, (tag_name, element.items()), |
ee092ccb3af1
add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents:
66
diff
changeset
|
31 ('<string>', 0, 0)) |
ee092ccb3af1
add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents:
66
diff
changeset
|
32 if element.text: |
ee092ccb3af1
add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents:
66
diff
changeset
|
33 yield Stream.TEXT, element.text, ('<string>', 0, 0) |
ee092ccb3af1
add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents:
66
diff
changeset
|
34 for child in element.getchildren(): |
ee092ccb3af1
add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents:
66
diff
changeset
|
35 for item in ET(child): |
ee092ccb3af1
add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents:
66
diff
changeset
|
36 yield item |
ee092ccb3af1
add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents:
66
diff
changeset
|
37 yield Stream.END, tag_name, ('<string>', 0, 0) |
ee092ccb3af1
add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents:
66
diff
changeset
|
38 if element.tail: |
ee092ccb3af1
add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents:
66
diff
changeset
|
39 yield Stream.TEXT, element.tail, ('<string>', 0, 0) |
4
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
40 |
77
f5ec6d4a61e4
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
72
diff
changeset
|
41 |
4
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
42 class TemplateEnginePlugin(object): |
29
ab8703fa68b8
* Minor simplification of template directives: they no longer get passed the template instance and the position, as no directive was actually using
cmlenz
parents:
27
diff
changeset
|
43 """Implementation of the plugin API.""" |
4
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
44 |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
45 def __init__(self, extra_vars_func=None, options=None): |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
46 if options is None: |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
47 options = {} |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
48 # TODO get loader_args from the options dict |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
49 |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
50 self.loader = TemplateLoader(auto_reload=True) |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
51 self.options = options |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
52 self.get_extra_vars = extra_vars_func |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
53 |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
54 def load_template(self, templatename): |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
55 """Find a template specified in python 'dot' notation.""" |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
56 divider = templatename.rfind('.') |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
57 if divider >= 0: |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
58 package = templatename[:divider] |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
59 basename = templatename[divider + 1:] + '.html' |
21
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
4
diff
changeset
|
60 templatename = resource_filename(package, basename) |
4
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
61 |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
62 return self.loader.load(templatename) |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
63 |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
64 def render(self, info, format='html', fragment=False, template=None): |
21
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
4
diff
changeset
|
65 """Render the template to a string using the provided info.""" |
4
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
66 return self.transform(info, template).render(method=format) |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
67 |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
68 def transform(self, info, template): |
21
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
4
diff
changeset
|
69 """Render the output to an event stream.""" |
4
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
70 if not isinstance(template, Template): |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
71 template = self.load_template(template) |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
72 |
72
ee092ccb3af1
add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents:
66
diff
changeset
|
73 data = {'ET': ET} |
4
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
74 if self.get_extra_vars: |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
75 data.update(self.get_extra_vars()) |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
76 data.update(info) |
49364e784c47
Added first stab of an implementation of the !TurboGears [http://www.turbogears.org/docs/plugins/template.html plugin API for template engines], and also a !TurboGears-based example using this plugin. Both written by Matt Good.
cmlenz
parents:
diff
changeset
|
77 |
149
537f819c547b
`Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents:
145
diff
changeset
|
78 return template.generate(**data) |