-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.py
executable file
·138 lines (113 loc) · 3.01 KB
/
generate.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
#!/usr/bin/env python3
from glob import glob
from os import path
import os
import re
import sys
import shutil
import markdown
def generate_index(dir, is_root):
# Read the header
readme_path = path.join(dir, "README.md")
index_path = path.join(dir, "INDEX.md")
header = ""
if path.exists(readme_path):
header = open(readme_path).read()
elif path.exists(index_path):
header = open(index_path).read()
else:
header = str.format("# {}\n", path.basename(path.dirname(dir)))
if not is_root:
header += "\n"
header += "[Go up a directory](./..).\n"
header += "[Go up to root directory](/).\n"
# Gather the files
locals = glob(path.join(dir, "*"))
all = glob(path.join(dir, "*/**"), recursive=True)
# Filter the files
locals = [x for x in locals if not path.isdir(x)]
hiddenExtensions = [".css", ".md", "index.html"]
def shouldHide(path):
for extension in hiddenExtensions:
if path.endswith(extension):
return True
return False
locals = list(filter(lambda x: not shouldHide(x), locals))
all = list(filter(lambda x: not shouldHide(x), all))
# Sort the lists
locals.sort()
all.sort()
# Create the markdown file
md = header
if len(locals) > 0:
md += "## Contents\n"
for file in locals:
rel_path = file.replace(dir, "")
md += str.format(" * [{}]({})", rel_path, rel_path) + "\n"
md += "\n"
if len(all) > 0:
md += "## Subdirectories\n"
for file in all:
rel_path = file.replace(dir, "")
if path.isdir(file):
md += str.format(" * [{}]({})", rel_path, path.join(rel_path, "index.html")) + "\n"
else:
md += str.format(" * [{}]({})", rel_path, rel_path) + "\n"
md += "\n"
# Generate the body
body = markdown.markdown(md, extensions=['markdown.extensions.nl2br'])
# Generate the Html
html = \
"""<!DOCTYPE html>
<meta charset="utf-8"/>
<html>
<head>
<style type="text/css">
html {{
background-color: #3D9970
}}
body {{
margin: 1em;
border-radius: 1em;
padding: 1.5em;
background-color: white;
font-family: sans-serif;
}}
h1 {{
margin-top: 0px;
font-style: italic;
}}
h2 {{
margin-bottom: 0.5em;
}}
ul {{
margin-top: 0.5em;
}}
li {{
margin: 0.5em;
}}
a, a:visited, a:hover, a:active {{
color: rgb(0, 0, 238);
}}
</style>
</head>
<body>
{0}
</body>
</html>
""".format(body)
with open(path.join(dir, "index.html"), "w") as out:
out.write(html)
def generate_site(out_dir):
out_dir = path.join(out_dir, "")
if path.isdir(out_dir):
shutil.rmtree(out_dir)
shutil.copytree(".",
out_dir,
ignore=shutil.ignore_patterns("*.py", "index.html", ".git"))
is_root = True
for (dir, _, _) in os.walk(out_dir):
dir = path.join(dir, "")
generate_index(dir, is_root)
is_root = False
generate_site(sys.argv[1])