summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--__main__.py109
-rw-r--r--modules/blog_generate.py112
2 files changed, 115 insertions, 106 deletions
diff --git a/__main__.py b/__main__.py
index 8083d9b..16c2243 100644
--- a/__main__.py
+++ b/__main__.py
@@ -1,12 +1,9 @@
-import datetime
-import email.utils
import os.path
-import shutil
import sys
-
import jinja2
+
from .config import Config
-from .modules import blogpost_processor
+from .modules import blog_generate
def init_jinja_env() -> jinja2.Environment:
@@ -18,108 +15,8 @@ def main(output_root_path: str, local: bool):
if local:
Config.BLOG_ROOT_URL = "http://localhost"
- if os.path.exists(output_root_path):
- shutil.rmtree(output_root_path)
-
- os.mkdir(output_root_path)
-
jinja_env = init_jinja_env()
- 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)
+ blog_generate.generate(jinja_env, os.path.join(output_root_path, "blog"), local)
if __name__ == '__main__':
diff --git a/modules/blog_generate.py b/modules/blog_generate.py
new file mode 100644
index 0000000..ff296e0
--- /dev/null
+++ b/modules/blog_generate.py
@@ -0,0 +1,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) \ No newline at end of file