f2f19f39fbd740249b593eacd7ca1bdc4d25f4b4
[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 hashtag = ""
63 link = v[2:find_brack]
64 rest = link.split("#") # can be joined up again with '#'.join(rest)
65 link = rest[0]
66 out.write(" link %s\n" % link)
67 lookups = {'sv': 'Scalable Vectors for Power ISA',
68 'SV|sv': 'Scalable Vectors for Power ISA',
69 'openpower/sv': 'Scalable Vectors for Power ISA',
70 'sv/overview': 'Overview Chapter',
71 'sv/vector_isa_comparison': 'Vector ISA Comparison',
72 'sv/comparison_table': 'ISA Comparison Table',
73 'sv/compliancy_levels': 'Compliancy Levels',
74 'sv/svp64': 'SVP64 Chapter',
75 'svp64': 'SVP64 Chapter',
76 'sv/sprs': 'SPRs',
77 'sv/normal': 'Arithmetic Mode',
78 'sv/ldst': 'Load/Store Mode',
79 'sv/cr_ops': 'Condition Register Fields Mode',
80 'sv/executive_summary': 'Exevutive Summary',
81 'sv/branches': 'Branch Mode',
82 'sv/setvl': 'setvl instruction',
83 'sv/svstep': 'svstep instruction',
84 'sv/remap': 'REMAP subsystem',
85 'sv/remap/appendix': 'REMAP Appendix',
86 'sv/mv.swizzle': 'Swizzle Move',
87 'sv/twin_butterfly': 'Twin Butterfly',
88 'sv/mv.vec': 'Pack / Unpack',
89 'svp64/appendix': 'SVP64 Appendix',
90 'sv/svp64/appendix': 'SVP64 Appendix',
91 'sv/svp64_quirks': 'SVP64 Quirks',
92 'openpower/isa/simplev': 'Simple-V pseudocode',
93 'opcode_regs_deduped': 'SVP64 Augmentation Table',
94 'sv/av_opcodes': 'Audio and Video Opcodes',
95 'av_opcodes': 'Audio and Video Opcodes',
96 'sv/vector_ops': 'SV Vector ops',
97 'sv/int_fp_mv': 'FP/Int Conversion ops',
98 'sv/bitmanip': 'Bitmanip ops',
99 'sv/cr_int_predication': 'CR Weird ops',
100 'cr_int_predication': 'CR Weird ops',
101 'sv/fclass': 'FP Class ops',
102 'sv/biginteger': 'Big Integer',
103 'sv/biginteger/analysis': 'Big Integer Analysis',
104 'isa/svfparith': 'Floating Point pseudocode',
105 'isa/svfixedarith': 'Fixed Point pseudocode',
106 'openpower/isa/branch': 'Branch pseudocode',
107 'openpower/transcendentals': 'Transcendentals',
108 }
109 if link.startswith("ls") and link[2].isdigit():
110 out.write(" found RFC %s\n" % link)
111 return [Link(['', [], []],
112 [Str("{RFC "+link+"}")],
113 ['#%s' % link, '']),
114 Str(v[find_brack+2:])]
115 if link in lookups:
116 out.write(" found %s\n" % lookups[link])
117 return [Link(['', [], []],
118 [Str("{"+lookups[link]+"}")],
119 ['#%s' % linklookups(rest), '']),
120 Str(v[find_brack+2:])]
121 if '|' in link:
122 link, ref = link.split("|")
123 return [Link(['', [], []],
124 [Str(link)],
125 [ref, '']),
126 Str(v[find_brack+2:])]
127 if k == 'Link':
128 out.write(" link type %s\n" %
129 (type(v[1][0])))
130 if k == 'RawInline' and v[0] == 'html':
131 if re.fullmatch(r"< *br */? *>", v[1]):
132 return [RawInline('latex', r'\\')]
133 if re.fullmatch(r"< *sup *>", v[1]):
134 return [RawInline('latex', r'\textsuperscript{')]
135 if re.fullmatch(r"< */ *sup *>", v[1]):
136 return [RawInline('latex', '}')]
137 if re.fullmatch(r"< *sub *>", v[1]):
138 return [RawInline('latex', r'\textsubscript{')]
139 if re.fullmatch(r"< */ *sub *>", v[1]):
140 return [RawInline('latex', '}')]
141 if re.fullmatch(r"< *small *>", v[1]):
142 return [RawInline('latex', r'{\small ')]
143 if re.fullmatch(r"< */ *small *>", v[1]):
144 return [RawInline('latex', '}')]
145
146 def linklookups(rest):
147 res = '#'.join(rest)
148 if len(rest[0]) == 0:
149 res = '_'.join(rest[1:])
150 return res
151
152 if __name__ == "__main__":
153 toJSONFilter(inlinenotes)