Skip to content

Commit

Permalink
fixed super call for python2 compatibility (note HTMLParser not a sub…
Browse files Browse the repository at this point in the history
…class of Object), djeffified dhtml variable, renamed djeffify to djeffify_html
  • Loading branch information
willplaehn committed Nov 12, 2015
1 parent c09e58d commit 7aac049
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
18 changes: 9 additions & 9 deletions djeff/djeff.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def get_template(self, template_name, dirs=_dirs_undefined):
class DjeffTemplate(Template):
def render(self, context=None, request=None):
rendered_context = super().render(context, request)
return djeffify(rendered_context)
return djeffify_html(rendered_context)


def djeffify_string(string_to_djeff):
Expand All @@ -29,38 +29,38 @@ def djeffify_string(string_to_djeff):
return string_to_djeff


def djeffify(rendered_string):
def djeffify_html(rendered_string):
"""
This function contains the core logic for a
middleware, template tag or Template engine approach
"""
parser = DjeffParser()
parser.feed(rendered_string)
return parser.dhtml
return parser.djhtml


def reconstruct_attrs(attrs):
tag_string = ''
for attr in attrs:
tag_string += (attr[0] + '=' + attr[1] + ' ')
tag_string += ('{}={} ').format(attr[0], attr[1])
return tag_string.strip()


class DjeffParser(HTMLParser):
def __init__(self, convert_charrefs=True, *args, **kwargs):
super().__init__(convert_charrefs, *args, **kwargs)
self.dhtml = ''
HTMLParser.__init__(self)
self.djhtml = ''

def handle_starttag(self, tag, attrs):
self.dhtml += '<{} {}>'.format(tag, reconstruct_attrs(attrs))
self.djhtml += '<{} {}>'.format(tag, reconstruct_attrs(attrs))

def handle_endtag(self, tag):
self.dhtml += '</{}>'.format(tag)
self.djhtml += '</{}>'.format(tag)

def handle_data(self, data):
"""
Djeffify data between tags
"""
if data.strip():
data = djeffify_string(data)
self.dhtml += data
self.djhtml += data
11 changes: 11 additions & 0 deletions tests/test_djeff.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ def test_001_djeffify_string_spaces_and_case_insensitive(self):

assert(out == expected_out)

def test_002_instantiate_parser(self):
djeff.DjeffParser()

def test_003_reconstruct_attrs(self):
test_list = [('attr1', 'val1'), ('attr2', 'val2')]
expected_out = 'attr1=val1 attr2=val2'

out = djeff.reconstruct_attrs(test_list)

assert (out == expected_out)


if __name__ == '__main__':
import sys
Expand Down

0 comments on commit 7aac049

Please sign in to comment.