-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
64 lines (48 loc) · 1.73 KB
/
main.py
File metadata and controls
64 lines (48 loc) · 1.73 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from time import time
from uuid import uuid4
from equations import equation_to_png
import re
import os
import argparse
import graphviz as gv
parser = argparse.ArgumentParser(description="Extended Markdown")
parser.add_argument('file', type=str, help='Name of the file to load')
parser.add_argument('path', type=str, help='Path of the file to load')
args = parser.parse_args()
print(args.file)
print(args.path)
file_data: str
with open(args.file, 'r') as f:
file_data = f.read()
source_file_name = args.file.split('.')[0]
data_path = f'data/{source_file_name}'
path = args.path
if not os.path.isdir(f"{path}/data"):
os.mkdir('data')
if not os.path.isdir(f"{path}/{data_path}"):
os.mkdir(data_path)
def process_graph(match):
graph = gv.Digraph(format='svg')
filename = f'graph-{uuid4()}'
nodes = set()
for pair in match.group(1).strip().split('\n'):
pair = pair.split(' ')
source = pair[0]
destination = pair[-1]
if source not in nodes:
graph.node(source)
if destination not in nodes:
graph.node(destination)
nodes |= {source, destination}
graph.edge(source, destination)
graph.render(f"{path}/{data_path}/{filename}")
return f"\n"
file_data = re.sub(r"^--+\n((?:\w+ -> \w+\n?)+)\n--+", process_graph, file_data, flags=re.MULTILINE)
def process_latex(match):
filename = f"latex-{uuid4()}"
with open(f"{path}/{data_path}/{filename}.png", 'wb') as f:
equation_to_png(match.group(1), f)
return f""
file_data = re.sub(r"\$\$(.+?)\$\$", process_latex, file_data)
with open(f"{path}/{source_file_name}.md", 'w') as f:
f.write(file_data)