diff --git a/CHANGELOG b/CHANGELOG index 4988112..5b592cd 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,8 @@ v0.5 (unreleased): -- Fixed Wikicode.matches() on iterables besides lists and tuples. +- Made Template.remove(keep_field=True) behave more reasonably when the + parameter is already empty. +- Fixed Wikicode.matches()'s behavior on iterables besides lists and tuples. - Fixed len() sometimes raising ValueError on empty node lists. - Fixed release script after changes to PyPI. diff --git a/docs/changelog.rst b/docs/changelog.rst index e1e8ac8..bf0f492 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -7,7 +7,10 @@ v0.5 Unreleased (`changes `__): -- Fixed :meth:`.Wikicode.matches` on iterables besides lists and tuples. +- Made :meth:`Template.remove(keep_field=True) <.Template.remove>` behave more + reasonably when the parameter is already empty. +- Fixed :meth:`.Wikicode.matches`\ 's behavior on iterables besides lists and + tuples. - Fixed ``len()`` sometimes raising ``ValueError`` on empty node lists. - Fixed release script after changes to PyPI. diff --git a/mwparserfromhell/nodes/template.py b/mwparserfromhell/nodes/template.py index 57fec70..ccc63fd 100644 --- a/mwparserfromhell/nodes/template.py +++ b/mwparserfromhell/nodes/template.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2012-2016 Ben Kurtovic +# Copyright (C) 2012-2017 Ben Kurtovic # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -70,7 +70,8 @@ class Template(Node): get(param.value) write("}}") - def _surface_escape(self, code, char): + @staticmethod + def _surface_escape(code, char): """Return *code* with *char* escaped as an HTML entity. The main use of this is to escape pipes (``|``) or equal signs (``=``) @@ -82,7 +83,8 @@ class Template(Node): if char in node: code.replace(node, node.replace(char, replacement), False) - def _select_theory(self, theories): + @staticmethod + def _select_theory(theories): """Return the most likely spacing convention given different options. Given a dictionary of convention options as keys and their occurrence @@ -96,6 +98,22 @@ class Template(Node): if confidence >= 0.75: return tuple(theories.keys())[values.index(best)] + @staticmethod + def _blank_param_value(value): + """Remove the content from *value* while keeping its whitespace. + + Replace *value*\ 's nodes with two text nodes, the first containing + whitespace from before its content and the second containing whitespace + from after its content. + """ + sval = str(value) + if sval.isspace(): + before, after = "", sval + else: + match = re.search(r"^(\s*).*?(\s*)$", sval, FLAGS) + before, after = match.group(1), match.group(2) + value.nodes = [Text(before), Text(after)] + def _get_spacing_conventions(self, use_names): """Try to determine the whitespace conventions for parameters. @@ -119,16 +137,6 @@ class Template(Node): after = self._select_theory(after_theories) return before, after - def _blank_param_value(self, value): - """Remove the content from *value* while keeping its whitespace. - - Replace *value*\ 's nodes with two text nodes, the first containing - whitespace from before its content and the second containing whitespace - from after its content. - """ - match = re.search(r"^(\s*).*?(\s*)$", str(value), FLAGS) - value.nodes = [Text(match.group(1)), Text(match.group(2))] - def _fix_dependendent_params(self, i): """Unhide keys if necessary after removing the param at index *i*.""" if not self.params[i].showkey: diff --git a/tests/test_template.py b/tests/test_template.py index c306b60..a97d6de 100644 --- a/tests/test_template.py +++ b/tests/test_template.py @@ -216,6 +216,7 @@ class TestTemplate(TreeEqualityTestCase): node39 = Template(wraptext("a"), [pgenh("1", " b ")]) node40 = Template(wraptext("a"), [pgenh("1", " b"), pgenh("2", " c")]) node41 = Template(wraptext("a"), [pgens("1", " b"), pgens("2", " c")]) + node42 = Template(wraptext("a"), [pgens("b", " \n")]) node1.add("e", "f", showkey=True) node2.add(2, "g", showkey=False) @@ -261,6 +262,7 @@ class TestTemplate(TreeEqualityTestCase): node39.add("1", "c") node40.add("3", "d") node41.add("3", "d") + node42.add("b", "hello") self.assertEqual("{{a|b=c|d|e=f}}", node1) self.assertEqual("{{a|b=c|d|g}}", node2) @@ -308,6 +310,7 @@ class TestTemplate(TreeEqualityTestCase): self.assertEqual("{{a|c}}", node39) self.assertEqual("{{a| b| c|d}}", node40) self.assertEqual("{{a|1= b|2= c|3= d}}", node41) + self.assertEqual("{{a|b=hello \n}}", node42) def test_remove(self): """test Template.remove()"""