[gi-docgen/link-text] utils: Allow links to have custom text




commit 154c548eb7a65003bf44886d61a19c5754627b09
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Sun Aug 22 18:37:32 2021 +0100

    utils: Allow links to have custom text
    
    Use the idiomatic Markdown form:
    
        [text][link]
    
    format, where "link" is our homegrown link syntax, and text is a subset
    of word characters, spaces, and punctuation marks.
    
    Fixes: #100

 gidocgen/utils.py | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
---
diff --git a/gidocgen/utils.py b/gidocgen/utils.py
index 6b91ee6..1332f36 100644
--- a/gidocgen/utils.py
+++ b/gidocgen/utils.py
@@ -66,6 +66,7 @@ CODEBLOCK_END_RE = re.compile(
 
 LINK_RE = re.compile(
     r'''
+    (?P<text>\[ [\w\s,\-_:]+ \])?
     \[
     (`)?
     (?P<fragment>[\w]+)
@@ -196,6 +197,7 @@ class LinkGenerator:
         self._fragment = kwargs.get('fragment', '')
         self._endpoint = kwargs.get('endpoint', '')
         self._no_link = kwargs.get('no_link', False)
+        self._alt_text = kwargs.get('text')
 
         assert self._namespace is not None
 
@@ -556,7 +558,9 @@ class LinkGenerator:
 
     @property
     def text(self):
-        if self._fragment in ['alias', 'class', 'const', 'enum', 'error', 'flags', 'iface', 'struct']:
+        if self._alt_text is not None:
+            return self._alt_text[1:len(self._alt_text) - 1]
+        elif self._fragment in ['alias', 'class', 'const', 'enum', 'error', 'flags', 'iface', 'struct']:
             return f"<code>{self._type}</code>"
         elif self._fragment == 'property':
             return f"<code>{self._type}:{self._property_name}</code>"
@@ -661,12 +665,13 @@ def preprocess_docs(text, namespace, summary=False, md=None, extensions=[], plai
             for m in LINK_RE.finditer(line, idx):
                 fragment = m.group('fragment')
                 endpoint = m.group('endpoint')
+                text = m.group('text')
                 start = m.start()
                 end = m.end()
                 link = LinkGenerator(line=line, start=start, end=end,
                                      namespace=namespace,
                                      fragment=fragment, endpoint=endpoint,
-                                     no_link=summary)
+                                     no_link=summary, text=text)
                 left_pad = line[idx:start]
                 replacement = re.sub(LINK_RE, str(link), line[start:end])
                 new_line.append(left_pad)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]