Skip to content

Commit

Permalink
remove containers from identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
Walavouchey committed Dec 12, 2023
1 parent e101c24 commit 85e41e2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
13 changes: 13 additions & 0 deletions tests/test_identifier_parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from wikitools import identifier_parser


Expand Down Expand Up @@ -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),
Expand Down
12 changes: 11 additions & 1 deletion wikitools/identifier_parser.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import typing
import re

from wikitools import link_parser

ID_PREFIXES = {'#', 'id='}

ESCAPEABLE_CHARS = { '\\', '(' , ')', '[', ']', '{', '}', '<', '>', '#', '*', '~', '`', '_', '-', '+', '|', '\'', '"', '@', '$', ';', ':', ',', '.' }

CONTAINER_REGEX = re.compile(r"::{.*?}::")


# backslashes are sometimes used (perhaps unnecessarily) to avoid remark errors
def unescape(s: str) -> str:
unescaped = ""
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion wikitools_cli/VERSION.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = "2.2.0"
VERSION = "2.2.1"

0 comments on commit 85e41e2

Please sign in to comment.