annotate piecrust/templating/pystacheengine.py @ 369:4b1019bb2533

serve: Giant refactor to change how we handle data when serving pages. * We need a distinction between source metadata and route metadata. In most cases they're the same, but in cases like taxonomy pages, route metadata contains more things that can't be in source metadata if we want to re-use cached pages. * Create a new `QualifiedPage` type which is a page with a specific route and route metadata. Pass this around in many places. * Instead of passing an URL around, use the route in the `QualifiedPage` to generate URLs. This is better since it removes the guess-work from trying to generate URLs for sub-pages. * Deep-copy app and page configurations before passing them around to things that could modify them, like data builders and such. * Exclude taxonomy pages from iterator data providers. * Properly nest iterator data providers for when the theme and user page sources are merged inside `site.pages`.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 03 May 2015 18:47:10 -0700
parents 139179dc7abd
children f4b7c8f183a4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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