Skip to content

Commit

Permalink
Added Mako parsing rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Kreft committed Aug 12, 2014
1 parent 8871a8d commit de963f2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 10 deletions.
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -17,7 +17,7 @@

setup(
name='template-remover',
version='0.1.6',
version='0.1.7',
description='Remove the template markup from html files',
long_description=open('README.rst').read(),
author='Sebastian Kreft - Deezer',
Expand Down
39 changes: 30 additions & 9 deletions template_remover.py
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Replaces the template markup (PHP, Jinja) from an HTML file.
"""Replaces the template markup (PHP, Jinja, Mako) from an HTML file.
It replaces the markup so the lines and positions of actual HTML content is
preserved.
Expand Down Expand Up @@ -51,6 +51,13 @@
JINJA_END_TAG = r'%}'
JINJA_END_ECHO = r'}}'

# Mako patterns
MAKO_START_ECHO = r'\${'
MAKO_END_ECHO = r'}'
MAKO_START_TAG = (
r'(</?%|%\s*(end)?(if|else|elif|for|while|try|catch|except|finally)|##)')
MAKO_END_TAG = r'([%/]?>|$)'


def get_pattern(echo_tags, start_tags, end_tags):
"""Constructs a pattern to use with the method clean().
Expand Down Expand Up @@ -90,26 +97,32 @@ def get_pattern(echo_tags, start_tags, end_tags):
[PHP_START_TAG, PHP_START_TAG_SHORT],
[PHP_END_TAG])

MAKO_PATTERN = get_pattern(
[MAKO_START_ECHO],
[MAKO_START_TAG],
[MAKO_END_TAG, MAKO_END_ECHO])

ALL_PATTERN = get_pattern(
[PHP_START_ECHO, PHP_START_TAG_WITH_ECHO, PHP_START_TAG_SHORT_WITH_ECHO,
JINJA_START_ECHO],
[PHP_START_TAG, PHP_START_TAG_SHORT, JINJA_START_TAG],
[PHP_END_TAG, JINJA_END_TAG, JINJA_END_ECHO])
JINJA_START_ECHO, MAKO_START_ECHO],
[PHP_START_TAG, PHP_START_TAG_SHORT, JINJA_START_TAG, MAKO_START_TAG],
[PHP_END_TAG, JINJA_END_TAG, JINJA_END_ECHO, MAKO_END_TAG, MAKO_END_ECHO])


def _get_tag(match):
groups = match.groupdict()
if groups.get('echo'):
if groups.get('echo') is not None:
return 'ECHO'
elif groups.get('end'):
elif groups.get('end') is not None:
return 'END'
elif groups.get('start'):
elif groups.get('start') is not None:
return 'START'
elif groups.get('newline'):
elif groups.get('newline') is not None:
return 'NEWLINE'
elif groups.get('spaces'):
elif groups.get('spaces') is not None:
return 'SPACES'

print(groups)
assert False, ('Only the groups "echo", "end", "start", "newline" and ' +
'"spaces" are allowed. Please correct your pattern or use ' +
'the method get_pattern() to construct it.')
Expand Down Expand Up @@ -261,3 +274,11 @@ def clean_jinja(html_content):
See clean() for more details.
"""
return clean(html_content, pattern=JINJA_PATTERN)


def clean_mako(html_content):
"""Removes the Mako markup from the supplied string.
See clean() for more details.
"""
return clean(html_content, pattern=MAKO_PATTERN)
39 changes: 39 additions & 0 deletions test/test_template_remover.py
Expand Up @@ -162,3 +162,42 @@ def test_jinja_does_not_complain_with_double_braces(self):
template = '<script>var dict={foo:{}}</script>'
expected = '<script>var dict={foo:{}}</script>'
self.assertEquals(expected, template_remover.clean_jinja(template))

def test_mako_comprehensive(self):
template = '''<html>
<body>
<a title="${"this is some text" | u}">Foo</a>
% if x == 5:
<a href="${foo}"><a>
% endif
<%
x = db.get_resource('foo')
y = [z.element for z in x if x.frobnizzle==5]
%>
<%include file="foo.txt"/>
## Comment
<%def name="foo" buffered="True">
this is a def
</%def>
'''
expected = '''<html>
<body>
<a title="00000000000000000000000000">Foo</a>
<a href="000000"><a>
this is a def
'''
self.assertEquals(expected, template_remover.clean_mako(template))

0 comments on commit de963f2

Please sign in to comment.