summaryrefslogtreecommitdiff
path: root/modules/comic_generate.py
blob: 8615f1f73c64ec1fc9e30817064efa69e5c2d264 (plain)
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
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"
EXTRA_ATTR_KEY = "extra_attributes"


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_date(self) -> str:
        return self.get_publish_time().split()[0]

    def get_publish_time(self) -> str:
        return self.meta_data[PUBLISH_DATE_KEY]

    def get_publish_time_rfc2822(self) -> str:
        return email.utils.format_datetime(datetime.datetime.fromisoformat(self.get_publish_time() + "+02:00"))

    def get_link(self) -> str:
        return f"{Config.COMIC_ROOT_URL}/issues/{self.index}"

    def extra_attributes(self) -> str:
        return self.meta_data[EXTRA_ATTR_KEY] if EXTRA_ATTR_KEY in self.meta_data.keys() else ""


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,
        "comic_url": Config.COMIC_ROOT_URL,
    })

    issues = get_issues(Config.ISSUE_SOURCE_DIR, local)
    issue_template = jinja_env.get_template("issue.html.j2")
    last_render = None

    last_issue_idx = len(issues) - 1
    for idx, issue in enumerate(issues):
        output_dir = os.path.join(output_root_path, "issues", issue.index)
        os.makedirs(output_dir, exist_ok=True)

        ctx = {
            "issue": issue,
            "url": issue.get_link(),
            "previous": "/#" if idx == 0 else f"/issues/{str(idx)}",
            "next": "/#" if idx == last_issue_idx else f"/issues/{str(idx + 2)}"
        }

        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)

    rss_template = jinja_env.get_template("comic_feed.xml.j2")
    ctx = {
        "build_date": email.utils.format_datetime(datetime.datetime.now(Config.TIMEZONE)),
        "issues": issues[:5]
    }
    with open(os.path.join(output_root_path, "feed.xml"), "w") as f:
        f.write(rss_template.render(ctx))