Skip to content

Commit

Permalink
Improve rendering perfomance
Browse files Browse the repository at this point in the history
  • Loading branch information
maxpoletaev committed Aug 11, 2016
1 parent 3bd4c73 commit 1c847b9
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions xmltag/renderer.py
@@ -1,3 +1,5 @@
from io import StringIO

class Renderer:
def __init__(self, strict_mode=False, single_tags=[]):
self.single_tags = set(single_tags)
Expand All @@ -6,25 +8,32 @@ def __init__(self, strict_mode=False, single_tags=[]):
def render_tag(self, tag, content=None, attrs={}):
is_single = tag in self.single_tags
attrs = self.render_attrs(attrs)
html = '<' + tag

buf = []
buf.append('<')
buf.append(tag)

if attrs:
html += ' ' + attrs
buf.append(' ')
buf.append(attrs)

html += ' />' if self.strict_mode and (not content and is_single) else '>'
if self.strict_mode and (is_single and not content):
buf.append(' />')
else:
buf.append('>')

if content:
html += content
buf.append(content)

if content or not is_single:
html += '</{}>'.format(tag)
buf.append('</{}>'.format(tag))

return html
return ''.join(buf)

def render_attrs(self, attrs):
result = []
is_true = set(['true'])
is_false = set(['false', 'none', 'null'])
is_true = {'true'}
is_false = {'false', 'none', 'null'}

for key, value in attrs.items():
key = key.strip('_').replace('_', '-')
Expand Down

0 comments on commit 1c847b9

Please sign in to comment.