forked from Mountain-Buzhou/Interview-Book
/
md_conv.py
148 lines (125 loc) · 4.41 KB
/
md_conv.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import argparse
import markdown
import os, os.path
import string, codecs
import re
parser = argparse.ArgumentParser(add_help=True)
parser.add_argument('files', metavar='mdFile', type=str, nargs='+',
help='md files to be converted')
parser.add_argument('--aside-width', dest='asideWidth',type= int,
default=20, help='the width of the aside toc, specified as percents. e.g., 15 means 15 percents')
parser.add_argument('--no-number', dest='noNumber',action='store_true',
default=False, help='do not prepend numbers for headers')
args = parser.parse_args()
htmlTemplate = string.Template('''
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Interview Questions & Answers</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="assets/css/modest.css">
<style>
html, body {width: 100%}
body {margin: 0px;padding: 0px;}
aside.toc {
position: absolute;
top: 0;
left: 0;
bottom: 0;
z-index: 1;
overflow-y: auto;
width: 380px;
color: #364149;
background: #fafafa;
border-right: 1px solid
}
main {
position: absolute;
top: 0;
right: 0;
left: 380px;
bottom: 0;
overflow-y: auto;
color: #000;
background: #fff;
}
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
margin: 0 auto;
padding: 30px;
}
@media (max-width: 767px) {
.markdown-body {
padding: 15px;
}
aside.toc {
display: none;
}
main {
left: 0;
margin-left: 0;
}
}
</style>
</head>
<body>
<aside class="toc markdown-body">$toc</aside>
<main>
<article class="markdown-body">
$mainContent
</article>
</main>
<script>
var tocElem = document.querySelector("aside.toc");
tocElem.style.setProperty("height", window.innerHeight+'px');
window.addEventListener("resize", resizeThrottler, false);
var resizeTimeout;
function resizeThrottler() {
// ignore resize events as long as an actualResizeHandler execution is in the queue
if (!resizeTimeout) {
resizeTimeout = setTimeout(function() {
resizeTimeout = null;
actualResizeHandler();
}, 300);
}
}
function actualResizeHandler() {
tocElem.style.setProperty("height", window.innerHeight+'px');
}
</script>
<body>
''')
if (args.files[0] == '*'):
files = filter(os.path.isfile, os.listdir(os.getcwd()))
files = filter(lambda x: x.endswith('.md'), files)
else:
files = filter(lambda x: x.endswith('.md'), args.files)
md = markdown.Markdown(extensions=['markdown.extensions.toc'], output_format="html5")
if not files:
print 'Error: File not found!'
else:
for fname in files:
# md.convert() accepts only unicode
infile = codecs.open(fname, mode="r", encoding="utf-8")
mdtext =infile.read()
# use convert() instead of convertFile() as convertFile() output the result to either a file or stdout.
mainContent = md.reset().convert(mdtext)
# warning: there should not be a marker such as [TOC] for toc in the converted .md file,
# or else md would not have attribute toc
# 100-3 : 3 percent for margin
html = htmlTemplate.substitute(asideWidth = args.asideWidth, toc = md.toc, mainContent = mainContent)
def replaceMd(val) :
return 'https://github.com/Liyuk/Interview-Questions-Answers/blob/master' + val.group(0);
def replaceImg(val) :
return 'https://raw.githubusercontent.com/Liyuk/Interview-Questions-Answers/master' + val.group(0);
html = re.sub(r'\/[\/\w]+.md', replaceMd, html)
html = re.sub(r'\/[\/\w]+.(jpeg|png)', replaceImg, html)
outfile = open(fname[:-2]+'html', 'w')
outfile.write(html.encode('utf-8'))
print '>>>>> html factory mission complete!'
infile.close()
outfile.close()