add link-creation for RFCs
[libreriscv.git] / openpower / pandoc_img.py
1 #!/usr/bin/env python3
2
3 import os
4 import re
5 import sys
6 import subprocess
7 from pandocfilters import (toJSONFilter, RawInline, Space, Str, walk, Image,
8 Link)
9
10 """
11 Pandoc filter for Markdown that converts most endnotes into
12 Pandoc's inline notes. If input notes had multiple paragraphs,
13 the paragraphs are joined by a space. But if an input note
14 had any blocks other than paragraphs, the note is left as is.
15 """
16
17 # inkscape -z sv/bridge_phy.svg --export-png=bridge.png
18 out = open("/tmp/log.txt", "w")
19
20
21 def query(k, v, f, meta):
22 global inlines
23 if k == 'BlockQuote':
24 inlines.append(v)
25 elif isinstance(v, list):
26 if inlines and k == 'Para':
27 inlines.append(Space())
28 inlines.extend(v)
29 return v
30
31
32 def inlinenotes(k, v, f, meta):
33 out.write("k v f meta %s %s %s %s\n" %
34 (repr(k), repr(v), repr(f), repr(meta)))
35 global inlines
36 inlines = []
37 if k == u'Image' and f == 'latex':
38 imgname = v[2][0]
39 out.write(" image %s\n" % (imgname))
40 if imgname.startswith("/"):
41 # use absolute paths so pandoc_img.py can be used in any directory
42 file_path = os.path.abspath(__file__)
43 openpower_path = os.path.split(file_path)[0]
44 wiki_path = os.path.split(openpower_path)[0]
45 imgname = os.path.join(wiki_path, imgname.lstrip('/'))
46 png = imgname.replace(".svg", ".png")
47 png = os.path.split(png)[1]
48 png = "tex_out/%s" % png
49 print(f"converting {imgname} to {png}", file=sys.stderr)
50 subprocess.run(["inkscape", "-z", "-C", imgname,
51 "--export-png=%s" % png],
52 stdout=subprocess.PIPE)
53 v[2][0] = png
54 return Image(*v)
55 if k == 'Str' and f == 'latex':
56 # link page
57 if not v.startswith("[["):
58 return
59 find_brack = v.find(']]')
60 if find_brack == -1:
61 return
62 link = v[2:find_brack]
63 out.write(" link %s\n" % link)
64 lookups = {'sv': 'Scalable Vectors for Power ISA',
65 'SV|sv': 'Scalable Vectors for Power ISA',
66 'openpower/sv': 'Scalable Vectors for Power ISA',
67 'sv/overview': 'Overview Chapter',
68 'sv/vector_isa_comparison': 'Vector ISA Comparison',
69 'sv/comparison_table': 'ISA Comparison Table',
70 'sv/compliancy_levels': 'Compliancy Levels',
71 'sv/svp64': 'SVP64 Chapter',
72 'svp64': 'SVP64 Chapter',
73 'sv/sprs': 'SPRs',
74 'sv/normal': 'Arithmetic Mode',
75 'sv/ldst': 'Load/Store Mode',
76 'sv/cr_ops': 'Condition Register Fields Mode',
77 'sv/executive_summary': 'Exevutive Summary',
78 'sv/branches': 'Branch Mode',
79 'sv/setvl': 'setvl instruction',
80 'sv/svstep': 'svstep instruction',
81 'sv/remap': 'REMAP subsystem',
82 'sv/remap/appendix': 'REMAP Appendix',
83 'sv/mv.swizzle': 'Swizzle Move',
84 'sv/mv.vec': 'Pack / Unpack',
85 'svp64/appendix': 'SVP64 Appendix',
86 'sv/svp64/appendix': 'SVP64 Appendix',
87 'sv/svp64_quirks': 'SVP64 Quirks',
88 'openpower/isa/simplev': 'Simple-V pseudocode',
89 'opcode_regs_deduped': 'SVP64 Augmentation Table',
90 'sv/av_opcodes': 'Audio and Video Opcodes',
91 'av_opcodes': 'Audio and Video Opcodes',
92 'sv/vector_ops': 'SV Vector ops',
93 'sv/int_fp_mv': 'FP/Int Conversion ops',
94 'sv/bitmanip': 'Bitmanip ops',
95 'sv/cr_int_predication': 'CR Weird ops',
96 'cr_int_predication': 'CR Weird ops',
97 'sv/fclass': 'FP Class ops',
98 'sv/biginteger': 'Big Integer',
99 'sv/biginteger/analysis': 'Big Integer Analysis',
100 'isa/svfparith': 'Floating Point pseudocode',
101 'isa/svfixedarith': 'Fixed Point pseudocode',
102 'openpower/isa/branch': 'Branch pseudocode',
103 'openpower/transcendentals': 'Transcendentals',
104 }
105 if link.startswith("ls") and link[2].isdigit():
106 out.write(" found RFC %s\n" % link)
107 return [Link(['', [], []],
108 [Str("{RFC "+link+"}")],
109 ['#%s' % link, '']), Str(v[find_brack+2:])]
110 if link in lookups:
111 out.write(" found %s\n" % lookups[link])
112 return [Link(['', [], []],
113 [Str("{"+lookups[link]+"}")],
114 ['#%s' % link, '']), Str(v[find_brack+2:])]
115 if '|' in link:
116 link, ref = link.split("|")
117 return [Link(['', [], []],
118 [Str(link)],
119 [ref, '']), Str(v[find_brack+2:])]
120 if k == 'Link':
121 out.write(" link type %s\n" %
122 (type(v[1][0])))
123 if k == 'RawInline' and v[0] == 'html':
124 if re.fullmatch(r"< *br */? *>", v[1]):
125 return [RawInline('latex', r'\\')]
126 if re.fullmatch(r"< *sup *>", v[1]):
127 return [RawInline('latex', r'\textsuperscript{')]
128 if re.fullmatch(r"< */ *sup *>", v[1]):
129 return [RawInline('latex', '}')]
130 if re.fullmatch(r"< *sub *>", v[1]):
131 return [RawInline('latex', r'\textsubscript{')]
132 if re.fullmatch(r"< */ *sub *>", v[1]):
133 return [RawInline('latex', '}')]
134 if re.fullmatch(r"< *small *>", v[1]):
135 return [RawInline('latex', r'{\small ')]
136 if re.fullmatch(r"< */ *small *>", v[1]):
137 return [RawInline('latex', '}')]
138
139
140 if __name__ == "__main__":
141 toJSONFilter(inlinenotes)