Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'mako-lookup'

  • Loading branch information...
commit 4fe7c9b11a608fe7e8af6a542974ee9545519ba1 2 parents 943463b + a367b95
@posativ authored
View
2  acrylamid/helpers.py
@@ -244,6 +244,8 @@ def link(title, href=None, entry=None):
'title': title,
'href': title if href is None else href,
'__str__' if PY3 else '__unicode__': lambda cls: cls.href,
+ '__add__': lambda self, other: unicode(self) + other,
+ '__radd__': lambda self, other: other + unicode(self)
})()
View
79 acrylamid/templates/mako.py
@@ -5,16 +5,84 @@
from __future__ import absolute_import
-import os, stat
+import os
+import io
+import re
+import posixpath
+
+from os.path import getmtime, isfile, basename
from acrylamid.templates import AbstractEnvironment, AbstractTemplate
from mako.lookup import TemplateLookup
+from mako import exceptions
+
+class ExtendedLookup(TemplateLookup):
+
+ inherits = re.compile(r'\<%inherit file="([^"]+)" /\>')
+ includes = re.compile(r'\<%namespace file="([^"]+)" import="[^"]+" /\>')
+
+ # remember already resolved templates
+ resolved = {}
+
+ def get_template(self, uri):
+ """This is stolen and truncated from mako.lookup:TemplateLookup."""
+
+ u = re.sub(r'^\/+', '', uri)
+ for dir in self.directories:
+ filename = posixpath.normpath(posixpath.join(dir, u))
+ if os.path.isfile(filename):
+ return self._load(filename, uri)
+ else:
+ raise exceptions.TopLevelLookupException(
+ "Cant locate template for uri %r" % uri)
+
+ def _load(self, filename, uri):
+
+ def resolve(uri):
+ """Check whether any referenced template has changed -- recursively."""
+
+ if uri in self.resolved:
+ return self.resolved[uri]
+
+ filename = posixpath.normpath(posixpath.join(self.directories[0], uri))
+ p = self.modulename_callable(filename, uri)
+ has_changed = getmtime(filename) > getmtime(p) if isfile(p) else True
+
+ if has_changed:
+ self.resolved[uri] = True
+ return True
+
+ with io.open(filename) as fp:
+ source = fp.read()
+
+ for match in self.inherits.finditer(source):
+ if resolve(match.group(1)):
+ return True
+
+ for match in self.includes.finditer(source):
+ if resolve(match.group(1)):
+ return True
+
+ return False
+
+ try:
+ template = self._collection[uri]
+ except KeyError:
+ template = super(ExtendedLookup, self)._load(filename, uri)
+
+ try:
+ template.has_changed = resolve(basename(template.filename))
+ except (OSError, IOError):
+ raise exceptions.TemplateLookupException(
+ "Can't locate template for uri %r" % uri)
+ return template
class Environment(AbstractEnvironment):
def init(self, layoutdir, cachedir):
- self.mako = TemplateLookup(directories=[layoutdir],
+ self.mako = ExtendedLookup(
+ directories=[layoutdir],
module_directory=cachedir,
# similar to mako.template.Template.__init__ but with
# leading cache_ for the acrylamid cache
@@ -54,9 +122,4 @@ def render(self, **kw):
@property
def has_changed(self):
- # inspired by mako.lookup.TemplateLookup._check
- template_stat = os.stat(self.template.filename)
- if self.template.last_modified < template_stat[stat.ST_MTIME]:
- return True
- else:
- return False
+ return self.template.has_changed
View
26 specs/compile.t
@@ -15,12 +15,12 @@ in less than a second.
$ acrylamid compile -C
create [?.??s] output/articles/index.html (glob)
create [?.??s] output/2012/die-verwandlung/index.html (glob)
- create [0.00s] output/index.html
- create [0.00s] output/tag/die-verwandlung/index.html
- create [0.00s] output/tag/franz-kafka/index.html
+ create [0.0?s] output/index.html (glob)
+ create [0.0?s] output/tag/die-verwandlung/index.html (glob)
+ create [0.0?s] output/tag/franz-kafka/index.html (glob)
create [?.??s] output/atom/index.html (glob)
create [?.??s] output/rss/index.html (glob)
- create [0.00s] output/sitemap.xml
+ create [0.0?s] output/sitemap.xml (glob)
8 new, 0 updated, 0 skipped [?.??s] (glob)
If we compile a second time, nothing should happen at all (except you use a
@@ -80,13 +80,13 @@ Lets try if we have really incremental rendering:
$ acrylamid compile -Cv
update [?.??s] output/articles/index.html (glob)
create [?.??s] output/2012/spam/index.html (glob)
- update [0.00s] output/2012/die-verwandlung/index.html (glob)
+ update [0.0?s] output/2012/die-verwandlung/index.html (glob)
update [?.??s] output/index.html (glob)
skip output/tag/die-verwandlung/index.html
skip output/tag/franz-kafka/index.html
update [?.??s] output/atom/index.html (glob)
update [?.??s] output/rss/index.html (glob)
- update [0.00s] output/sitemap.xml
+ update [0.0?s] output/sitemap.xml (glob)
1 new, 6 updated, 2 skipped [?.??s] (glob)
Now with templates. We have patched jinja2 template loader so we have a
@@ -165,10 +165,10 @@ Now we change the base template and should see some updates:
$ acrylamid compile -Cv
update [?.??s] output/articles/index.html (glob)
update [?.??s] output/2012/spam/index.html (glob)
- update [0.00s] output/2012/die-verwandlung/index.html (glob)
- update [0.00s] output/index.html
- update [0.00s] output/tag/die-verwandlung/index.html
- update [0.00s] output/tag/franz-kafka/index.html
+ update [0.0?s] output/2012/die-verwandlung/index.html (glob)
+ update [0.0?s] output/index.html (glob)
+ update [0.0?s] output/tag/die-verwandlung/index.html (glob)
+ update [0.0?s] output/tag/franz-kafka/index.html (glob)
skip output/atom/index.html
skip output/rss/index.html
identical output/sitemap.xml
@@ -184,9 +184,9 @@ If we change a filter in conf.py we should see an update:
skip output/articles/index.html
identical output/2012/spam/index.html
update [?.??s] output/2012/die-verwandlung/index.html (glob)
- update [0.00s] output/index.html
- update [0.00s] output/tag/die-verwandlung/index.html
- update [0.00s] output/tag/franz-kafka/index.html
+ update [0.0?s] output/index.html (glob)
+ update [0.0?s] output/tag/die-verwandlung/index.html (glob)
+ update [0.0?s] output/tag/franz-kafka/index.html (glob)
identical output/atom/index.html
identical output/rss/index.html
identical output/sitemap.xml
View
1  tox.ini
@@ -13,6 +13,7 @@ deps=
cram
unidecode
translitcodec
+ mako
[testenv]
commands=
nosetests specs/
Please sign in to comment.
Something went wrong with that request. Please try again.