diff --git a/.gitignore b/.gitignore index ef9df96..6b3069e 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,10 @@ __pycache__/ # C extensions *.so +# Local data and mappings +data/ +mappings/ + # Distribution / packaging .Python build/ diff --git a/thebeast/contrib/jinja_custom_filters/__init__.py b/thebeast/contrib/jinja_custom_filters/__init__.py new file mode 100644 index 0000000..e85e2f3 --- /dev/null +++ b/thebeast/contrib/jinja_custom_filters/__init__.py @@ -0,0 +1,16 @@ +import re + +def regex_search(string, pattern): + # Search for a pattern in the given string. + + match = re.search(pattern, string) + return match.group(0) if match else None + +def regex_match(string, pattern): + #Check if the entire string matches the pattern. + return bool(re.fullmatch(pattern, string)) + +def regex_replace(string, pattern, replacement): + #Replace occurrences of the pattern with the replacement in the string. + + return re.sub(pattern, replacement, string) diff --git a/thebeast/digest/resolvers.py b/thebeast/digest/resolvers.py index f37a74e..9c1fc31 100644 --- a/thebeast/digest/resolvers.py +++ b/thebeast/digest/resolvers.py @@ -4,6 +4,8 @@ from jinja2 import Environment, BaseLoader, select_autoescape +from thebeast.contrib.jinja_custom_filters import regex_search, regex_match, regex_replace + from followthemoney.schema import Schema # type: ignore from thebeast.contrib.ftm_ext.rigged_entity_proxy import StrProxy @@ -11,6 +13,9 @@ # TODO: expose jmespath to templates as a filter? jinja_env = Environment(loader=BaseLoader(), autoescape=select_autoescape()) +jinja_env.filters['regex_search'] = regex_search +jinja_env.filters['regex_match'] = regex_match +jinja_env.filters['regex_replace'] = regex_replace @dataclass