-
Notifications
You must be signed in to change notification settings - Fork 1
/
sitegen.py
executable file
·115 lines (95 loc) · 3.83 KB
/
sitegen.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
#!/usr/bin/env python3
# 重新生成整个网站
import os
import sys
import json
import argparse
import traceback
import pagegen
import libs.database as database
import libs.logging as logging
from libs.logging import debug, info, warn, error
from libs.utility import *
from preferences import *
update_map = {}
update_all = False
update_count = 0
def check_template_update(token, current_time):
global update_map
global update_all
if token not in update_map or update_map[token] != current_time:
update_map[token] = current_time
update_all = True
info("Template (%s) changed." % token[:8])
def real_generate(filepath):
try:
info("Parsing '%s'..." % filepath)
return pagegen.generate(filepath, not update_all)
except Exception as e:
error("Failed to generate %s." % filepath)
error("%s" % str(e))
debug("Python traceback:\n" + "".join(traceback.format_tb(e.__traceback__)))
def generate(root, name):
global update_map
global update_all
global update_count
global ALL_PATHS
path = os.path.join(root, name)
path_token = hash(path)
time_token = hash(int(os.path.getmtime(path)))
if path_token not in update_map:
update_map[path_token] = None
if update_map[path_token] == time_token and not update_all:
return
data = real_generate(path)
if data:
update_map[path_token] = time_token
if type(data) != bool:
update_count += 1
if type(data) == str:
database.del_index(data)
elif type(data) != bool:
database.add_index(*data)
if __name__ == "__main__":
argpr = argparse.ArgumentParser(description="Convert specific Markdown file to HTML page")
log_group = argpr.add_mutually_exclusive_group()
log_group.add_argument("-v", "--verbose", action="count", help="show more messages", default=0)
log_group.add_argument("-q", "--quiet", action="count", help="show less messages", default=0)
argpr.add_argument("-r", "--regenerate", action="store_true", help="force to regenerate the entire site")
args = argpr.parse_args()
if args.verbose:
logging.LOGGING_LEVEL = logging.LoggingLevel.DEBUG
else:
levels = [logging.LoggingLevel.INFO, logging.LoggingLevel.WARN,
logging.LoggingLevel.ERROR, logging.LoggingLevel.FATAL, logging.LoggingLevel.NONE]
logging.LOGGING_LEVEL = levels[min(args.quiet, len(levels) - 1)]
if args.regenerate:
update_all = True
warn("Force to update all pages.")
if not update_all:
database.load_index(DATABASE_LOCATION)
if os.path.exists(UPDATE_MAP_FILE):
with open(UPDATE_MAP_FILE) as fp:
update_map = json.load(fp)
for root, dirs, files in os.walk(TEMPLATES_FOLDER):
for file in files:
path = os.path.join(root, file)
check_template_update(hash(path), hash(int(os.path.getmtime(path))))
for root, dirs, files in os.walk(os.path.abspath(BLOG_FOLDER)):
for name in files:
if name.endswith(".md") or name.endswith(".markdown"):
generate(root, name)
with open(UPDATE_MAP_FILE, "w") as fp:
json.dump(update_map, fp, indent=4, sort_keys=True)
if update_count:
info("Successfully updated %s page(s)." % update_count)
info("Writing to JSON database '%s'..." % DATABASE_LOCATION)
database.save_json_index(DATABASE_LOCATION)
info("Writing to Tipuesearch database '%s'..." % TIPUESEARCH_DATABASE_LOCATION)
database.save_tipuesearch_index(TIPUESEARCH_DATABASE_LOCATION)
info("Writing to sitemap '%s'..." % SITEMAP_LOCATION)
database.save_text_sitemap(SITEMAP_LOCATION)
else:
warn("Nothing updated.")
if logging.ERROR_CNT or logging.WARN_CNT:
info("%s error(s), %s warning(s) generated." % (logging.ERROR_CNT, logging.WARN_CNT))