annotate markup/plugin.py @ 161:7b1f07496bf7 trunk

Various docstring additions and other cosmetic changes.
author cmlenz
date Wed, 16 Aug 2006 22:32:31 +0000
parents fb71efbd6ad4
children 2f30ce3fb85e
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
161
7b1f07496bf7 Various docstring additions and other cosmetic changes.
cmlenz
parents: 151
diff changeset
21 from markup.core import Attributes, 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
161
7b1f07496bf7 Various docstring additions and other cosmetic changes.
cmlenz
parents: 151
diff changeset
24 def et_to_stream(element):
7b1f07496bf7 Various docstring additions and other cosmetic changes.
cmlenz
parents: 151
diff changeset
25 """Converts the given ElementTree element to a markup stream."""
72
ee092ccb3af1 add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents: 66
diff changeset
26 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
27 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
28 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
29 tag_name = QName(tag_name)
161
7b1f07496bf7 Various docstring additions and other cosmetic changes.
cmlenz
parents: 151
diff changeset
30 attrib = Attributes(element.items())
72
ee092ccb3af1 add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents: 66
diff changeset
31
161
7b1f07496bf7 Various docstring additions and other cosmetic changes.
cmlenz
parents: 151
diff changeset
32 yield (Stream.START, (tag_name, attrib), (None, -1, -1))
72
ee092ccb3af1 add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents: 66
diff changeset
33 if element.text:
151
fb71efbd6ad4 The convention for an unknown position is `(None, -1, -1)`.
cmlenz
parents: 149
diff changeset
34 yield Stream.TEXT, element.text, (None, -1, -1)
72
ee092ccb3af1 add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents: 66
diff changeset
35 for child in element.getchildren():
161
7b1f07496bf7 Various docstring additions and other cosmetic changes.
cmlenz
parents: 151
diff changeset
36 for item in et_to_stream(child):
72
ee092ccb3af1 add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents: 66
diff changeset
37 yield item
151
fb71efbd6ad4 The convention for an unknown position is `(None, -1, -1)`.
cmlenz
parents: 149
diff changeset
38 yield Stream.END, tag_name, (None, -1, -1)
72
ee092ccb3af1 add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents: 66
diff changeset
39 if element.tail:
151
fb71efbd6ad4 The convention for an unknown position is `(None, -1, -1)`.
cmlenz
parents: 149
diff changeset
40 yield Stream.TEXT, element.tail, (None, -1, -1)
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
41
77
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 72
diff changeset
42
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
43 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
44 """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
45
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 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
47 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
48 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
49 # 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
50
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.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
52 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
53 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
54
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 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
56 """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
57 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
58 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
59 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
60 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
61 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
62
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 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
64
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
65 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
66 """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
67 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
68
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
69 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
70 """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
71 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
72 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
73
161
7b1f07496bf7 Various docstring additions and other cosmetic changes.
cmlenz
parents: 151
diff changeset
74 data = {'ET': et_to_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
75 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
76 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
77 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
78
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
79 return template.generate(**data)
Copyright (C) 2012-2017 Edgewall Software