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
|
import datetime
import email.utils
import json
import os.path
import shutil
import jinja2
from ..config import Config
META_FILE_NAME = "meta.json"
IMAGE_FILE_NAME = "comic.png"
TITLE_KEY = "title"
DESCRIPTION_KEY = "description"
PUBLISH_DATE_KEY = "publish_date"
class Issue:
def __init__(self, path: str):
self.path = path
self.index = os.path.basename(path)
with open(os.path.join(path, META_FILE_NAME)) as f:
self.meta_data = json.load(f)
def title(self) -> str:
return self.meta_data[TITLE_KEY]
def description(self) -> str:
return self.meta_data[DESCRIPTION_KEY]
def get_publish_time(self) -> str:
return self.meta_data[PUBLISH_DATE_KEY]
def get_issues(path: str, local: bool) -> list[Issue]:
return_list = []
for directory in os.listdir(path):
if directory.endswith(".unpublished") and not local:
continue
return_list.append(Issue(os.path.join(path, directory)))
return_list.sort(key=lambda post: post.meta_data[PUBLISH_DATE_KEY], reverse=True)
return return_list
def generate(jinja_env: jinja2.Environment, output_root_path: str, local: bool):
if os.path.exists(output_root_path):
shutil.rmtree(output_root_path)
os.mkdir(output_root_path)
jinja_env.globals.update(comic={
"comic_name": Config.COMIC_NAME,
})
issues = get_issues(Config.ISSUE_SOURCE_DIR, local)
issue_template = jinja_env.get_template("issue.html.j2")
last_render = None
for issue in issues:
output_dir = os.path.join(output_root_path, "issues", issue.index)
os.makedirs(output_dir, exist_ok=True)
ctx = {
"issue": issue,
"url": f"{Config.COMIC_ROOT_URL}/issues/{issue.index}"
}
last_render = issue_template.render(ctx)
with open(os.path.join(output_dir, "index.html"), "w") as f:
f.write(last_render)
shutil.copyfile(os.path.join(issue.path, IMAGE_FILE_NAME), os.path.join(output_dir, IMAGE_FILE_NAME))
with open(os.path.join(output_root_path, "index.html"), "w") as f:
f.write(last_render)
shutil.copytree(Config.COMIC_ASSETS_SOURCE_DIR, output_root_path + Config.ASSETS_IMPORT_PATH)
|