|
34 | 34 | from __future__ import print_function |
35 | 35 | import jinja2 |
36 | 36 | import gettext |
| 37 | +import mimetypes |
37 | 38 | import sys |
38 | 39 |
|
39 | | -from types import MethodType |
40 | | - |
41 | 40 | # http://jinja.pocoo.org/docs/api/#loaders |
42 | 41 |
|
43 | 42 | from roundup.cgi.templating import context, LoaderBase, TemplateBase |
44 | 43 | from roundup.anypy.strings import s2u |
45 | 44 |
|
46 | 45 | class Jinja2Loader(LoaderBase): |
47 | 46 | def __init__(self, dir): |
48 | | - extensions = [ |
49 | | - 'jinja2.ext.autoescape', |
50 | | - ] |
51 | | - print("Jinja2 templates: ", dir) |
52 | | - print("Extensions: ", extensions) |
53 | 47 | self._env = jinja2.Environment( |
54 | | - loader=jinja2.FileSystemLoader(dir), |
55 | | - extensions=extensions |
56 | | - ) |
| 48 | + loader=jinja2.FileSystemLoader(dir), |
| 49 | + extensions=[] |
| 50 | + ) |
57 | 51 |
|
58 | 52 | # Adding a custom filter that can transform roundup's vars to unicode |
59 | 53 | # This is necessary because jinja2 can only deal with unicode objects |
60 | 54 | # and roundup uses utf-8 for the internal representation. |
61 | 55 | # The automatic conversion will assume 'ascii' and fail sometime. |
62 | 56 | # Analysed with roundup 1.5.0 and jinja 2.7.1. See issue2550811. |
63 | | - self._env.filters["u"] = lambda s: \ |
64 | | - s2u(s()) if type(s) == MethodType else s2u(s) |
| 57 | + self._env.filters["u"] = s2u |
| 58 | + |
| 59 | + def _find(self, tplname): |
| 60 | + for extension in ('', '.html', '.xml'): |
| 61 | + try: |
| 62 | + filename = tplname + extension |
| 63 | + return self._env.get_template(filename) |
| 64 | + except jinja2.TemplateNotFound: |
| 65 | + continue |
| 66 | + |
| 67 | + return None |
65 | 68 |
|
66 | 69 | def check(self, tplname): |
67 | | - #print tplname |
68 | | - try: |
69 | | - #print self._env.get_template(tplname + '.html') |
70 | | - self._env.get_template(tplname + '.html') |
71 | | - except jinja2.TemplateNotFound: |
72 | | - return |
73 | | - else: |
74 | | - return True |
| 70 | + return bool(self._find(tplname)) |
75 | 71 |
|
76 | 72 | def load(self, tplname): |
77 | | - #src, filename = self.check(tplname) |
78 | | - return Jinja2ProxyPageTemplate(self._env.get_template(tplname + '.html')) |
| 73 | + tpl = self._find(tplname) |
| 74 | + pt = Jinja2ProxyPageTemplate(tpl) |
| 75 | + pt.content_type = mimetypes.guess_type(tpl.filename)[0] or 'text/html' |
| 76 | + return pt |
79 | 77 |
|
80 | 78 | def precompile(self): |
81 | 79 | pass |
|
0 commit comments