forked from sunnycomes/IoVision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
82 lines (63 loc) · 2.2 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
#!/usr/bin/env python
'''
Created on Oct 6, 2014
@author: sunnycomes
'''
import os
import shutil
from tornado.options import options
from src.common.utils import initRootPath, loadConfig
from src.common.template_parser import TemplateParser
from src.common.settings import getSiteInfo
from src.common import markdown_parser
from src.common.markdown_parser import BasicParser
def listTemplateFiles():
return os.listdir(options.current_template_dir)
def mkdir(dirx):
if os.path.exists(dirx):
return
os.mkdir(dirx)
def rmdir(dest):
if os.path.exists(dest):
shutil.rmtree(dest)
pass
def generateIndex():
posts = markdown_parser.getAllParsedPosts()
params = getSiteInfo()
html = TemplateParser.parse(options.current_template_dir, "index.html", posts=posts, params=params)
index_file = open("build/index.html", "wb")
index_file.write(html)
def copyStaticFiles():
dest = options.build_dir + os.sep + "static"
rmdir(dest)
shutil.copytree(options.static_resource_dir, dest)
def generatePosts():
dest = options.build_dir + os.sep + "post"
mkdir(dest)
posts = markdown_parser.getAllParsedPosts(brief=False)
params = getSiteInfo()
for post in posts:
html = TemplateParser.parse(options.current_template_dir, "post.html", post=post, params=params)
post_file = open(dest + os.sep + post["post_name"] + ".html", "wb")
post_file.write(html)
def generateAbout():
dest = options.build_dir + os.sep + "about"
mkdir(dest)
post = BasicParser.parse(options.about_dir, "about.markdown")
post["title"] = options.author
params = getSiteInfo()
html = TemplateParser.parse(options.current_template_dir, "about.html", post=post, params=params)
about_file = open(dest + os.sep + "index.html", "wb")
about_file.write(html)
def generate():
mkdir(options.build_dir)
generateIndex()
copyStaticFiles()
generatePosts()
generateAbout()
if __name__ == '__main__':
root_path = os.path.dirname(os.path.abspath(__file__))
initRootPath(root_path)
config_file_path = root_path + os.sep + "setup.cfg"
loadConfig(config_file_path)
generate()