Browse Source

Wikicode.matches() now accepts tuples (closes #48)

tags/v0.3.3
Ben Kurtovic 10 years ago
parent
commit
5e6c994c2c
4 changed files with 25 additions and 6 deletions
  1. +2
    -0
      CHANGELOG
  2. +3
    -0
      docs/changelog.rst
  3. +12
    -6
      mwparserfromhell/wikicode.py
  4. +8
    -0
      tests/test_wikicode.py

+ 2
- 0
CHANGELOG View File

@@ -2,6 +2,8 @@ v0.4 (unreleased):

- 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.
- 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):



+ 3
- 0
docs/changelog.rst View File

@@ -10,6 +10,9 @@ Unreleased
- The *matches* argument of :py:class:`Wikicode's <.Wikicode>`
: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: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
------


+ 12
- 6
mwparserfromhell/wikicode.py View File

@@ -362,16 +362,22 @@ class Wikicode(StringMixIn):
"""Do a loose equivalency test suitable for comparing page names.

*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"): ...``.
"""
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()
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()
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,
forcetype=None):


+ 8
- 0
tests/test_wikicode.py View File

@@ -242,6 +242,7 @@ class TestWikicode(TreeEqualityTestCase):
"""test Wikicode.matches()"""
code1 = parse("Cleanup")
code2 = parse("\nstub<!-- TODO: make more specific -->")
code3 = parse("")
self.assertTrue(code1.matches("Cleanup"))
self.assertTrue(code1.matches("cleanup"))
self.assertTrue(code1.matches(" cleanup\n"))
@@ -250,6 +251,13 @@ class TestWikicode(TreeEqualityTestCase):
self.assertTrue(code2.matches("stub"))
self.assertTrue(code2.matches("Stub<!-- no, it's fine! -->"))
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):
"""test the Wikicode.i?filter() family of functions"""


Loading…
Cancel
Save