@@ -11,6 +11,8 @@ v0.3 (unreleased): | |||||
remove() now accepts other Wikicode objects and strings representing parts of | remove() now accepts other Wikicode objects and strings representing parts of | ||||
wikitext, instead of just nodes. These methods also make all possible | wikitext, instead of just nodes. These methods also make all possible | ||||
substitutions instead of just one. | substitutions instead of just one. | ||||
- Renamed Template.has_param() to has() for consistency with Template's other | |||||
methods; has_param() is now an alias. | |||||
- The C tokenizer extension now works on Python 3 in addition to Python 2.7. | - The C tokenizer extension now works on Python 3 in addition to Python 2.7. | ||||
- Various fixes and cleanup. | - Various fixes and cleanup. | ||||
@@ -90,7 +90,7 @@ whitespace:: | |||||
>>> text = "{{cleanup}} '''Foo''' is a [[bar]]. {{uncategorized}}" | >>> text = "{{cleanup}} '''Foo''' is a [[bar]]. {{uncategorized}}" | ||||
>>> code = mwparserfromhell.parse(text) | >>> code = mwparserfromhell.parse(text) | ||||
>>> for template in code.filter_templates(): | >>> for template in code.filter_templates(): | ||||
... if template.name.matches("Cleanup") and not template.has_param("date"): | |||||
... if template.name.matches("Cleanup") and not template.has("date"): | |||||
... template.add("date", "July 2012") | ... template.add("date", "July 2012") | ||||
... | ... | ||||
>>> print code | >>> print code | ||||
@@ -16,11 +16,14 @@ Unreleased | |||||
if you rely on any filter() methods being non-recursive by default.** | if you rely on any filter() methods being non-recursive by default.** | ||||
- Added a :py:meth:`.matches` method to :py:class:`~.Wikicode` for | - Added a :py:meth:`.matches` method to :py:class:`~.Wikicode` for | ||||
page/template name comparisons. | page/template name comparisons. | ||||
- The *obj* param of :py:meth:`Wikicode.insert_before <.insert_before>`, | |||||
:py:meth:`~.insert_after`, :py:meth:`~.replace`, and :py:meth:`~.remove` now | |||||
accepts :py:class:`~.Wikicode` objects and strings representing parts of | |||||
wikitext, instead of just nodes. These methods also make all possible | |||||
substitutions instead of just one. | |||||
- The *obj* param of :py:meth:`Wikicode.insert_before() <.insert_before>`, | |||||
:py:meth:`~.insert_after`, :py:meth:`~.Wikicode.replace`, and | |||||
:py:meth:`~.Wikicode.remove` now accepts :py:class:`~.Wikicode` objects and | |||||
strings representing parts of wikitext, instead of just nodes. These methods | |||||
also make all possible substitutions instead of just one. | |||||
- Renamed :py:meth:`Template.has_param() <.has_param>` to | |||||
:py:meth:`~.Template.has` for consistency with :py:class:`~.Template`\ 's | |||||
other methods; :py:meth:`~.has_param` is now an alias. | |||||
- The C tokenizer extension now works on Python 3 in addition to Python 2.7. | - The C tokenizer extension now works on Python 3 in addition to Python 2.7. | ||||
- Various fixes and cleanup. | - Various fixes and cleanup. | ||||
@@ -58,7 +58,7 @@ names, which takes care of capitalization and whitespace:: | |||||
>>> text = "{{cleanup}} '''Foo''' is a [[bar]]. {{uncategorized}}" | >>> text = "{{cleanup}} '''Foo''' is a [[bar]]. {{uncategorized}}" | ||||
>>> code = mwparserfromhell.parse(text) | >>> code = mwparserfromhell.parse(text) | ||||
>>> for template in code.filter_templates(): | >>> for template in code.filter_templates(): | ||||
... if template.name.matches("Cleanup") and not template.has_param("date"): | |||||
... if template.name.matches("Cleanup") and not template.has("date"): | |||||
... template.add("date", "July 2012") | ... template.add("date", "July 2012") | ||||
... | ... | ||||
>>> print code | >>> print code | ||||
@@ -164,7 +164,7 @@ class Template(Node): | |||||
def name(self, value): | def name(self, value): | ||||
self._name = parse_anything(value) | self._name = parse_anything(value) | ||||
def has_param(self, name, ignore_empty=True): | |||||
def has(self, name, ignore_empty=True): | |||||
"""Return ``True`` if any parameter in the template is named *name*. | """Return ``True`` if any parameter in the template is named *name*. | ||||
With *ignore_empty*, ``False`` will be returned even if the template | With *ignore_empty*, ``False`` will be returned even if the template | ||||
@@ -180,6 +180,9 @@ class Template(Node): | |||||
return True | return True | ||||
return False | return False | ||||
has_param = lambda self, *args, **kwargs: self.has(*args, **kwargs) | |||||
has_param.__doc__ = "Alias for :py:meth:`has`." | |||||
def get(self, name): | def get(self, name): | ||||
"""Get the parameter whose name is *name*. | """Get the parameter whose name is *name*. | ||||
@@ -226,7 +229,7 @@ class Template(Node): | |||||
name, value = parse_anything(name), parse_anything(value) | name, value = parse_anything(name), parse_anything(value) | ||||
self._surface_escape(value, "|") | self._surface_escape(value, "|") | ||||
if self.has_param(name): | |||||
if self.has(name): | |||||
self.remove(name, keep_field=True) | self.remove(name, keep_field=True) | ||||
existing = self.get(name) | existing = self.get(name) | ||||
if showkey is not None: | if showkey is not None: | ||||
@@ -90,7 +90,7 @@ class TestDocs(unittest.TestCase): | |||||
text = "{{cleanup}} '''Foo''' is a [[bar]]. {{uncategorized}}" | text = "{{cleanup}} '''Foo''' is a [[bar]]. {{uncategorized}}" | ||||
code = mwparserfromhell.parse(text) | code = mwparserfromhell.parse(text) | ||||
for template in code.filter_templates(): | for template in code.filter_templates(): | ||||
if template.name.matches("Cleanup") and not template.has_param("date"): | |||||
if template.name.matches("Cleanup") and not template.has("date"): | |||||
template.add("date", "July 2012") | template.add("date", "July 2012") | ||||
res = "{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{uncategorized}}" | res = "{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{uncategorized}}" | ||||
self.assertPrint(code, res) | self.assertPrint(code, res) | ||||
@@ -115,23 +115,23 @@ class TestTemplate(TreeEqualityTestCase): | |||||
self.assertEqual([], node1.params) | self.assertEqual([], node1.params) | ||||
self.assertIs(plist, node2.params) | self.assertIs(plist, node2.params) | ||||
def test_has_param(self): | |||||
"""test Template.has_param()""" | |||||
def test_has(self): | |||||
"""test Template.has()""" | |||||
node1 = Template(wraptext("foobar")) | node1 = Template(wraptext("foobar")) | ||||
node2 = Template(wraptext("foo"), | node2 = Template(wraptext("foo"), | ||||
[pgenh("1", "bar"), pgens("\nabc ", "def")]) | [pgenh("1", "bar"), pgens("\nabc ", "def")]) | ||||
node3 = Template(wraptext("foo"), | node3 = Template(wraptext("foo"), | ||||
[pgenh("1", "a"), pgens("b", "c"), pgens("1", "d")]) | [pgenh("1", "a"), pgens("b", "c"), pgens("1", "d")]) | ||||
node4 = Template(wraptext("foo"), [pgenh("1", "a"), pgens("b", " ")]) | node4 = Template(wraptext("foo"), [pgenh("1", "a"), pgens("b", " ")]) | ||||
self.assertFalse(node1.has_param("foobar")) | |||||
self.assertTrue(node2.has_param(1)) | |||||
self.assertTrue(node2.has_param("abc")) | |||||
self.assertFalse(node2.has_param("def")) | |||||
self.assertTrue(node3.has_param("1")) | |||||
self.assertTrue(node3.has_param(" b ")) | |||||
self.assertFalse(node4.has_param("b")) | |||||
self.assertTrue(node3.has_param("b", False)) | |||||
self.assertTrue(node4.has_param("b", False)) | |||||
self.assertFalse(node1.has("foobar")) | |||||
self.assertTrue(node2.has(1)) | |||||
self.assertTrue(node2.has("abc")) | |||||
self.assertFalse(node2.has("def")) | |||||
self.assertTrue(node3.has("1")) | |||||
self.assertTrue(node3.has(" b ")) | |||||
self.assertFalse(node4.has("b")) | |||||
self.assertTrue(node3.has("b", False)) | |||||
self.assertTrue(node4.has("b", False)) | |||||
def test_get(self): | def test_get(self): | ||||
"""test Template.get()""" | """test Template.get()""" | ||||