add link-lookups into mdwn_inline.py and pandoc_img.py
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Thu, 1 Jun 2023 14:34:43 +0000 (15:34 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Thu, 1 Jun 2023 14:34:47 +0000 (15:34 +0100)
attempting to be able to do cross-referencing automatically,
involves headers "# " automatically gaining an <a name="...." /> tag

openpower/mdwn_inline.py
openpower/pandoc_img.py

index efabb12ce55595a8abb78ec595ae1fa732067574..2c03ae22c917e9d6eeca01a5e28d127c1d337787 100755 (executable)
@@ -48,6 +48,17 @@ def recursive_inline(f, input_name, depth):
             o.write(line)
             o.write("\\itemsep -0.3em\n")
             continue
+        # find headings and create name-refs, including filename
+        if line.startswith("#"):
+            iname = input_name.split("/")
+            if iname[0] == 'openpower': iname.pop(0)
+            l = line.strip().split(" ")[1:3]
+            l = map(lambda x: ''.join(filter(str.isalnum, x)), l)
+            l = map(str.lower, l)
+            l = filter(lambda x:x, l)
+            l = list(dict.fromkeys(iname + list(l)))
+            l = '_'.join(l)
+            line = '%s <a name="%s"> </>\n' % (line.strip(), l)
         # find actual inlines
         if not line.startswith("[[!inline"):
             o.write(line)
index 5fb6745bb10dd5b1e6a4b10dc90d83902be691f7..f2f19f39fbd740249b593eacd7ca1bdc4d25f4b4 100755 (executable)
@@ -59,7 +59,10 @@ def inlinenotes(k, v, f, meta):
         find_brack = v.find(']]')
         if find_brack == -1:
             return
+        hashtag = ""
         link = v[2:find_brack]
+        rest = link.split("#") # can be joined up again with '#'.join(rest)
+        link = rest[0]
         out.write("     link %s\n" % link)
         lookups = {'sv': 'Scalable Vectors for Power ISA',
                    'SV|sv': 'Scalable Vectors for Power ISA',
@@ -107,17 +110,20 @@ def inlinenotes(k, v, f, meta):
             out.write("     found RFC %s\n" % link)
             return [Link(['', [], []],
                          [Str("{RFC "+link+"}")],
-                         ['#%s' % link, '']), Str(v[find_brack+2:])]
+                         ['#%s' % link, '']), 
+                    Str(v[find_brack+2:])]
         if link in lookups:
             out.write("     found %s\n" % lookups[link])
             return [Link(['', [], []],
                          [Str("{"+lookups[link]+"}")],
-                         ['#%s' % link, '']), Str(v[find_brack+2:])]
+                         ['#%s' % linklookups(rest), '']),
+                    Str(v[find_brack+2:])]
         if '|' in link:
             link, ref = link.split("|")
             return [Link(['', [], []],
                          [Str(link)],
-                         [ref, '']), Str(v[find_brack+2:])]
+                         [ref, '']), 
+                    Str(v[find_brack+2:])]
     if k == 'Link':
         out.write("     link type %s\n" %
                   (type(v[1][0])))
@@ -137,6 +143,11 @@ def inlinenotes(k, v, f, meta):
         if re.fullmatch(r"< */ *small *>", v[1]):
             return [RawInline('latex', '}')]
 
+def linklookups(rest):
+    res = '#'.join(rest)
+    if len(rest[0]) == 0:
+        res = '_'.join(rest[1:])
+    return res
 
 if __name__ == "__main__":
     toJSONFilter(inlinenotes)