Mercurial > code > home > repos > href
comparison jadestache.py @ 2:80b11112c9e0
web app for query urls like /user and /user/tag+tag
Ignore-this: bb1f40dd6bbff4c3ee463b440738726d
author | Drew Perttula <drewp@bigasterisk.com> |
---|---|
date | Sun, 17 Feb 2013 03:56:28 -0800 |
parents | |
children | 7c82ffbca5d0 |
comparison
equal
deleted
inserted
replaced
1:7cecda055fae | 2:80b11112c9e0 |
---|---|
1 import pyjade, pyjade.exceptions, pystache | |
2 | |
3 | |
4 class _JadeLoader(pystache.loader.Loader): | |
5 """ | |
6 expands jade of incoming files. Also includes a cache so it doesn't | |
7 read the same file twice | |
8 """ | |
9 def __init__(self, *args, **kw): | |
10 pystache.renderer.Loader.__init__(self, *args, **kw) | |
11 self.seen = {} # path : expanded jade | |
12 | |
13 def read(self, path, encoding=None): | |
14 if path in self.seen: | |
15 return self.seen[path] | |
16 | |
17 b = pystache.common.read(path) | |
18 | |
19 if encoding is None: | |
20 encoding = self.file_encoding | |
21 | |
22 src = self.unicode(b, encoding) | |
23 | |
24 expanded = pyjade.utils.process(src) | |
25 self.seen[path] = expanded | |
26 return expanded | |
27 | |
28 class Renderer(pystache.renderer.Renderer): | |
29 """ | |
30 pystache renderer that expands base jade syntax on its input | |
31 files. No jade data interpolation happens, so you could use these | |
32 same templates in the browser with js mustache. | |
33 | |
34 Files need to end with .mustache since it's the mustache loader | |
35 that's going to look for them. | |
36 """ | |
37 def __init__(self, *args, **kw): | |
38 debug = False | |
39 if 'debug' in kw: | |
40 debug = kw['debug'] | |
41 del kw['debug'] | |
42 pystache.renderer.Renderer.__init__(self, *args, **kw) | |
43 self._loader = None if debug else self._new_loader() | |
44 | |
45 def _new_loader(self): | |
46 return _JadeLoader( | |
47 file_encoding=self.file_encoding, extension=self.file_extension, | |
48 to_unicode=self.unicode, search_dirs=self.search_dirs) | |
49 | |
50 def _make_loader(self): | |
51 if self._loader is not None: | |
52 return self._loader | |
53 else: | |
54 # this is for debug mode, to make the templates get reread | |
55 return self._new_loader() |