annotate markup/plugin.py @ 115:d4ea684655d9

Various fixes for breakage introduced in [132].
author cmlenz
date Tue, 01 Aug 2006 10:42:48 +0000
parents f1aa49c759b2
children 56d534eb53f9
rev   line source
4
f8612f05af99 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 -*-
f8612f05af99 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
822089ae65ce Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents: 29
diff changeset
3 # Copyright (C) 2006 Edgewall Software
21
eca77129518a * 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
f8612f05af99 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.
f8612f05af99 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 #
f8612f05af99 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
f8612f05af99 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
822089ae65ce 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
f8612f05af99 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 #
f8612f05af99 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
f8612f05af99 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
822089ae65ce 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
f8612f05af99 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
4b6cee37ce62 * 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
4b6cee37ce62 * 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.
4b6cee37ce62 * 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 """
4b6cee37ce62 * 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
f8612f05af99 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 import os
f8612f05af99 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 from pkg_resources import resource_filename
f8612f05af99 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
21
f8612f05af99 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
72
29f22f4fd583 add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents: 66
diff changeset
23 from markup.core import Stream, QName
4
f8612f05af99 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
24
72
29f22f4fd583 add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents: 66
diff changeset
25 def ET(element):
29f22f4fd583 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
29f22f4fd583 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('{'):
29f22f4fd583 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:]
29f22f4fd583 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)
29f22f4fd583 add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents: 66
diff changeset
30
29f22f4fd583 add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents: 66
diff changeset
31 yield (Stream.START, (tag_name, element.items()),
29f22f4fd583 add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents: 66
diff changeset
32 ('<string>', 0, 0))
29f22f4fd583 add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents: 66
diff changeset
33 if element.text:
29f22f4fd583 add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents: 66
diff changeset
34 yield Stream.TEXT, element.text, ('<string>', 0, 0)
29f22f4fd583 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():
29f22f4fd583 add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents: 66
diff changeset
36 for item in ET(child):
29f22f4fd583 add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents: 66
diff changeset
37 yield item
29f22f4fd583 add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents: 66
diff changeset
38 yield Stream.END, tag_name, ('<string>', 0, 0)
29f22f4fd583 add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents: 66
diff changeset
39 if element.tail:
29f22f4fd583 add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents: 66
diff changeset
40 yield Stream.TEXT, element.tail, ('<string>', 0, 0)
4
f8612f05af99 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
f1aa49c759b2 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 72
diff changeset
42
4
f8612f05af99 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
4b6cee37ce62 * 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
f8612f05af99 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
f8612f05af99 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):
f8612f05af99 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:
f8612f05af99 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 = {}
f8612f05af99 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
f8612f05af99 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
f8612f05af99 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)
f8612f05af99 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
f8612f05af99 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
f8612f05af99 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
f8612f05af99 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):
f8612f05af99 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."""
f8612f05af99 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('.')
f8612f05af99 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:
f8612f05af99 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]
f8612f05af99 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
eca77129518a * 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
f8612f05af99 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
f8612f05af99 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)
f8612f05af99 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
f8612f05af99 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
eca77129518a * 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
f8612f05af99 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)
f8612f05af99 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
f8612f05af99 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
eca77129518a * 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
f8612f05af99 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):
f8612f05af99 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)
f8612f05af99 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
72
29f22f4fd583 add a function `ET` in the template plugin including `ElementTree` elements in the output stream
mgood
parents: 66
diff changeset
74 data = {'ET': ET}
4
f8612f05af99 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:
f8612f05af99 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())
f8612f05af99 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)
f8612f05af99 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
f8612f05af99 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
79 ctxt = Context(**data)
f8612f05af99 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
80 return template.generate(ctxt)
Copyright (C) 2012-2017 Edgewall Software