Skip to content

Commit

Permalink
Fixed mako and genshi problems in new templating system found by test…
Browse files Browse the repository at this point in the history
…ing against tw2.devtools.
  • Loading branch information
ralphbean committed May 25, 2012
1 parent 9fc8820 commit 41b8e52
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions tw2/core/templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def get_engine_name(template_name, mw=None):

@memoize
def _get_dotted_filename(engine_name, template):
template = _strip_engine_name(template)
location, filename = template.rsplit('.', 1)
module = __import__(location, globals(), locals(), ['*'])
parent_dir = SEP.join(module.__file__.split(SEP)[:-1])
Expand All @@ -75,18 +76,21 @@ def _get_dotted_filename(engine_name, template):

raise IOError("Couldn't find source for %r" % template)

def _strip_engine_name(template):
""" Strip off the leading engine name from the template if it exists. """
if any(map(template.lstrip().startswith, rendering_extension_lookup)):
return template.split(':', 1)[1]

return template


@memoize
def get_source(engine_name, template, inline=False):
if inline:
return template

# Strip off the leading engine name from the template if it exists
if any(map(template.lstrip().startswith, rendering_extension_lookup)):
template = template.split(':', 1)[1]

if SEP in template:
filename = template
filename = _strip_engine_name(template)
else:
filename = _get_dotted_filename(engine_name, template)

Expand All @@ -109,12 +113,28 @@ def get_render_callable(engine_name, displays_on, src, filename=None):

if engine_name == 'mako':
import mako.template
tmpl = mako.template.Template(text=src, filename=filename)
args = dict(
text=src,
filename=filename,
)

if filename:
from mako.lookup import TemplateLookup

if SEP not in filename:
filename = _get_dotted_filename(engine_name, filename)

directory = os.path.sep.join(filename.split(os.path.sep)[:-1])
args['lookup'] = TemplateLookup(directories=[directory])

tmpl = mako.template.Template(**args)
return lambda kwargs: literal(tmpl.render(**kwargs))
elif engine_name in ('genshi', 'genshi_abs'):
import genshi.template
tmpl = genshi.template.MarkupTemplate(src)
return lambda kwargs: literal(tmpl.generate(**kwargs))
return lambda kwargs: literal(
''.join(tmpl.generate(**kwargs).serialize('xhtml'))
)
elif engine_name == 'jinja':
import jinja2
tmpl = jinja2.Template(src)
Expand Down

0 comments on commit 41b8e52

Please sign in to comment.