bug 1034: update spec page on bin/tern lut2/lut3
[libreriscv.git] / openpower / mdwn_inline.py
1 #!/usr/bin/env python3
2 import sys
3 import os.path
4 from io import StringIO
5
6 deps_only = sys.argv[1] == '--deps'
7
8 if deps_only:
9 del sys.argv[1]
10
11 opened_files = []
12
13 def open_tracked(name, mode='r'):
14 opened_files.append(name)
15 try:
16 return open(name, mode)
17 except FileNotFoundError:
18 if deps_only:
19 return StringIO("")
20 raise
21
22 output_file = sys.argv[2]
23 try:
24 os.remove(output_file)
25 except FileNotFoundError:
26 pass
27 temp_output_file = output_file + '.tmp'
28 file_path = os.path.abspath(__file__)
29 openpower_path = os.path.split(file_path)[0]
30 wiki_path = os.path.split(openpower_path)[0]
31
32
33 def recursive_inline(f, input_name, depth):
34 assert depth < 10, "probably found an [[!inline]]-loop"
35 skip = False # found the pattern <!-- hide -->
36 for line in f.readlines():
37 # skip hide/show
38 if skip:
39 if line.startswith("<!-- show -->"):
40 skip = False
41 continue
42 elif line.startswith("<!-- hide -->"):
43 skip = True
44 continue
45 # fix separation in comparison table
46 if input_name.endswith("comparison_table.tex") and \
47 line.startswith("\begin{itemize}"):
48 o.write(line)
49 o.write("\\itemsep -0.3em\n")
50 continue
51 # find headings and create name-refs, including filename
52 if line.startswith("#"):
53 iname = input_name.split("/")
54 if iname[0] == 'openpower': iname.pop(0)
55 l = line.strip().split(" ")[1:3]
56 l = map(lambda x: ''.join(filter(str.isalnum, x)), l)
57 l = map(str.lower, l)
58 l = filter(lambda x:x, l)
59 l = list(dict.fromkeys(iname + list(l)))
60 l = '_'.join(l)
61 line = '%s <a name="%s"> </>\n' % (line.strip(), l)
62 # find actual inlines
63 if not line.startswith("[[!inline"):
64 o.write(line)
65 continue
66 print(line.strip())
67 # assume first thing is pagename
68 line = line.split('"')
69 fname = line[1]
70 print(f"\tdepth={depth}: {fname}")
71 if fname.endswith(".py"):
72 if fname.startswith("gf_reference"):
73 with open_tracked(
74 wiki_path + "/../nmigen-gf/" + fname) as inc:
75 recursive_inline(inc, fname, depth + 1)
76 else:
77 with open_tracked(wiki_path + "/" + fname) as inc:
78 recursive_inline(inc, fname, depth + 1)
79 else:
80 if fname.endswith(".mdwn"):
81 with open_tracked(wiki_path + "/" + fname) as inc:
82 recursive_inline(inc, fname, depth + 1)
83 elif fname.startswith('openpower/isatables'):
84 pth = wiki_path + "/../openpower-isa/" + fname
85 with open_tracked(pth) as inc:
86 recursive_inline(inc, fname, depth + 1)
87 elif fname.startswith('openpower/isa'):
88 pth = wiki_path + "/../openpower-isa/" + fname + ".mdwn"
89 with open_tracked(pth) as inc:
90 recursive_inline(inc, fname, depth + 1)
91 else:
92 with open_tracked(wiki_path + "/" + fname + ".mdwn") as inc:
93 recursive_inline(inc, fname, depth + 1)
94
95
96 def body(o, print=print):
97 with open_tracked(sys.argv[1], "r") as f:
98 recursive_inline(f, sys.argv[1], 0)
99
100 if deps_only:
101 with StringIO() as o:
102 body(o, print=lambda *_: None)
103 deps_file = output_file + '.d'
104 with open(deps_file, "w") as o:
105 deps = " ".join(opened_files)
106 o.write(f"{output_file} {deps_file}: {deps}\n")
107 else:
108 with open(temp_output_file, "w") as o:
109 body(o)
110 os.rename(temp_output_file, output_file)
111