Skip to content
Snippets Groups Projects
Commit ecc8dab3 authored by Mike Bayer's avatar Mike Bayer
Browse files

lexer picks up on magic encoding comment

parent 5d922a3b
No related branches found
No related tags found
No related merge requests found
<%
%>
<%inherit file="root.html"/>
This is index.html
......
......@@ -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)
......
......@@ -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
......
......@@ -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):
......
......@@ -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):
......
# -*- 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("""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment