@@ -2,6 +2,8 @@ v0.4 (unreleased): | |||||
- The 'matches' argument of Wikicode's filter methods now accepts a function | - The 'matches' argument of Wikicode's filter methods now accepts a function | ||||
(taking one argument, a Node, and returning a bool) in addition to a regex. | (taking one argument, a Node, and returning a bool) in addition to a regex. | ||||
- Wikicode.matches() now accepts a tuple of strings/Wikicode objects instead of | |||||
just a single string or Wikicode. | |||||
v0.3.2 (released September 1, 2013): | v0.3.2 (released September 1, 2013): | ||||
@@ -10,6 +10,9 @@ Unreleased | |||||
- The *matches* argument of :py:class:`Wikicode's <.Wikicode>` | - The *matches* argument of :py:class:`Wikicode's <.Wikicode>` | ||||
:py:meth:`.filter` methods now accepts a function (taking one argument, a | :py:meth:`.filter` methods now accepts a function (taking one argument, a | ||||
:py:class:`.Node`, and returning a bool) in addition to a regex. | :py:class:`.Node`, and returning a bool) in addition to a regex. | ||||
- :py:meth:`.Wikicode.matches` now accepts a tuple of | |||||
strings/:py:class:`.Wikicode` objects instead of just a single string or | |||||
:py:class:`.Wikicode`. | |||||
v0.3.2 | v0.3.2 | ||||
------ | ------ | ||||
@@ -362,16 +362,22 @@ class Wikicode(StringMixIn): | |||||
"""Do a loose equivalency test suitable for comparing page names. | """Do a loose equivalency test suitable for comparing page names. | ||||
*other* can be any string-like object, including | *other* can be any string-like object, including | ||||
:py:class:`~.Wikicode`. This operation is symmetric; both sides are | |||||
adjusted. Specifically, whitespace and markup is stripped and the first | |||||
letter's case is normalized. Typical usage is | |||||
:py:class:`~.Wikicode`, or a tuple of these. This operation is | |||||
symmetric; both sides are adjusted. Specifically, whitespace and markup | |||||
is stripped and the first letter's case is normalized. Typical usage is | |||||
``if template.name.matches("stub"): ...``. | ``if template.name.matches("stub"): ...``. | ||||
""" | """ | ||||
cmp = lambda a, b: (a[0].upper() + a[1:] == b[0].upper() + b[1:] | |||||
if a and b else a == b) | |||||
this = self.strip_code().strip() | this = self.strip_code().strip() | ||||
if isinstance(other, tuple): | |||||
for obj in other: | |||||
that = parse_anything(obj).strip_code().strip() | |||||
if cmp(this, that): | |||||
return True | |||||
return False | |||||
that = parse_anything(other).strip_code().strip() | that = parse_anything(other).strip_code().strip() | ||||
if not this or not that: | |||||
return this == that | |||||
return this[0].upper() + this[1:] == that[0].upper() + that[1:] | |||||
return cmp(this, that) | |||||
def ifilter(self, recursive=True, matches=None, flags=FLAGS, | def ifilter(self, recursive=True, matches=None, flags=FLAGS, | ||||
forcetype=None): | forcetype=None): | ||||
@@ -242,6 +242,7 @@ class TestWikicode(TreeEqualityTestCase): | |||||
"""test Wikicode.matches()""" | """test Wikicode.matches()""" | ||||
code1 = parse("Cleanup") | code1 = parse("Cleanup") | ||||
code2 = parse("\nstub<!-- TODO: make more specific -->") | code2 = parse("\nstub<!-- TODO: make more specific -->") | ||||
code3 = parse("") | |||||
self.assertTrue(code1.matches("Cleanup")) | self.assertTrue(code1.matches("Cleanup")) | ||||
self.assertTrue(code1.matches("cleanup")) | self.assertTrue(code1.matches("cleanup")) | ||||
self.assertTrue(code1.matches(" cleanup\n")) | self.assertTrue(code1.matches(" cleanup\n")) | ||||
@@ -250,6 +251,13 @@ class TestWikicode(TreeEqualityTestCase): | |||||
self.assertTrue(code2.matches("stub")) | self.assertTrue(code2.matches("stub")) | ||||
self.assertTrue(code2.matches("Stub<!-- no, it's fine! -->")) | self.assertTrue(code2.matches("Stub<!-- no, it's fine! -->")) | ||||
self.assertFalse(code2.matches("StuB")) | self.assertFalse(code2.matches("StuB")) | ||||
self.assertTrue(code1.matches(("cleanup", "stub"))) | |||||
self.assertTrue(code2.matches(("cleanup", "stub"))) | |||||
self.assertFalse(code2.matches(("StuB", "sTUb", "foobar"))) | |||||
self.assertTrue(code2.matches(("StuB", "sTUb", "foo", "bar", "Stub"))) | |||||
self.assertTrue(code3.matches("")) | |||||
self.assertTrue(code3.matches("<!-- nothing -->")) | |||||
self.assertTrue(code3.matches(("a", "b", ""))) | |||||
def test_filter_family(self): | def test_filter_family(self): | ||||
"""test the Wikicode.i?filter() family of functions""" | """test the Wikicode.i?filter() family of functions""" | ||||