summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2006-12-09 01:56:46 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2006-12-09 01:56:46 +0000
commitecc8dab3ab8d2d58a0faadc16fa928e8eda5f314 (patch)
tree456bd368b4aa623f153309e51f415e450ead3c47
parent5d922a3b9133dac77ab7374868b91e79b1794dd1 (diff)
downloadmako-ecc8dab3ab8d2d58a0faadc16fa928e8eda5f314.tar.gz
lexer picks up on magic encoding comment
-rw-r--r--examples/wsgi/htdocs/index.html3
-rw-r--r--lib/mako/codegen.py3
-rw-r--r--lib/mako/lexer.py12
-rw-r--r--test/lexer.py1
-rw-r--r--test/lookup.py20
-rw-r--r--test/template.py17
6 files changed, 46 insertions, 10 deletions
diff --git a/examples/wsgi/htdocs/index.html b/examples/wsgi/htdocs/index.html
index 79a5af2..0f0ce9d 100644
--- a/examples/wsgi/htdocs/index.html
+++ b/examples/wsgi/htdocs/index.html
@@ -1,3 +1,6 @@
+<%
+%>
+
<%inherit file="root.html"/>
This is index.html
diff --git a/lib/mako/codegen.py b/lib/mako/codegen.py
index 71f8152..a06ef26 100644
--- a/lib/mako/codegen.py
+++ b/lib/mako/codegen.py
@@ -66,6 +66,8 @@ class _GenerateRenderMethod(object):
namespaces = {}
module_code = []
pagetag = [None]
+ encoding =[None]
+
class FindTopLevel(object):
def visitInheritTag(s, node):
inherit.append(node)
@@ -76,6 +78,7 @@ class _GenerateRenderMethod(object):
def visitCode(self, node):
if node.ismodule:
module_code.append(node)
+
f = FindTopLevel()
for n in self.node.nodes:
n.accept_visitor(f)
diff --git a/lib/mako/lexer.py b/lib/mako/lexer.py
index 921d206..d032b5a 100644
--- a/lib/mako/lexer.py
+++ b/lib/mako/lexer.py
@@ -88,7 +88,12 @@ class Lexer(object):
raise exceptions.SyntaxException("Keyword '%s' not a legal ternary for keyword '%s'" % (node.keyword, self.control_line[-1].keyword), self.matched_lineno, self.matched_charpos, self.filename)
def parse(self):
+ encoding = self.match_encoding()
+ if encoding:
+ self.text = self.text.decode(encoding)
+
length = len(self.text)
+
while (True):
if self.match_position > length:
break
@@ -116,6 +121,13 @@ class Lexer(object):
raise exceptions.SyntaxException("Unclosed tag: <%%%s>" % self.tag[-1].keyword, self.matched_lineno, self.matched_charpos, self.filename)
return self.template
+ def match_encoding(self):
+ match = self.match(r'#\s*-\*- encoding: (.+?) -\*-\n')
+ if match:
+ return match.group(1)
+ else:
+ return None
+
def match_tag_start(self):
match = self.match(r'''
\<% # opening tag
diff --git a/test/lexer.py b/test/lexer.py
index 8b7fe67..db9d4c8 100644
--- a/test/lexer.py
+++ b/test/lexer.py
@@ -14,6 +14,7 @@ class LexerTest(unittest.TestCase):
and some more text.
"""
node = Lexer(template).parse()
+ print repr(node)
assert repr(node) == r"""TemplateNode({}, [Text('\n<b>Hello world</b>\n ', (1, 1)), DefTag('def', {'name': 'foo'}, (3, 9), ["Text('\\n this is a def.\\n ', (3, 26))"]), Text('\n \n and some more text.\n', (5, 16))])"""
def test_unclosed_tag(self):
diff --git a/test/lookup.py b/test/lookup.py
index 25d54e3..5986f64 100644
--- a/test/lookup.py
+++ b/test/lookup.py
@@ -7,16 +7,18 @@ import os
if not os.access('./test_htdocs', os.F_OK):
os.mkdir('./test_htdocs')
- file('./test_htdocs/index.html', 'w').write("this is index")
- file('./test_htdocs/incl.html', 'w').write("this is include 1")
+file('./test_htdocs/index.html', 'w').write("this is index")
+file('./test_htdocs/incl.html', 'w').write("this is include 1")
+if not os.access('./test_htdocs/subdir', os.F_OK):
os.mkdir('./test_htdocs/subdir')
- file('./test_htdocs/subdir/incl.html', 'w').write("""
- this is include 2
- """)
- file('./test_htdocs/subdir/index.html', 'w').write("""
- this is sub index
- <%include file="incl.html"/>
- """)
+file('./test_htdocs/subdir/incl.html', 'w').write("""
+ this is include 2
+""")
+file('./test_htdocs/subdir/index.html', 'w').write("""
+ this is sub index
+ <%include file="incl.html"/>
+""")
+
tl = lookup.TemplateLookup(directories=['./test_htdocs'])
class LookupTest(unittest.TestCase):
def test_basic(self):
diff --git a/test/template.py b/test/template.py
index 6c9c4ef..5700d76 100644
--- a/test/template.py
+++ b/test/template.py
@@ -1,9 +1,14 @@
# -*- encoding: utf-8 -*-
from mako.template import Template
-import unittest, re
+import unittest, re, os
from util import flatten_result, result_lines
+if not os.access('./test_htdocs', os.F_OK):
+ os.mkdir('./test_htdocs')
+file('./test_htdocs/unicode.html', 'w').write("""# -*- encoding: utf-8 -*-
+Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""")
+
class EncodingTest(unittest.TestCase):
def test_unicode(self):
template = Template(u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""")
@@ -14,6 +19,16 @@ class EncodingTest(unittest.TestCase):
template = Template("${val}")
assert template.render_unicode(val=val) == u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"""
+ def test_unicode_file(self):
+ template = Template(filename='./test_htdocs/unicode.html', module_directory='./test_htdocs')
+ assert template.render_unicode() == u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"""
+
+ def test_unicode_memory(self):
+ val = u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"""
+ val = "# -*- encoding: utf-8 -*-\n" + val.encode('utf-8')
+ template = Template(val)
+ assert template.render_unicode() == u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"""
+
class ControlTest(unittest.TestCase):
def test_control(self):
t = Template("""