File: render.py

package info (click to toggle)
grpc 1.51.1-8
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 78,336 kB
  • sloc: cpp: 361,873; python: 72,206; ansic: 37,787; objc: 12,434; ruby: 11,521; sh: 7,652; php: 7,615; makefile: 3,481; xml: 3,246; cs: 1,836; javascript: 1,614; java: 465; pascal: 227; awk: 132
file content (43 lines) | stat: -rwxr-xr-x 1,255 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python3

import subprocess
import sys
import shutil
import os

if len(sys.argv) < 2:
    print("Must pass a filename argument")
    sys.exit(1)

in_filename = sys.argv[1]
out_filename = in_filename.replace(".in.md", ".md")
out_dir = in_filename.replace(".in.md", "")

if in_filename == out_filename:
    print("File must end in .in.md")
    sys.exit(1)

if os.path.isdir(out_dir):
    shutil.rmtree(out_dir)

os.mkdir(out_dir)
file_num = 1

with open(out_filename, "wb") as out_file, open(in_filename, "rb") as in_file:
    for line in in_file:
        if line.startswith(b"```dot"):
            dot_lines = []
            while True:
                dot_line = next(in_file)
                if dot_line == b"```\n":
                    break
                dot_lines.append(dot_line)
            dot_input = b"".join(dot_lines)
            svg_filename = out_dir + "/" + str(file_num) + ".svg"
            svg = subprocess.check_output(['dot', '-Tsvg', '-o', svg_filename], input=dot_input)
            out_file.write(b"<div align=center>\n")
            out_file.write(b"<img src='%s'/>\n" % (svg_filename.encode('utf-8')))
            out_file.write(b"</div>\n")
            file_num += 1
        else:
            out_file.write(line)