Parcourir la source

Make Wikicode.matches() treat _ and space as equivalent (fixes #216)

tags/v0.6
Ben Kurtovic il y a 4 ans
Parent
révision
6136b1b205
4 fichiers modifiés avec 22 ajouts et 12 suppressions
  1. +2
    -1
      CHANGELOG
  2. +2
    -1
      docs/changelog.rst
  3. +6
    -6
      mwparserfromhell/wikicode.py
  4. +12
    -4
      tests/test_wikicode.py

+ 2
- 1
CHANGELOG Voir le fichier

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



+ 2
- 1
docs/changelog.rst Voir le fichier

@@ -7,7 +7,8 @@ v0.6
Unreleased
(`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
------


+ 6
- 6
mwparserfromhell/wikicode.py Voir le fichier

@@ -1,6 +1,6 @@
# -*- 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
# 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



+ 12
- 4
tests/test_wikicode.py Voir le fichier

@@ -312,7 +312,9 @@ class TestWikicode(TreeEqualityTestCase):
"""test Wikicode.matches()"""
code1 = parse("Cleanup")
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\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("<!-- 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):
"""test the Wikicode.i?filter() family of functions"""


Chargement…
Annuler
Enregistrer