summaryrefslogtreecommitdiff
path: root/modules/blog_generate.py
blob: ff296e04139eb85063eb84621f72dea1f816db81 (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
108
109
110
111
112
import datetime
import email.utils
import os.path
import shutil
import jinja2

from . import blogpost_processor
from ..config import Config


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)

    posts = blogpost_processor.get_posts(Config.POST_SOURCE_DIR, local)
    tag_occurrence_count = {}
    for post in posts:
        for tag in post.meta_data[blogpost_processor.TAGS_KEY]:
            if tag in tag_occurrence_count:
                tag_occurrence_count[tag] += 1
            else:
                tag_occurrence_count[tag] = 1

    tags = [str(key) for key in tag_occurrence_count.keys()]
    tags.sort(key=lambda key: tag_occurrence_count[key], reverse=True)
    top_tags = tags[:10]

    jinja_env.globals.update(site={
        "assets_path": Config.ASSETS_IMPORT_PATH,
        "assets_path_static": Config.ASSETS_IMPORT_PATH_STATIC,
        "host_name": Config.BLOG_HOSTNAME,
        "blog_name": Config.BLOG_NAME,
        "subtitle": Config.BLOG_SUBTITLE,
        "top_tags": top_tags
    })

    blogpost_template = jinja_env.get_template("blogpost.html.j2")
    subpage_template = jinja_env.get_template("blogpost_sub.html.j2")
    for post in posts:
        output_dir = os.path.join(output_root_path, "posts", post.get_publish_year(), post.name)
        os.makedirs(output_dir, exist_ok=True)
        for extra_file in post.extra_files:
            from_path = os.path.join(post.path, extra_file)
            if os.path.isdir(from_path):
                shutil.copytree(from_path, os.path.join(output_dir, extra_file))
            else:
                shutil.copyfile(from_path, os.path.join(output_dir, extra_file))

        ctx = {
            "post": post,
            "url": f"{Config.BLOG_ROOT_URL}{post.href}"
        }
        with open(os.path.join(output_dir, "index.html"), "w") as f:
            f.write(blogpost_template.render(ctx))

        for subpage in post.subpages:
            ctx = {
                "post": post,
                "subpage_name": subpage[0],
                "subpage_html": subpage[1],
                "url": f"{Config.BLOG_ROOT_URL}{post.href}/{subpage[0]}.html"
            }

            with open(os.path.join(output_dir, f"{subpage[0]}.html"), "w") as f:
                f.write(subpage_template.render(ctx))

    tags_template = jinja_env.get_template("tags.html.j2")
    output_dir = os.path.join(output_root_path, "tags")
    os.makedirs(output_dir, exist_ok=True)

    ctx = {
        "config": Config,
        "tags_ls": " ".join([f'<a href="{tag}.html">{tag}/</a>' for tag in tags]),
        "url": f"{Config.BLOG_ROOT_URL}/tags"
    }
    with open(os.path.join(output_dir, "index.html"), "w") as f:
        f.write(tags_template.render(ctx))

    tag_template = jinja_env.get_template("tag.html.j2")
    for tag in tags:
        ctx = {
            "config": Config,
            "tag": tag,
            "posts": [post for post in posts if tag in post.meta_data["tags"]],
            "url": f"{Config.BLOG_ROOT_URL}/tags/{tag}.html"
        }
        with open(os.path.join(output_dir, "{}.html".format(tag)), "w") as f:
            f.write(tag_template.render(ctx))

    index_template = jinja_env.get_template("index.html.j2")
    ctx = {
        "posts": posts,
        "url": Config.BLOG_ROOT_URL
    }
    with open(os.path.join(output_root_path, "index.html"), "w") as f:
        f.write(index_template.render(ctx))

    acknowledgement_template = jinja_env.get_template("acknowledgement.html.j2")
    with open(os.path.join(output_root_path, "acknowledgement.html"), "w") as f:
        f.write(acknowledgement_template.render({}))

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

    shutil.copytree(Config.ASSETS_SOURCE_DIR, output_root_path + Config.ASSETS_IMPORT_PATH)