Mercurial > piecrust2
annotate piecrust/templating/pystacheengine.py @ 196:154b8df04829
processing: Add Compass and Sass processors.
The Sass processor is similar to the Less processor, i.e. it tries to be
part of the structured pipeline processing by using the mapfile produced by
the Sass compiler in order to provide a list of dependencies.
The Compass processor is completely acting outside of the pipeline, so the
server won't know what's up to date and what's not. It's expected that the
user will run `compass watch` to keep things up to date. However, it will
require to pass the server's cache directory to put things in, so we'll need
to add some easy way to get that path for the user.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 11 Jan 2015 23:08:49 -0800 |
parents | 139179dc7abd |
children | f4b7c8f183a4 |
rev | line source |
---|---|
185
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 import logging |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 import pystache |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 from piecrust.templating.base import ( |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 TemplateEngine, TemplateNotFoundError, TemplatingError) |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 logger = logging.getLogger(__name__) |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 class PystacheTemplateEngine(TemplateEngine): |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 ENGINE_NAMES = ['mustache'] |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 EXTENSIONS = ['mustache'] |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 def __init__(self): |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 self.renderer = None |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 def renderString(self, txt, data, filename=None): |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
18 self._ensureLoaded() |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 try: |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 return self.renderer.render(txt, data) |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 except pystache.TemplateNotFoundError as ex: |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 raise TemplateNotFoundError() from ex |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 except pystache.PystacheError as ex: |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 raise TemplatingError(str(ex), filename) from ex |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
25 |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 def renderFile(self, paths, data): |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
27 self._ensureLoaded() |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 tpl = None |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
29 logger.debug("Looking for template: %s" % paths) |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
30 for p in paths: |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
31 if not p.endswith('.mustache'): |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
32 raise TemplatingError( |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
33 "The Mustache template engine only accepts template " |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 "filenames with a `.mustache` extension. Got: %s" % |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 p) |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 name = p[:-9] # strip `.mustache` |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
37 try: |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
38 tpl = self.renderer.load_template(name) |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 except Exception as ex: |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
40 print(p, ex) |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
41 pass |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
42 |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 if tpl is None: |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 raise TemplateNotFoundError() |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 try: |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 return self.renderer.render(tpl, data) |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 except pystache.PystacheError as ex: |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 raise TemplatingError(str(ex)) from ex |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
51 def _ensureLoaded(self): |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
52 if self.renderer: |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
53 return |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
54 |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
55 self.renderer = pystache.Renderer( |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
56 search_dirs=self.app.templates_dirs) |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
57 |