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