@@ -1,6 +1,7 @@ | |||||
v0.6 (unreleased): | v0.6 (unreleased): | ||||
- ... | |||||
- Updated Wikicode.matches() to recognize underscores as being equivalent | |||||
to spaces. (#216) | |||||
v0.5.4 (released May 15, 2019): | v0.5.4 (released May 15, 2019): | ||||
@@ -7,7 +7,8 @@ v0.6 | |||||
Unreleased | Unreleased | ||||
(`changes <https://github.com/earwig/mwparserfromhell/compare/v0.5.4...develop>`__): | (`changes <https://github.com/earwig/mwparserfromhell/compare/v0.5.4...develop>`__): | ||||
- ... | |||||
- Updated Wikicode.matches() to recognize underscores as being equivalent | |||||
to spaces. (`#216 <https://github.com/earwig/mwparserfromhell/issues/216>`_) | |||||
v0.5.4 | v0.5.4 | ||||
------ | ------ | ||||
@@ -1,6 +1,6 @@ | |||||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||||
# | # | ||||
# Copyright (C) 2012-2017 Ben Kurtovic <ben.kurtovic@gmail.com> | |||||
# Copyright (C) 2012-2019 Ben Kurtovic <ben.kurtovic@gmail.com> | |||||
# | # | ||||
# Permission is hereby granted, free of charge, to any person obtaining a copy | # Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
# of this software and associated documentation files (the "Software"), to deal | # of this software and associated documentation files (the "Software"), to deal | ||||
@@ -501,16 +501,16 @@ class Wikicode(StringMixIn): | |||||
letter's case is normalized. Typical usage is | 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() | |||||
normalize = lambda s: (s[0].upper() + s[1:]).replace("_", " ") if s else s | |||||
this = normalize(self.strip_code().strip()) | |||||
if isinstance(other, (str, bytes, Wikicode, Node)): | if isinstance(other, (str, bytes, Wikicode, Node)): | ||||
that = parse_anything(other).strip_code().strip() | that = parse_anything(other).strip_code().strip() | ||||
return cmp(this, that) | |||||
return this == normalize(that) | |||||
for obj in other: | for obj in other: | ||||
that = parse_anything(obj).strip_code().strip() | that = parse_anything(obj).strip_code().strip() | ||||
if cmp(this, that): | |||||
if this == normalize(that): | |||||
return True | return True | ||||
return False | return False | ||||
@@ -312,7 +312,9 @@ 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("") | |||||
code3 = parse("Hello world!") | |||||
code4 = parse("World,_hello?") | |||||
code5 = 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")) | ||||
@@ -327,9 +329,15 @@ class TestWikicode(TreeEqualityTestCase): | |||||
self.assertFalse(code2.matches(["StuB", "sTUb", "foobar"])) | self.assertFalse(code2.matches(["StuB", "sTUb", "foobar"])) | ||||
self.assertTrue(code2.matches(("StuB", "sTUb", "foo", "bar", "Stub"))) | self.assertTrue(code2.matches(("StuB", "sTUb", "foo", "bar", "Stub"))) | ||||
self.assertTrue(code2.matches(["StuB", "sTUb", "foo", "bar", "Stub"])) | self.assertTrue(code2.matches(["StuB", "sTUb", "foo", "bar", "Stub"])) | ||||
self.assertTrue(code3.matches("")) | |||||
self.assertTrue(code3.matches("<!-- nothing -->")) | |||||
self.assertTrue(code3.matches(("a", "b", ""))) | |||||
self.assertTrue(code3.matches("hello world!")) | |||||
self.assertTrue(code3.matches("hello_world!")) | |||||
self.assertFalse(code3.matches("hello__world!")) | |||||
self.assertTrue(code4.matches("World,_hello?")) | |||||
self.assertTrue(code4.matches("World, hello?")) | |||||
self.assertFalse(code4.matches("World, hello?")) | |||||
self.assertTrue(code5.matches("")) | |||||
self.assertTrue(code5.matches("<!-- nothing -->")) | |||||
self.assertTrue(code5.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""" | ||||