Debian Codegraph @

third_party/upb/docs/render.py

raw
1 #!/usr/bin/env python3
2
3 import subprocess
4 import sys
5 import shutil
6 import os
7
8 if len(sys.argv) < 2:
9 print("Must pass a filename argument")
10 sys.exit(1)
11
12 in_filename = sys.argv[1]
13 out_filename = in_filename.replace(".in.md", ".md")
14 out_dir = in_filename.replace(".in.md", "")
15
16 if in_filename == out_filename:
17 print("File must end in .in.md")
18 sys.exit(1)
19
20 if os.path.isdir(out_dir):
21 shutil.rmtree(out_dir)
22
23 os.mkdir(out_dir)
24 file_num = 1
25
26 with open(out_filename, "wb") as out_file, open(in_filename, "rb") as in_file:
27 for line in in_file:
28 if line.startswith(b"```dot"):
29 dot_lines = []
30 while True:
31 dot_line = next(in_file)
32 if dot_line == b"```\n":
33 break
34 dot_lines.append(dot_line)
35 dot_input = b"".join(dot_lines)
36 svg_filename = out_dir + "/" + str(file_num) + ".svg"
37 svg = subprocess.check_output(['dot', '-Tsvg', '-o', svg_filename], input=dot_input)
38 out_file.write(b"<div align=center>\n")
39 out_file.write(b"<img src='%s'/>\n" % (svg_filename.encode('utf-8')))
40 out_file.write(b"</div>\n")
41 file_num += 1
42 else:
43 out_file.write(line)