-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown_converter.py
76 lines (59 loc) · 2.16 KB
/
markdown_converter.py
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
65
66
67
68
69
70
71
72
73
74
75
76
import os
import glob
import re
import json
def convert_markdown(filename):
newlines = []
with open(filename, 'r') as f:
lines = f.readlines()
showpat = re.compile(r"- {\"([a-z]+)\":\"(.+)\"}$")
tablepat = re.compile(r"- {\"table\":(\[.+\])}$")
stamppat = re.compile(r"\*\*.+\*\*$")
seppat = re.compile(r"- {\"separator\".+}$")
for line in lines:
line = re.sub("<span[^<>]+>", "", line)
line = re.sub("</span>", "", line)
showmatch = showpat.match(line)
tablematch = tablepat.match(line)
stampmatch = stamppat.match(line)
sepmatch = seppat.match(line)
if showmatch:
showtype = showmatch.group(1)
content = showmatch.group(2)
if showtype == "title":
newlines.append("## " + content)
else:
newlines.append(content)
newlines.append("\n\n")
elif tablematch:
content = tablematch.group(1)
newlines += format_table(content)
newlines.append("\n\n")
elif stampmatch:
newlines.append("---\n---\n")
elif sepmatch:
newlines.append("-"*10 + "\n\n")
else:
newlines.append(line)
with open(filename, 'w') as f:
f.writelines(newlines)
def format_table(content):
formatted = ["<table>"]
ary = json.loads(content)
style = "border: 1px solid gray; text-align: center"
for row in ary:
newrow = ""
for cell in row:
this_style = style
if isinstance(cell, dict):
newcell = cell.get("content") or "?"
if cell.get("class") == "td-filled-slot":
this_style = style + "; background-color: lightskyblue"
else:
newcell = cell
newrow += "<td style=\"{}\">{}</td>".format(this_style, newcell)
formatted.append("<tr>{}</tr>".format(newrow))
formatted.append("</table>")
return formatted
for filename in glob.iglob('**/test_results.md', recursive=True):
print(convert_markdown(filename))