Skip to content

Commit 72874de

Browse files
authored
Merge pull request #61 from lcy-seso/convert_md_to_html
convert markdown into html.
2 parents 5974ea9 + 72e6225 commit 72874de

File tree

8 files changed

+2081
-0
lines changed

8 files changed

+2081
-0
lines changed

.pre-commit-config.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,11 @@
2525
files: \.md$
2626
- id: remove-tabs
2727
files: \.md$
28+
- repo: local
29+
hooks:
30+
- id: convert-markdown-into-html
31+
name: convert-markdown-into-html
32+
description: Convert README.md into index.html
33+
entry: python .pre-commit-hooks/convert_markdown_into_html.py
34+
language: system
35+
files: .+README\.md$
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import argparse
2+
import re
3+
import sys
4+
5+
HEAD = """
6+
<html>
7+
<head>
8+
<script type="text/x-mathjax-config">
9+
MathJax.Hub.Config({
10+
extensions: ["tex2jax.js", "TeX/AMSsymbols.js", "TeX/AMSmath.js"],
11+
jax: ["input/TeX", "output/HTML-CSS"],
12+
tex2jax: {
13+
inlineMath: [ ['$','$'] ],
14+
displayMath: [ ['$$','$$'] ],
15+
processEscapes: true
16+
},
17+
"HTML-CSS": { availableFonts: ["TeX"] }
18+
});
19+
</script>
20+
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js" async></script>
21+
<script type="text/javascript" src="../.tools/theme/marked.js">
22+
</script>
23+
<link href="http://cdn.bootcss.com/highlight.js/9.9.0/styles/darcula.min.css" rel="stylesheet">
24+
<script src="http://cdn.bootcss.com/highlight.js/9.9.0/highlight.min.js"></script>
25+
<link href="http://cdn.bootcss.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet">
26+
<link href="https://cdn.jsdelivr.net/perfect-scrollbar/0.6.14/css/perfect-scrollbar.min.css" rel="stylesheet">
27+
<link href="../.tools/theme/github-markdown.css" rel='stylesheet'>
28+
</head>
29+
<style type="text/css" >
30+
.markdown-body {
31+
box-sizing: border-box;
32+
min-width: 200px;
33+
max-width: 980px;
34+
margin: 0 auto;
35+
padding: 45px;
36+
}
37+
</style>
38+
39+
40+
<body>
41+
42+
<div id="context" class="container-fluid markdown-body">
43+
</div>
44+
45+
<!-- This block will be replaced by each markdown file content. Please do not change lines below.-->
46+
<div id="markdown" style='display:none'>
47+
"""
48+
49+
TAIL = """
50+
</div>
51+
<!-- You can change the lines below now. -->
52+
53+
<script type="text/javascript">
54+
marked.setOptions({
55+
renderer: new marked.Renderer(),
56+
gfm: true,
57+
breaks: false,
58+
smartypants: true,
59+
highlight: function(code, lang) {
60+
code = code.replace(/&amp;/g, "&")
61+
code = code.replace(/&gt;/g, ">")
62+
code = code.replace(/&lt;/g, "<")
63+
code = code.replace(/&nbsp;/g, " ")
64+
return hljs.highlightAuto(code, [lang]).value;
65+
}
66+
});
67+
document.getElementById("context").innerHTML = marked(
68+
document.getElementById("markdown").innerHTML)
69+
</script>
70+
</body>
71+
"""
72+
73+
74+
def convert_markdown_into_html(argv=None):
75+
parser = argparse.ArgumentParser()
76+
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
77+
args = parser.parse_args(argv)
78+
79+
retv = 0
80+
81+
for filename in args.filenames:
82+
with open(
83+
re.sub(r"README", "index", re.sub(r"\.md$", ".html", filename)),
84+
"w") as output:
85+
output.write(HEAD)
86+
with open(filename) as input:
87+
for line in input:
88+
output.write(line)
89+
output.write(TAIL)
90+
91+
return retv
92+
93+
94+
if __name__ == '__main__':
95+
sys.exit(convert_markdown_into_html())

0 commit comments

Comments
 (0)