Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propose an escape character for the alias #678

Open
mgoonde opened this issue Nov 19, 2024 · 1 comment
Open

Propose an escape character for the alias #678

mgoonde opened this issue Nov 19, 2024 · 1 comment

Comments

@mgoonde
Copy link

mgoonde commented Nov 19, 2024

Hello,
i think it might be useful to have an escape character for the aliases. Here is an idea to use \ as the escape character, such that if |some_alias| is defined, typing \|some_alias| will escape it and write verbatim |some_alias| instead.

I defined the ALIAS_RE with a lookbehind for the \ character, then do the replacements in 2 steps: replace the non-escaped aliases with their definition; then replace the escaped aliases with their verbatim word, without the \.

What do you think?

--- a/ford/_markdown.py
+++ b/ford/_markdown.py
@@ -133,7 +133,8 @@ class AliasPreprocessor(Preprocessor):
     """Substitute text aliases of the form ``|foo|`` from a dictionary
     of aliases and their replacements"""
 
-    ALIAS_RE = re.compile(r"\|([^ ].*?[^ ]?)\|")
+    # pattern to match alias only if not preceded by `\`
+    ALIAS_RE = re.compile(r"(?<!\\)\|([^ ].*?[^ ]?)\|")
 
     def __init__(self, md: Markdown, aliases: Dict[str, str]):
         self.aliases = aliases
@@ -149,7 +150,11 @@ class AliasPreprocessor(Preprocessor):
 
     def run(self, lines: List[str]) -> List[str]:
         for line_num, line in enumerate(lines):
-            lines[line_num] = self.ALIAS_RE.sub(self._lookup, line)
+            # replace the real aliases
+            line = self.ALIAS_RE.sub(self._lookup, line)
+            # replace the escaped aliases verbatim, without the preceding escape char `\`
+            line = re.sub(r"\\(\|([^ ].*?[^ ]?)\|)", r"\g<1>",line)
+            lines[line_num]=line
         return lines
@ZedThree
Copy link
Member

Nice! Yes, I could see this being useful. Do you want to add a test and make a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants