Skip to content

Commit

Permalink
fix redirect parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Walavouchey committed Nov 23, 2023
1 parent 3c78b7a commit cf9d2a7
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions wikitools/redirect_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
Redirects = typing.Dict[str, typing.Tuple[str, int]]


def unquote_and_trim(s):
s = s.strip()
if (s[0] == '"' and s[-1] == '"') or (s[0] == "'" and s[-1] == "'"):
return s[1:-1]
return s


def load_redirects(path: str) -> Redirects:
"""
Read redirects from a YAML dictionary file (done manually to preserve line numbers).
Expand All @@ -11,9 +18,9 @@ def load_redirects(path: str) -> Redirects:
redirects = {}
with open(path, 'r', encoding='utf-8') as fd:
for line_number, line in enumerate(fd, start=1):
split = line.split('"')
split = line.split(':')
try:
redirects[split[1]] = (split[3], line_number)
redirects[unquote_and_trim(split[0])] = (unquote_and_trim(split[1]), line_number)
except IndexError:
pass
return redirects

0 comments on commit cf9d2a7

Please sign in to comment.