Commit f4adb11f authored by di68kap's avatar di68kap
Browse files

- DHParser/transform.py: has_content: rules for matching content changed:...

- DHParser/transform.py: has_content: rules for matching content changed: regexp must not match the complete content, nur just the beginning
parent 28abee18
...@@ -411,7 +411,14 @@ def is_one_of(context: List[Node], tag_name_set: AbstractSet[str]) -> bool: ...@@ -411,7 +411,14 @@ def is_one_of(context: List[Node], tag_name_set: AbstractSet[str]) -> bool:
@transformation_factory @transformation_factory
def has_content(context: List[Node], regexp: str) -> bool: def has_content(context: List[Node], regexp: str) -> bool:
"""Checks a node's content against a regular expression.""" """
Checks a node's content against a regular expression.
In contrast to ``re.match`` the regular expression must match the complete
string and not just the beginning of the string to succeed!
"""
if not regexp.endswith('$'):
regexp += "$"
return bool(re.match(regexp, context[-1].content)) return bool(re.match(regexp, context[-1].content))
......
...@@ -26,7 +26,7 @@ sys.path.extend(['../', './']) ...@@ -26,7 +26,7 @@ sys.path.extend(['../', './'])
from DHParser.syntaxtree import mock_syntax_tree from DHParser.syntaxtree import mock_syntax_tree
from DHParser.transform import traverse, reduce_single_child, remove_whitespace, \ from DHParser.transform import traverse, reduce_single_child, remove_whitespace, \
traverse_locally, collapse, lstrip, rstrip traverse_locally, collapse, lstrip, rstrip, remove_content
class TestRemoval: class TestRemoval:
...@@ -71,6 +71,14 @@ class TestRemoval: ...@@ -71,6 +71,14 @@ class TestRemoval:
rstrip([cst]) rstrip([cst])
assert cst.as_sxpr().find(":Whitespace") < 0, cst.as_sxpr() assert cst.as_sxpr().find(":Whitespace") < 0, cst.as_sxpr()
def test_remove_content(self):
cst = mock_syntax_tree('(BelegLemma (:Series (:RegExp "#") (LAT_WORT (:RegExp "facitergula"))))')
remove_content([cst], '#')
assert cst.content == "#facitergula", str(cst.content)
reduce_single_child([cst])
remove_content([cst], '#')
assert cst.content == "facitergula"
class TestConditionalTransformations: class TestConditionalTransformations:
"""Tests conditional transformations.""" """Tests conditional transformations."""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment