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"/> <%inherit file="root.html"/>
This is index.html This is index.html
......
...@@ -66,6 +66,8 @@ class _GenerateRenderMethod(object): ...@@ -66,6 +66,8 @@ class _GenerateRenderMethod(object):
namespaces = {} namespaces = {}
module_code = [] module_code = []
pagetag = [None] pagetag = [None]
encoding =[None]
class FindTopLevel(object): class FindTopLevel(object):
def visitInheritTag(s, node): def visitInheritTag(s, node):
inherit.append(node) inherit.append(node)
...@@ -76,6 +78,7 @@ class _GenerateRenderMethod(object): ...@@ -76,6 +78,7 @@ class _GenerateRenderMethod(object):
def visitCode(self, node): def visitCode(self, node):
if node.ismodule: if node.ismodule:
module_code.append(node) module_code.append(node)
f = FindTopLevel() f = FindTopLevel()
for n in self.node.nodes: for n in self.node.nodes:
n.accept_visitor(f) n.accept_visitor(f)
......
...@@ -88,7 +88,12 @@ class Lexer(object): ...@@ -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) 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): def parse(self):
encoding = self.match_encoding()
if encoding:
self.text = self.text.decode(encoding)
length = len(self.text) length = len(self.text)
while (True): while (True):
if self.match_position > length: if self.match_position > length:
break break
...@@ -116,6 +121,13 @@ class Lexer(object): ...@@ -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) raise exceptions.SyntaxException("Unclosed tag: <%%%s>" % self.tag[-1].keyword, self.matched_lineno, self.matched_charpos, self.filename)
return self.template 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): def match_tag_start(self):
match = self.match(r''' match = self.match(r'''
\<% # opening tag \<% # opening tag
......
...@@ -14,6 +14,7 @@ class LexerTest(unittest.TestCase): ...@@ -14,6 +14,7 @@ class LexerTest(unittest.TestCase):
and some more text. and some more text.
""" """
node = Lexer(template).parse() 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))])""" 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): def test_unclosed_tag(self):
......
...@@ -7,16 +7,18 @@ import os ...@@ -7,16 +7,18 @@ import os
if not os.access('./test_htdocs', os.F_OK): if not os.access('./test_htdocs', os.F_OK):
os.mkdir('./test_htdocs') os.mkdir('./test_htdocs')
file('./test_htdocs/index.html', 'w').write("this is index") file('./test_htdocs/index.html', 'w').write("this is index")
file('./test_htdocs/incl.html', 'w').write("this is include 1") 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') os.mkdir('./test_htdocs/subdir')
file('./test_htdocs/subdir/incl.html', 'w').write(""" file('./test_htdocs/subdir/incl.html', 'w').write("""
this is include 2 this is include 2
""") """)
file('./test_htdocs/subdir/index.html', 'w').write(""" file('./test_htdocs/subdir/index.html', 'w').write("""
this is sub index this is sub index
<%include file="incl.html"/> <%include file="incl.html"/>
""") """)
tl = lookup.TemplateLookup(directories=['./test_htdocs']) tl = lookup.TemplateLookup(directories=['./test_htdocs'])
class LookupTest(unittest.TestCase): class LookupTest(unittest.TestCase):
def test_basic(self): def test_basic(self):
......
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
from mako.template import Template from mako.template import Template
import unittest, re import unittest, re, os
from util import flatten_result, result_lines 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): class EncodingTest(unittest.TestCase):
def test_unicode(self): 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! »""") 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): ...@@ -14,6 +19,16 @@ class EncodingTest(unittest.TestCase):
template = Template("${val}") 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! »""" 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): class ControlTest(unittest.TestCase):
def test_control(self): def test_control(self):
t = Template(""" 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