diff --git a/anki/deck.py b/anki/deck.py index c31da1f3f..ff83f396b 100644 --- a/anki/deck.py +++ b/anki/deck.py @@ -9,7 +9,7 @@ The Deck __docformat__ = 'restructuredtext' import tempfile, time, os, random, sys, re, stat, shutil -import types, traceback, simplejson, datetime, pystache +import types, traceback, simplejson, datetime from anki.db import * from anki.lang import _, ngettext @@ -25,6 +25,7 @@ from anki.tags import initTagTables, tagIds from operator import itemgetter from itertools import groupby from anki.hooks import runHook, hookEmpty +from anki.template import render # ensure all the metadata in other files is loaded before proceeding import anki.models, anki.facts, anki.cards, anki.stats @@ -1450,8 +1451,8 @@ and due < :now""", now=time.time()) empty['tags'] = "" local['tags'] = fact.tags try: - if (pystache.render(format, local) == - pystache.render(format, empty)): + if (render(format, local) == + render(format, empty)): ok = False break except (KeyError, TypeError, ValueError): diff --git a/anki/models.py b/anki/models.py index 6757dfa90..8b84415d3 100644 --- a/anki/models.py +++ b/anki/models.py @@ -12,7 +12,7 @@ Model - define the way in which facts are added and shown """ -import time, re, pystache +import time, re from sqlalchemy.ext.orderinglist import ordering_list from anki.db import * from anki.utils import genID, canonifyTags @@ -20,6 +20,7 @@ from anki.fonts import toPlatformFont from anki.utils import parseTags, hexifyID, checksum, stripHTML from anki.lang import _ from anki.hooks import runFilter +from anki.template import render from copy import copy def alignmentLabels(): @@ -154,7 +155,7 @@ def formatQA(cid, mid, fact, tags, cm): format = re.sub("%\((.+?)\)s", "{{\\1}}", format) # allow custom rendering functions & info fields = runFilter("prepareFields", fields, cid, mid, fact, tags, cm) - html = pystache.render(format, fields) + html = render(format, fields) d[type] = runFilter("formatQA", html, type, cid, mid, fact, tags, cm) return d diff --git a/pystache/LICENSE b/anki/template/LICENSE similarity index 100% rename from pystache/LICENSE rename to anki/template/LICENSE diff --git a/anki/template/README.anki b/anki/template/README.anki new file mode 100644 index 000000000..efd8d0969 --- /dev/null +++ b/anki/template/README.anki @@ -0,0 +1,6 @@ +Anki uses a modified version of Pystache to provide Mustache-like syntax. +Behaviour is a little different from standard Mustache: + +- {{text}} returns text verbatim with no HTML escaping +- {{{text}}} strips an outer span tag +- partial rendering is disabled for security reasons diff --git a/pystache/README.rst b/anki/template/README.rst similarity index 100% rename from pystache/README.rst rename to anki/template/README.rst diff --git a/pystache/__init__.py b/anki/template/__init__.py similarity index 67% rename from pystache/__init__.py rename to anki/template/__init__.py index bb832b877..955518123 100644 --- a/pystache/__init__.py +++ b/anki/template/__init__.py @@ -1,5 +1,5 @@ -from pystache.template import Template -from pystache.view import View +from anki.template.template import Template +from anki.template.view import View def render(template, context=None, **kwargs): context = context and context.copy() or {} diff --git a/pystache/template.py b/anki/template/template.py similarity index 100% rename from pystache/template.py rename to anki/template/template.py diff --git a/pystache/view.py b/anki/template/view.py similarity index 97% rename from pystache/view.py rename to anki/template/view.py index 94239033e..b3c43e3d7 100644 --- a/pystache/view.py +++ b/anki/template/view.py @@ -1,4 +1,4 @@ -from pystache import Template +from anki.template import Template import os.path import re @@ -19,7 +19,7 @@ class View(object): # Contents of the template. template = None - + # Character encoding of the template file. If None, Pystache will not # do any decoding of the template. template_encoding = None @@ -47,24 +47,24 @@ class View(object): def load_template(self): if self.template: return self.template - + if self.template_file: return self._load_template() - + name = self.get_template_name() + '.' + self.template_extension - + if isinstance(self.template_path, basestring): self.template_file = os.path.join(self.template_path, name) return self._load_template() - + for path in self.template_path: self.template_file = os.path.join(path, name) if os.path.exists(self.template_file): return self._load_template() - + raise IOError('"%s" not found in "%s"' % (name, ':'.join(self.template_path),)) - + def _load_template(self): f = open(self.template_file, 'r') try: diff --git a/setup.py b/setup.py index 2ecc37a3a..afbda84e1 100644 --- a/setup.py +++ b/setup.py @@ -24,7 +24,7 @@ setup(name='anki', author_email='anki@ichi2.net', url='http://ichi2.net/anki/index.html', license='GPLv3', - packages=["anki", "anki.importing", "pystache"], + packages=["anki", "anki.importing", "anki.template"], package_data={'anki': ['locale/*/*/*'],}, include_package_data=True, zip_safe=False,