forked from zxn64/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
construct.py
51 lines (43 loc) · 1.41 KB
/
construct.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
from markdown import markdown
from os import listdir, path
def find_cpp_source(directory):
source = {}
for dirname in listdir(directory):
if path.isdir(dirname):
for filename in listdir(dirname):
if filename.endswith('.cpp'):
source[dirname] = source.get(dirname, []) + [filename]
return source
def wirte_cpp_code(mdfile, sourcefile):
mdfile.write('\n```c++\n')
cppfile = open(sourcefile, 'r', encoding='utf8')
mdfile.write(cppfile.read())
mdfile.write('\n```\n')
source = find_cpp_source('.')
mdfile = open('summary.md', 'w')
mdfile.write('# UESTC_404\'s Template\n')
for dirname, filenames in source.items():
mdfile.write(f'\n## {dirname}\n')
for filename in filenames:
mdfile.write(f"\n### {filename.rstrip('.cpp')}\n")
wirte_cpp_code(mdfile, path.join(dirname, filename))
mdfile.close()
mdfile = open('summary.md', 'r')
mdstr = mdfile.read()
mdfile.close()
html_body = markdown(text=mdstr, output_format='html', extensions=['extra', 'fenced_code', 'codehilite'])
html_whole = \
f"""<html lang="zh-cn">
<head>
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
<link href="codecolor.css" rel="stylesheet">
<link href="pagestyle.css" rel="stylesheet">
</head>
<body>
{html_body}
</body>
</html>
"""
html_file = open('summary.html', 'w', encoding='utf8')
html_file.write(html_whole)
html_file.close()