diff --git a/tests/test_identifier_parser.py b/tests/test_identifier_parser.py index 11d6c83..fa27504 100644 --- a/tests/test_identifier_parser.py +++ b/tests/test_identifier_parser.py @@ -1,3 +1,5 @@ +import pytest + from wikitools import identifier_parser @@ -75,6 +77,17 @@ def test__link(self): ): assert identifier_parser.extract_identifier(heading) == (identifier, 0) + @pytest.mark.parametrize( + "payload", + [ + {"heading": "### ::{ flag=TH }:: woah", "identifier": "woah"}, + {"heading": "### woah ::{ flag=TH }::", "identifier": "woah"}, + {"heading": "### ::{ flag=TH }:: Thailand vs. ::{ flag=NZ }:: New Zealand", "identifier": "thailand-vs.--new-zealand"}, + ] + ) + def test__flag(self, payload): + assert identifier_parser.extract_identifier(payload["heading"]) == (payload["identifier"], 0) + def test__custom(self): for line, identifier, pos in ( ('osu! is a free-to-win game.', None, 0), diff --git a/wikitools/identifier_parser.py b/wikitools/identifier_parser.py index 6e70c78..1326656 100644 --- a/wikitools/identifier_parser.py +++ b/wikitools/identifier_parser.py @@ -1,4 +1,5 @@ import typing +import re from wikitools import link_parser @@ -6,6 +7,9 @@ ESCAPEABLE_CHARS = { '\\', '(' , ')', '[', ']', '{', '}', '<', '>', '#', '*', '~', '`', '_', '-', '+', '|', '\'', '"', '@', '$', ';', ':', ',', '.' } +CONTAINER_REGEX = re.compile(r"::{.*?}::") + + # backslashes are sometimes used (perhaps unnecessarily) to avoid remark errors def unescape(s: str) -> str: unescaped = "" @@ -76,4 +80,10 @@ def extract_identifier( if k == len(links_on_line) - 1: heading += s[start:] - return ("-".join(word.lower() for word in unescape(heading).strip().split()), 0) + identifier = "-".join(word.lower() for word in unescape(heading).strip().split()) + + # headings can contain custom containers, such as flags + # TODO: maybe do this in a smarter way + identifier = re.sub(CONTAINER_REGEX, "", identifier).strip("-") + + return (identifier, 0) diff --git a/wikitools_cli/VERSION.py b/wikitools_cli/VERSION.py index 3c00bb4..3f755ed 100644 --- a/wikitools_cli/VERSION.py +++ b/wikitools_cli/VERSION.py @@ -1 +1 @@ -VERSION = "2.2.0" +VERSION = "2.2.1"