Simplify code to replace shortcodes

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
This commit is contained in:
Kévin Commaille 2025-05-06 12:09:28 +02:00
parent c1c90a6b3b
commit a23985296e
No known key found for this signature in database
GPG key ID: F26F4BE20A08255B

View file

@ -72,11 +72,11 @@ def prefix_absolute_path_references(text, base_url):
""" """
return text.replace("](/", "]({}/".format(base_url)) return text.replace("](/", "]({}/".format(base_url))
def replace_match(text, match, replacement): def replace_match(match, replacement):
"""Replaces the regex match by the replacement in the text.""" """Replaces the regex match by the replacement in the text."""
return text[:match.start()] + replacement + text[match.end():] return match.string[:match.start()] + replacement + match.string[match.end():]
def replace_shortcode(text, shortcode): def replace_shortcode(shortcode):
"""Replaces the shortcode by a Markdown fallback in the text. """Replaces the shortcode by a Markdown fallback in the text.
The supported shortcodes are: The supported shortcodes are:
@ -87,32 +87,37 @@ def replace_shortcode(text, shortcode):
if shortcode['name'].startswith("/"): if shortcode['name'].startswith("/"):
# This is the end of the shortcode, just remove it. # This is the end of the shortcode, just remove it.
return replace_match(text, shortcode['match'], "") return replace_match(shortcode, "")
# Parse the parameters of the shortcode
params = {}
if shortcode['params']:
for param in shortcode_params_regex.finditer(shortcode['params']):
if param['key']:
params[param['key']] = param['value']
match shortcode['name']: match shortcode['name']:
case "boxes/note": case "boxes/note":
text = replace_match(text, shortcode['match'], "**NOTE:** ") return replace_match(shortcode, "**NOTE:** ")
case "boxes/rationale": case "boxes/rationale":
text = replace_match(text, shortcode['match'], "**RATIONALE:** ") return replace_match(shortcode, "**RATIONALE:** ")
case "boxes/warning": case "boxes/warning":
text = replace_match(text, shortcode['match'], "**WARNING:** ") return replace_match(shortcode, "**WARNING:** ")
case "added-in": case "added-in":
version = shortcode['params']['v'] version = params['v']
if not version: if not version:
raise ValueError("Missing parameter `v` for `added-in` shortcode") raise ValueError("Missing parameter `v` for `added-in` shortcode")
text = replace_match(text, shortcode['match'], f"**[Added in `v{version}`]** ") return replace_match(shortcode, f"**[Added in `v{version}`]** ")
case "changed-in": case "changed-in":
version = shortcode['params']['v'] version = params['v']
if not version: if not version:
raise ValueError("Missing parameter `v` for `changed-in` shortcode") raise ValueError("Missing parameter `v` for `changed-in` shortcode")
text = replace_match(text, shortcode['match'], f"**[Changed in `v{version}`]** ") return replace_match(shortcode, f"**[Changed in `v{version}`]** ")
case _: case _:
raise ValueError("Unknown shortcode", shortcode['name']) raise ValueError("Unknown shortcode", shortcode['name'])
return text
def find_and_replace_shortcodes(text): def find_and_replace_shortcodes(text):
"""Finds Hugo shortcodes and replaces them by a Markdown fallback. """Finds Hugo shortcodes and replaces them by a Markdown fallback.
@ -125,20 +130,8 @@ def find_and_replace_shortcodes(text):
# We use a `while` loop with `search` instead of a `for` loop with # We use a `while` loop with `search` instead of a `for` loop with
# `finditer`, because as soon as we start replacing text, the # `finditer`, because as soon as we start replacing text, the
# indices of the match are invalid. # indices of the match are invalid.
while match := shortcode_regex.search(text): while shortcode := shortcode_regex.search(text):
# Parse the parameters of the shortcode text = replace_shortcode(shortcode)
params = {}
if match['params']:
for param in shortcode_params_regex.finditer(match['params']):
if param['key']:
params[param['key']] = param['value']
shortcode = {
'name': match['name'],
'params': params,
'match': match,
}
text = replace_shortcode(text, shortcode)
return text return text