Skip to content

Commit

Permalink
Modified templatize to ignore non-haml files
Browse files Browse the repository at this point in the history
  • Loading branch information
cordery committed Aug 23, 2013
1 parent fe3ba8b commit fd3c93d
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions hamlpy/templatize.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
"""
This module decorates the django templatize function to parse haml templates
before the translation utility extracts tags from it.
--Modified to ignore non-haml files.
"""

try:
from django.utils.translation import trans_real
_django_available = True
except ImportError, e:
_django_available = False

import hamlpy


import os


def decorate_templatize(func):
def templatize(src, origin=None):
hamlParser = hamlpy.Compiler()
html = hamlParser.process(src.decode('utf-8'))
return func(html.encode('utf-8'), origin)

return templatize

def templatize(src, origin=None):
#if the template has no origin file then do not attempt to parse it with haml
if origin:
#if the template has a source file, then only parse it if it is haml
if os.path.splitext(origin)[1].lower() in ['.'+x.lower() for x in hamlpy.VALID_EXTENSIONS]:
hamlParser = hamlpy.Compiler()
html = hamlParser.process(src.decode('utf-8'))
src = html.encode('utf-8')
return func(src, origin)
return templatize

if _django_available:
trans_real.templatize = decorate_templatize(trans_real.templatize)

trans_real.templatize = decorate_templatize(trans_real.templatize)

1 comment on commit fd3c93d

@rotherfuchs
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

html = hamlParser.process(src.decode('utf-8')) this might throw a UnicodeEncodeError when using makemessages.

Please sign in to comment.