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