-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.py
55 lines (43 loc) · 1.46 KB
/
render.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
import json, os
import os.path as osp
from markdowngenerator import MarkdownGenerator
def fix_footnote(text):
"""tranfer [^d^] to [^d]"""
text = text.replace('^]', ']')
return text
def get_filename(path):
"""get filename from path"""
filename = osp.basename(path).split('.')[:-1]
filename = ''.join(filename)
return filename
def read_one(result_path):
with open(result_path) as f:
data = json.load(f)
text = data['details']['text']
text = fix_footnote(text)
text += '\n'
card_body = data['details']['adaptiveCards'][0]['body']
if len(card_body) < 2: # no reference
refer = ''
else:
refer = card_body[-1]['text']
filename = get_filename(result_path)
return text, refer, filename
def write_item_to_md(doc, text, refer, filename):
doc.addHeader(1, filename)
doc.writeTextLine(text)
doc.writeTextLine(f'{doc.addBoldedText(refer)}')
if __name__ == '__main__':
results_dir = 'results'
output_md_name = 'results.md'
results_paths = [osp.join(results_dir, result) for result in sorted(os.listdir(results_dir))]
with MarkdownGenerator(
# By setting enable_write as False, content of the file is written
# into buffer at first, instead of writing directly into the file
# This enables for example the generation of table of contents
filename=output_md_name, enable_write=False
) as doc:
# list all results in the directory
for result_path in results_paths:
text, refer, filename = read_one(result_path)
write_item_to_md(doc, text, refer, filename)