SVN: r25792 - in trunk/quixote: . test
David Binger <dbinger-fVcApmY9cLvQ3/1i3zOLAti2O/[email protected]> Fri, 17 Dec 2004 19:11:56 -0500
| Newsgroups | gmane.comp.web.quixote.cvs |
|---|---|
| Message-ID | <[email protected]> |
Author: dbinger
Date: 2004-12-16 11:09:05 -0500 (Thu, 16 Dec 2004)
New Revision: 25792
Modified:
trunk/quixote/ptl_compile.py
trunk/quixote/test/utest_ptl.py
Log:
Make $-substitution optional.
[html] and [plain] templates now work without $-substitution, as in releases
before 2.0a2.
The ptl compiler now supports [html$] and [plain$] templates that do include
$-substitution.
Modified: trunk/quixote/ptl_compile.py
===================================================================
--- trunk/quixote/ptl_compile.py 2004-12-16 14:34:07 UTC (rev 25791)
+++ trunk/quixote/ptl_compile.py 2004-12-16 16:09:05 UTC (rev 25792)
@@ -32,16 +32,21 @@
HTML_TEMPLATE_PREFIX = "_q_html_template_"
PLAIN_TEMPLATE_PREFIX = "_q_plain_template_"
+HTML_DOLLAR_TEMPLATE_PREFIX = "_q_html_dollar_template_"
+PLAIN_DOLLAR_TEMPLATE_PREFIX = "_q_plain_dollar_template_"
class TemplateTransformer(transformer.Transformer):
def __init__(self, *args, **kwargs):
transformer.Transformer.__init__(self, *args, **kwargs)
- self.__template_type = [] # stack, "html", "plain" or None
+ # __template_type is a stack whose values are
+ # "html", "plain", "html$", "plain$", or None
+ self.__template_type = []
def _get_template_type(self):
- """Return the type of the function being compiled ("html", "plain"
- or None)."""
+ """Return the type of the function being compiled (
+ "html", "plain", "html$", "plain$", or None)
+ """
if self.__template_type:
return self.__template_type[-1]
else:
@@ -73,11 +78,10 @@
name = nodelist[-4][1]
args = nodelist[-3][2]
- if not re.match('_q_((html|plain)_)?template_', name):
+ if not re.match('_q_(html|plain)_(dollar_)?template_', name):
# just a normal function, let base class handle it
self.__template_type.append(None)
n = transformer.Transformer.funcdef(self, nodelist)
-
else:
if name.startswith(PLAIN_TEMPLATE_PREFIX):
name = name[len(PLAIN_TEMPLATE_PREFIX):]
@@ -85,6 +89,12 @@
elif name.startswith(HTML_TEMPLATE_PREFIX):
name = name[len(HTML_TEMPLATE_PREFIX):]
template_type = "html"
+ elif name.startswith(HTML_DOLLAR_TEMPLATE_PREFIX):
+ name = name[len(HTML_DOLLAR_TEMPLATE_PREFIX):]
+ template_type = "html$"
+ elif name.startswith(PLAIN_DOLLAR_TEMPLATE_PREFIX):
+ name = name[len(PLAIN_DOLLAR_TEMPLATE_PREFIX):]
+ template_type = "plain$"
else:
raise RuntimeError, 'unknown prefix on %s' % name
@@ -102,7 +112,7 @@
# _q_output = _q_TemplateIO()
klass = ast.Name('_q_TemplateIO')
- args = [ast.Const(template_type == "html")]
+ args = [ast.Const(template_type in ("html", "html$"))]
instance = ast.CallFunc(klass, args)
assign_name = ast.AssName('_q_output', OP_ASSIGN)
assign = ast.Assign([assign_name], instance)
@@ -154,7 +164,7 @@
for node in nodelist:
k = k + eval(node[1])
lineno = node[2]
- if self._get_template_type() is not None and '$' in k:
+ if self._get_template_type() in ('html$', 'plain$') and '$' in k:
try:
k = _convert_string(k)
except ValueError, e:
@@ -165,7 +175,7 @@
return self._get_text_node(k)
def _get_text_node(self, k):
- if self._get_template_type() == "html":
+ if self._get_template_type() in ("html", "html$"):
return ast.CallFunc(ast.Name('_q_htmltext'), [ast.Const(k)])
else:
return ast.Const(k)
@@ -192,11 +202,12 @@
raise ValueError('invalid substitution %r' % invalid)
return _substitution_re.sub(convert, s.replace('%', '%%'))
-_template_re = re.compile(r"^([ \t]*) def (?:[ \t]+)" # def
- r" ([a-zA-Z_][a-zA-Z_0-9]*)" # <name>
- r" (?:[ \t]*) \[(plain|html)\] (?:[ \t]*)" # <type>
- r" (?:[ \t]*[\(\\])", # (
- re.MULTILINE|re.VERBOSE)
+_template_re = re.compile(
+ r"^(?P<indent>[ \t]*) def (?:[ \t]+)"
+ r" (?P<name>[a-zA-Z_][a-zA-Z_0-9]*)"
+ r" (?:[ \t]*) \[(?P<type>plain|html|html\$|plain\$)\] (?:[ \t]*)"
+ r" (?:[ \t]*[\(\\])",
+ re.MULTILINE|re.VERBOSE)
def translate_tokens(buf):
"""
@@ -211,8 +222,15 @@
XXX This parser is too stupid. For example, it doesn't understand
triple quoted strings.
"""
- return _template_re.sub(r"\1def _q_\3_template_\2(", buf)
+ def replacement(match):
+ template_type = match.group('type')
+ if template_type[-1] == '$':
+ template_type = template_type[:-1] + '_dollar'
+ return '%sdef _q_%s_template_%s(' % (match.group('indent'),
+ template_type,
+ match.group('name'))
+ return _template_re.sub(replacement, buf)
def parse(buf, filename='<string>'):
buf = translate_tokens(buf)
Modified: trunk/quixote/test/utest_ptl.py
===================================================================
--- trunk/quixote/test/utest_ptl.py 2004-12-16 14:34:07 UTC (rev 25791)
+++ trunk/quixote/test/utest_ptl.py 2004-12-16 16:09:05 UTC (rev 25792)
@@ -57,29 +57,29 @@
run_ptl('def f(a):',
' "yes $a"',
'assert f(1) == None')
- run_ptl('def f [plain] (a):',
+ run_ptl('def f [plain$] (a):',
' "yes $a"',
'assert f(1) == "yes 1"')
- run_ptl('def f [html] (a):',
+ run_ptl('def f [html$] (a):',
' "yes ${a} $a $$"',
'assert f(1) == "yes 1 1 $"')
try:
- run_ptl('def f [plain] (a):',
+ run_ptl('def f [plain$] (a):',
' "yes ${a} $a $"')
assert 0
except SyntaxError, e:
assert str(e) == "invalid substitution '$' (line 2) (test, line 2)"
try:
- run_ptl('def f [plain] (a):',
+ run_ptl('def f [plain$] (a):',
' "$-"')
assert 0
except SyntaxError, e:
assert str(e) == (
"invalid substitution '$-' (line 2) (test, line 2)")
- run_ptl('def f [html] (a):',
+ run_ptl('def f [html$] (a):',
' "yes %s $a" % "odd"',
'assert f(1) == "yes odd 1"')
- run_ptl('def f [plain] (a):',
+ run_ptl('def f [plain$] (a):',
' "yes %(a)s $a" % locals()',
'assert f(1) == "yes 1 1"')