From ad0c70bac19e1ff6f4557c45ddf0fe5c87518481 Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Wed, 22 Jun 2022 19:08:42 -0400 Subject: docs: make the refman linkification slightly prettier for codeblocks If we link to ```meson [[#function]]('posarg') ``` then the ideal linkification would operate on "function" in the formatted text: ``` function('posarg') ``` Instead, it operated on "function()" in the formatted text: ``` function()('posarg') ``` Fix this by detecting the next character after the reference, and skipping the usual "automatically append the () for you" stage if it already has that opening parenthesis. --- docs/extensions/refman_links.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'docs') diff --git a/docs/extensions/refman_links.py b/docs/extensions/refman_links.py index 857d2cb..3aec368 100644 --- a/docs/extensions/refman_links.py +++ b/docs/extensions/refman_links.py @@ -52,9 +52,9 @@ class RefmanLinksExtension(Extension): with valid links to the correct URL. To reference objects / types use the [[@object]] syntax. ''' - link_regex = re.compile(r'\[\[#?@?([ \n\t]*[a-zA-Z0-9_]+[ \n\t]*\.)*[ \n\t]*[a-zA-Z0-9_]+[ \n\t]*\]\]', re.MULTILINE) + link_regex = re.compile(r'(\[\[#?@?([ \n\t]*[a-zA-Z0-9_]+[ \n\t]*\.)*[ \n\t]*[a-zA-Z0-9_]+[ \n\t]*\]\])(.)?', re.MULTILINE) for m in link_regex.finditer(page.formatted_contents): - i = m.group() + i = m.group(1) obj_id: str = i[2:-2] obj_id = re.sub(r'[ \n\t]', '', obj_id) # Remove whitespaces @@ -77,12 +77,15 @@ class RefmanLinksExtension(Extension): text = obj_id if text.startswith('@'): text = text[1:] + elif in_code_block: + if m.group(3) != '(': + text = text + '()' else: text = text + '()' if not in_code_block: text = f'{text}' link = f'{text}' - page.formatted_contents = page.formatted_contents.replace(i, link) + page.formatted_contents = page.formatted_contents.replace(i, link, 1) def setup(self) -> None: super().setup() -- cgit v1.1