diff --git a/CHANGELOG b/CHANGELOG index ece051e..d95b07c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,7 @@ v0.6 (unreleased): -- ... +- Updated Wikicode.matches() to recognize underscores as being equivalent + to spaces. (#216) v0.5.4 (released May 15, 2019): diff --git a/docs/changelog.rst b/docs/changelog.rst index 948c9c1..c46e8f1 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -7,7 +7,8 @@ v0.6 Unreleased (`changes `__): -- ... +- Updated Wikicode.matches() to recognize underscores as being equivalent + to spaces. (`#216 `_) v0.5.4 ------ diff --git a/mwparserfromhell/wikicode.py b/mwparserfromhell/wikicode.py index 0cc7276..840d8ed 100644 --- a/mwparserfromhell/wikicode.py +++ b/mwparserfromhell/wikicode.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2012-2017 Ben Kurtovic +# Copyright (C) 2012-2019 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 @@ -501,16 +501,16 @@ class Wikicode(StringMixIn): 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() + 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)): that = parse_anything(other).strip_code().strip() - return cmp(this, that) + return this == normalize(that) for obj in other: that = parse_anything(obj).strip_code().strip() - if cmp(this, that): + if this == normalize(that): return True return False diff --git a/tests/test_wikicode.py b/tests/test_wikicode.py index fceb272..307ee9a 100644 --- a/tests/test_wikicode.py +++ b/tests/test_wikicode.py @@ -312,7 +312,9 @@ class TestWikicode(TreeEqualityTestCase): """test Wikicode.matches()""" code1 = parse("Cleanup") code2 = parse("\nstub") - 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\n")) @@ -327,9 +329,15 @@ class TestWikicode(TreeEqualityTestCase): 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(code3.matches("")) - self.assertTrue(code3.matches("")) - 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("")) + self.assertTrue(code5.matches(("a", "b", ""))) def test_filter_family(self): """test the Wikicode.i?filter() family of functions"""