Skip to content

Commit

Permalink
Handle blank templates correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
danmichaelo committed Oct 25, 2014
1 parent feb6f98 commit 00e3147
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
6 changes: 5 additions & 1 deletion mwtemplates/templateeditor2.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ def __init__(self, editor, doc):
def _templates(self):
templates = []
for node in self.doc.findall('.//template'):
templates.append(Template(node, self.editor))
q = Template(node, self.editor)
if q.key != '':
templates.append(q)
return templates

# def add(self, node):
Expand Down Expand Up @@ -435,6 +437,8 @@ def __repr__(self):
def key(self):
tmp = self._name.strip()
tmp = re.sub(r'^(?:[Mm]al|[Tt]emplate):', '', tmp)
if len(tmp) == 0:
return ''
return tmp[0].upper() + tmp[1:]

@property
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
README = open(os.path.join(here, 'README.rst')).read()

setup(name='mwtemplates',
version='0.3.1',
version='0.3.2',
description='MediaWiki template parser and editor',
long_description=README,
classifiers=[
Expand Down
12 changes: 12 additions & 0 deletions tests/templateeditor2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,5 +354,17 @@ def test_delete_template(self):
dp.templates['Infoboks A'][0].remove() # Note that 'del dp.templates['Infoboks A'][0]' currently does not work
self.assertEqual(dp.wikitext(), text2)

def test_blank_template(self):
# Should not crash...
text = 'Lorem ipsum {{}} dolor sit amet'
te = TemplateEditor(text)
self.assertEqual(len(te.templates), 0)

def test_blank_template_roundtrip(self):
# Check that a parameter can be deleted
text = 'Lorem ipsum {{ }} dolor sit amet'
dp = TemplateEditor(text)
self.assertEqual(dp.wikitext(), text)

if __name__ == '__main__':
unittest.main()

0 comments on commit 00e3147

Please sign in to comment.