convert pystache to private library since it doesn't behave like upstream

This commit is contained in:
Damien Elmes 2010-11-29 22:51:55 +09:00
parent dfb21a0f71
commit a5af8fb8e9
9 changed files with 24 additions and 16 deletions

View file

@ -9,7 +9,7 @@ The Deck
__docformat__ = 'restructuredtext' __docformat__ = 'restructuredtext'
import tempfile, time, os, random, sys, re, stat, shutil 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.db import *
from anki.lang import _, ngettext from anki.lang import _, ngettext
@ -25,6 +25,7 @@ from anki.tags import initTagTables, tagIds
from operator import itemgetter from operator import itemgetter
from itertools import groupby from itertools import groupby
from anki.hooks import runHook, hookEmpty from anki.hooks import runHook, hookEmpty
from anki.template import render
# ensure all the metadata in other files is loaded before proceeding # ensure all the metadata in other files is loaded before proceeding
import anki.models, anki.facts, anki.cards, anki.stats import anki.models, anki.facts, anki.cards, anki.stats
@ -1450,8 +1451,8 @@ and due < :now""", now=time.time())
empty['tags'] = "" empty['tags'] = ""
local['tags'] = fact.tags local['tags'] = fact.tags
try: try:
if (pystache.render(format, local) == if (render(format, local) ==
pystache.render(format, empty)): render(format, empty)):
ok = False ok = False
break break
except (KeyError, TypeError, ValueError): except (KeyError, TypeError, ValueError):

View file

@ -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 sqlalchemy.ext.orderinglist import ordering_list
from anki.db import * from anki.db import *
from anki.utils import genID, canonifyTags 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.utils import parseTags, hexifyID, checksum, stripHTML
from anki.lang import _ from anki.lang import _
from anki.hooks import runFilter from anki.hooks import runFilter
from anki.template import render
from copy import copy from copy import copy
def alignmentLabels(): def alignmentLabels():
@ -154,7 +155,7 @@ def formatQA(cid, mid, fact, tags, cm):
format = re.sub("%\((.+?)\)s", "{{\\1}}", format) format = re.sub("%\((.+?)\)s", "{{\\1}}", format)
# allow custom rendering functions & info # allow custom rendering functions & info
fields = runFilter("prepareFields", fields, cid, mid, fact, tags, cm) 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) d[type] = runFilter("formatQA", html, type, cid, mid, fact, tags, cm)
return d return d

View file

@ -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

View file

@ -1,5 +1,5 @@
from pystache.template import Template from anki.template.template import Template
from pystache.view import View from anki.template.view import View
def render(template, context=None, **kwargs): def render(template, context=None, **kwargs):
context = context and context.copy() or {} context = context and context.copy() or {}

View file

@ -1,4 +1,4 @@
from pystache import Template from anki.template import Template
import os.path import os.path
import re import re
@ -19,7 +19,7 @@ class View(object):
# Contents of the template. # Contents of the template.
template = None template = None
# Character encoding of the template file. If None, Pystache will not # Character encoding of the template file. If None, Pystache will not
# do any decoding of the template. # do any decoding of the template.
template_encoding = None template_encoding = None
@ -47,24 +47,24 @@ class View(object):
def load_template(self): def load_template(self):
if self.template: if self.template:
return self.template return self.template
if self.template_file: if self.template_file:
return self._load_template() return self._load_template()
name = self.get_template_name() + '.' + self.template_extension name = self.get_template_name() + '.' + self.template_extension
if isinstance(self.template_path, basestring): if isinstance(self.template_path, basestring):
self.template_file = os.path.join(self.template_path, name) self.template_file = os.path.join(self.template_path, name)
return self._load_template() return self._load_template()
for path in self.template_path: for path in self.template_path:
self.template_file = os.path.join(path, name) self.template_file = os.path.join(path, name)
if os.path.exists(self.template_file): if os.path.exists(self.template_file):
return self._load_template() return self._load_template()
raise IOError('"%s" not found in "%s"' % (name, ':'.join(self.template_path),)) raise IOError('"%s" not found in "%s"' % (name, ':'.join(self.template_path),))
def _load_template(self): def _load_template(self):
f = open(self.template_file, 'r') f = open(self.template_file, 'r')
try: try:

View file

@ -24,7 +24,7 @@ setup(name='anki',
author_email='anki@ichi2.net', author_email='anki@ichi2.net',
url='http://ichi2.net/anki/index.html', url='http://ichi2.net/anki/index.html',
license='GPLv3', license='GPLv3',
packages=["anki", "anki.importing", "pystache"], packages=["anki", "anki.importing", "anki.template"],
package_data={'anki': ['locale/*/*/*'],}, package_data={'anki': ['locale/*/*/*'],},
include_package_data=True, include_package_data=True,
zip_safe=False, zip_safe=False,