Blame
Date:
Sun Jun 28 20:46:09 2020 UTC
Message:
typo img
001
2020-06-28
dev
#!/usr/bin/env python3
002
2020-06-28
dev
003
2020-06-28
dev
# Copyright (c) 2020 Daniel Jakots
004
2020-06-28
dev
# 2020 Frédéric Galusik
005
2020-06-28
dev
#
006
2020-06-28
dev
# Licensed under the MIT license. See the LICENSE file.
007
2020-06-28
dev
008
2020-06-28
dev
import datetime
009
2020-06-28
dev
import glob
010
2020-06-28
dev
import sys
011
2020-06-28
dev
012
2020-06-28
dev
import jinja2
013
2020-06-28
dev
import markdown
014
2020-06-28
dev
015
2020-06-28
dev
import feedgenerator
016
2020-06-28
dev
017
2020-06-28
dev
CONTENT_PATH = "content/*"
018
2020-06-28
dev
SITE = {}
019
2020-06-28
dev
SITE["author"] = "Frédéric Galusik"
020
2020-06-28
dev
SITE["name"] = SITE["author"]
021
2020-06-28
dev
SITE["url"] = ""
022
2020-06-28
dev
SITE["feed_path"] = "blog/feeds/atom.xml"
023
2020-06-28
dev
OUTPUT_DIR = "output"
024
2020-06-28
dev
025
2020-06-28
dev
026
2020-06-28
dev
def md2html(md):
027
2020-06-28
dev
html = markdown.markdown(md, extensions=["codehilite", "fenced_code", "attr_list"])
028
2020-06-28
dev
return html
029
2020-06-28
dev
030
2020-06-28
dev
031
2020-06-28
dev
def parse_article(article_path):
032
2020-06-28
dev
article = {}
033
2020-06-28
dev
article["file"] = f"{article_path.replace('.md', '')[len(CONTENT_PATH) - 1:]}"
034
2020-06-28
dev
with open(article_path, "r") as f:
035
2020-06-28
dev
metadata = [next(f) for x in range(3)]
036
2020-06-28
dev
for line in metadata:
037
2020-06-28
dev
if line.startswith("Title: "):
038
2020-06-28
dev
article["title"] = line[7:].strip()
039
2020-06-28
dev
elif line.startswith("Date: "):
040
2020-06-28
dev
article["date"] = line[6:].strip()
041
2020-06-28
dev
elif line.startswith("Category: "):
042
2020-06-28
dev
article["category"] = line[10:].strip()
043
2020-06-28
dev
article["markdown"] = f.read()
044
2020-06-28
dev
045
2020-06-28
dev
if len(article) < 4:
046
2020-06-28
dev
print(f"There's a problem with metadata for {article_path}")
047
2020-06-28
dev
sys.exit(1)
048
2020-06-28
dev
return article
049
2020-06-28
dev
050
2020-06-28
dev
051
2020-06-28
dev
def parse_articles(content_path):
052
2020-06-28
dev
content = []
053
2020-06-28
dev
for article in glob.glob(content_path):
054
2020-06-28
dev
article = parse_article(article)
055
2020-06-28
dev
content.append(article)
056
2020-06-28
dev
content.sort(reverse=True, key=lambda i: i["date"])
057
2020-06-28
dev
return content
058
2020-06-28
dev
059
2020-06-28
dev
060
2020-06-28
dev
def generate_website(content):
061
2020-06-28
dev
jinja2_env = jinja2.Environment(
062
2020-06-28
dev
loader=jinja2.FileSystemLoader("templates"), trim_blocks=True
063
2020-06-28
dev
)
064
2020-06-28
dev
jinja2_template = jinja2_env.get_template("index.html.j2")
065
2020-06-28
dev
result = jinja2_template.render(articles=content, site=SITE)
066
2020-06-28
dev
with open(f"{OUTPUT_DIR}/index.html", "w") as f:
067
2020-06-28
dev
f.write(result)
068
2020-06-28
dev
069
2020-06-28
dev
jinja2_template = jinja2_env.get_template("article.html.j2")
070
2020-06-28
dev
for article in content:
071
2020-06-28
dev
result = jinja2_template.render(article=article, site=SITE)
072
2020-06-28
dev
prefix = ""
073
2020-06-28
dev
if article["category"] != "othercontent":
074
2020-06-28
dev
prefix = "blog/"
075
2020-06-28
dev
with open(f"{OUTPUT_DIR}/{prefix}{article['file']}.html", "w") as f:
076
2020-06-28
dev
f.write(result)
077
2020-06-28
dev
078
2020-06-28
dev
079
2020-06-28
dev
def create_feed():
080
2020-06-28
dev
return feedgenerator.Atom1Feed(
081
2020-06-28
dev
title=SITE["name"],
082
2020-06-28
dev
link=f'{SITE["url"]}/',
083
2020-06-28
dev
feed_url=f'{SITE["url"]}/{SITE["feed_path"]}',
084
2020-06-28
dev
description=f"Feed for {SITE['url']}",
085
2020-06-28
dev
)
086
2020-06-28
dev
087
2020-06-28
dev
088
2020-06-28
dev
def main():
089
2020-06-28
dev
content = parse_articles(CONTENT_PATH)
090
2020-06-28
dev
feed = create_feed()
091
2020-06-28
dev
for article in content:
092
2020-06-28
dev
article["html"] = md2html(article.pop("markdown"))
093
2020-06-28
dev
if article["category"] == "othercontent":
094
2020-06-28
dev
continue
095
2020-06-28
dev
date = [int(i) for i in article["date"].split("-")]
096
2020-06-28
dev
date = datetime.datetime(*date, 10, 0, 0)
097
2020-06-28
dev
098
2020-06-28
dev
feed.add_item(
099
2020-06-28
dev
title=article["title"],
100
2020-06-28
dev
link=f"{SITE['url']}/blog/{article['file']}.html",
101
2020-06-28
dev
author_name=SITE["author"],
102
2020-06-28
dev
pubdate=date,
103
2020-06-28
dev
description=article["html"],
104
2020-06-28
dev
)
105
2020-06-28
dev
with open(f"{OUTPUT_DIR}/{SITE['feed_path']}", "w") as f:
106
2020-06-28
dev
feed.write(f, "utf-8")
107
2020-06-28
dev
generate_website(content)
108
2020-06-28
dev
109
2020-06-28
dev
110
2020-06-28
dev
if __name__ == "__main__":
111
2020-06-28
dev
main()
Frédéric Galusik