Skip to content

Commit

Permalink
Revert to minidom for ease of use
Browse files Browse the repository at this point in the history
  • Loading branch information
3j14 committed Aug 26, 2019
1 parent 945ef31 commit dbdc7d3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
35 changes: 25 additions & 10 deletions django_feather/templatetags/icon.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import io
import time
from xml.dom import minidom

from django import template

from django_feather import icons
Expand Down Expand Up @@ -41,16 +45,13 @@ def render(self, context) -> str:
# Icon could not be found
return ''

svg: str = getattr(icons, icon_name)
head: list = ['<svg']
tail: list = [svg.split('<svg ')[-1]]
attributes: list = ['%s=\"%s\"' % (attr, val.resolve(context))
for attr, val in self.attrs.items()]
if 'width' not in self.attrs:
attributes.append('width=\"24\"')
if 'height' not in self.attrs:
attributes.append('height=\"24\"')
return ' '.join(head + attributes + tail)
doc = minidom.parseString(getattr(icons, icon_name))
for attr, val in self.attrs.items():
doc.documentElement.setAttribute(attr, val.resolve(context))

writer = io.StringIO()
SVGDocument(doc).writexml(writer)
return writer.getvalue()


@register.tag(name='icon')
Expand Down Expand Up @@ -80,3 +81,17 @@ def icon(parser, token) -> IconNode:
attrs[attr] = parser.compile_filter(val)

return IconNode(icon_expr, attrs=attrs)


class SVGDocument:
def __init__(self, doc: minidom.Document):
self._doc = doc

@property
def doc(self) -> minidom.Document:
return self._doc

def writexml(self, writer, indent="", addindent="", newl=""):
"""Ignore the xml tag"""
for node in self.doc.childNodes:
node.writexml(writer, indent, addindent, newl)
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def run(self):
icon_name = str(file.split('.')[0]).replace('-', '_')
with open(path.join(icon_dir, file), 'r') as icon:
svg = icon.read()
# Modify the svg, remove width and height
svg = re.sub(r'((?<!-)(width|height)=\"\d*\"|\n)', r'', svg)
# Modify the svg, remove line and spaces
svg = re.sub(r'\n', r' ', svg)
svg = re.sub(r'\s+', r' ', svg)
svg = svg.replace('> <', '><').replace(' />', '/>')
# Add to the build file
Expand All @@ -42,7 +42,7 @@ def run(self):

setup(
name='django-feather',
version='0.2.3',
version='0.2.5',
author='Jonas Drotleff',
author_email='[email protected]',
cmdclass={
Expand Down

0 comments on commit dbdc7d3

Please sign in to comment.