forked from sunnycomes/IoVision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
105 lines (82 loc) · 3.09 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
#!/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 init_root_path, load_config
from src.common.template_parser import TemplateParser
from src.common.settings import get_site_info
from src.common import markdown_parser
from src.common.markdown_parser import BasicParser
def list_template_files():
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 generate_index():
posts = markdown_parser.get_all_parsed_posts()
params = get_site_info()
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 copy_static_files():
dest = options.build_dir + os.sep + "static"
rmdir(dest)
shutil.copytree(options.static_resource_dir, dest)
def generate_posts():
dest = options.build_dir + os.sep + "post"
mkdir(dest)
posts = markdown_parser.get_all_parsed_posts(brief=False)
params = get_site_info()
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 generate_about():
dest = options.build_dir + os.sep + "about"
mkdir(dest)
post = BasicParser.parse(options.about_dir, "about.markdown")
post["title"] = options.author
params = get_site_info()
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_hobby_index():
dest = options.build_dir + os.sep + "hobby"
mkdir(dest)
posts = markdown_parser.get_all_parsed_hobby_posts()
params = get_site_info()
html = TemplateParser.parse(options.current_template_dir, "hobbyindex.html", posts=posts, params=params)
index_file = open("build/hobby/index.html", "wb")
index_file.write(html)
def generate_hobby_posts():
dest = options.build_dir + os.sep + "hobby"+os.sep+"post"
mkdir(dest)
posts = markdown_parser.get_all_parsed_hobby_posts(brief=False)
params = get_site_info()
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 generate():
mkdir(options.build_dir)
generate_index()
copy_static_files()
generate_posts()
generate_about()
generate_hobby_index()
generate_hobby_posts()
if __name__ == '__main__':
root_path = os.path.dirname(os.path.abspath(__file__))
init_root_path(root_path)
config_file_path = root_path + os.sep + "setup.cfg"
load_config(config_file_path)
generate()