Compare commits

..

1 commit

Author SHA1 Message Date
Kévin Commaille d0aea81e4f
Merge 73b1936dae into fca171427f 2025-04-22 17:33:13 +01:00

View file

@ -37,10 +37,10 @@ api_dir = os.path.join(os.path.dirname(scripts_dir), "data", "api")
# A shortcode is defined as (newlines and whitespaces for presentation purpose): # A shortcode is defined as (newlines and whitespaces for presentation purpose):
# #
# {{% # {{%
# <zero or more whitespaces> # <one or more whitespaces>
# <name of shortcode> # <name of shortcode>
# (optional <one or more whitespaces><list of parameters>) # <one or more whitespaces>
# <zero or more whitespaces> # (optional <list of parameters><one or more whitespaces>)
# %}} # %}}
# #
# With: # With:
@ -48,14 +48,9 @@ api_dir = os.path.join(os.path.dirname(scripts_dir), "data", "api")
# * <name of shortcode>: any word character and `-` and `/`. # * <name of shortcode>: any word character and `-` and `/`.
# * <list of parameters>: any character except `}`, must not start or end with a # * <list of parameters>: any character except `}`, must not start or end with a
# whitespace. # whitespace.
shortcode_regex = re.compile(r"""\{\{\% # {{% shortcode_regex = re.compile(r"\{\{\%\s+(?P<name>[\w\/-]+)\s+(?:(?P<params>[^\s\}][^\}]+[^\s\}])\s+)?\%\}\}", re.ASCII)
\s* # zero or more whitespaces
(?P<name>[\w/-]+) # name of shortcode
(?:\s+(?P<params>[^\s\}][^\}]+[^\s\}]))? # optional list of parameters
\s* # zero or more whitespaces
\%\}\} # %}}""", re.ASCII | re.VERBOSE)
# Parses the parameters of a Hugo shortcode. # Parses the parameters of a Hugo shortcode.
# #
# For simplicity, this currently only supports the `key="value"` format. # For simplicity, this currently only supports the `key="value"` format.
shortcode_params_regex = re.compile(r"(?P<key>\w+)=\"(?P<value>[^\"]+)\"", re.ASCII) shortcode_params_regex = re.compile(r"(?P<key>\w+)=\"(?P<value>[^\"]+)\"", re.ASCII)
@ -72,13 +67,13 @@ def prefix_absolute_path_references(text, base_url):
""" """
return text.replace("](/", "]({}/".format(base_url)) return text.replace("](/", "]({}/".format(base_url))
def replace_match(match, replacement): def replace_match(text, match, replacement):
"""Replaces the regex match by the replacement in the text.""" """Replaces the regex match by the replacement in the text."""
return match.string[:match.start()] + replacement + match.string[match.end():] return text[:match.start()] + replacement + text[match.end():]
def replace_shortcode(shortcode): def replace_shortcode(text, 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:
* boxes/note, boxes/rationale, boxes/warning * boxes/note, boxes/rationale, boxes/warning
@ -87,41 +82,36 @@ def replace_shortcode(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(shortcode, "") return replace_match(text, shortcode['match'], "")
# 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":
return replace_match(shortcode, "**NOTE:** ") text = replace_match(text, shortcode['match'], "**NOTE:** ")
case "boxes/rationale": case "boxes/rationale":
return replace_match(shortcode, "**RATIONALE:** ") text = replace_match(text, shortcode['match'], "**RATIONALE:** ")
case "boxes/warning": case "boxes/warning":
return replace_match(shortcode, "**WARNING:** ") text = replace_match(text, shortcode['match'], "**WARNING:** ")
case "added-in": case "added-in":
version = params['v'] version = shortcode['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")
return replace_match(shortcode, f"**[Added in `v{version}`]** ") text = replace_match(text, shortcode['match'], f"**[Added in `v{version}`]** ")
case "changed-in": case "changed-in":
version = params['v'] version = shortcode['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")
return replace_match(shortcode, f"**[Changed in `v{version}`]** ") text = replace_match(text, shortcode['match'], 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.
The supported shortcodes are: The supported shortcodes are:
* boxes/note, boxes/rationale, boxes/warning * boxes/note, boxes/rationale, boxes/warning
@ -130,18 +120,30 @@ 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 shortcode := shortcode_regex.search(text): while match := shortcode_regex.search(text):
text = replace_shortcode(shortcode) # Parse the parameters of the 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
def edit_descriptions(node, base_url): def edit_descriptions(node, base_url):
"""Finds description nodes and apply fixes to them. """Finds description nodes and apply fixes to them.
The fixes that are applied are: The fixes that are applied are:
* Make links absolute * Make links absolute
* Replace Hugo shortcodes * Replace shortcodes
""" """
if isinstance(node, dict): if isinstance(node, dict):
for key in node: for key in node: