Skip to content

Commit 6f2df32

Browse files
committed
move output dir to _site directory
1 parent f0c6a59 commit 6f2df32

13 files changed

+24
-2389
lines changed

generate-html.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,36 @@
22
# copied from https://github.com/tonybaloney/tonybaloney.github.io/blob/master/blog-gen.py
33
import markdown
44
import jinja2
5-
import glob
5+
from pathlib import Path
66
from datetime import date, datetime
77
from email.utils import formatdate, format_datetime # for RFC2822 formatting
88

99
TEMPLATE_FILE = "templates/blog_post_template.html"
1010
FEED_TEMPLATE_FILE = "templates/rss_feed_template.xml"
11-
BASE_URL = "https://tonybaloney.github.io/"
11+
BLOG_POSTS_PATH = Path("posts")
12+
OUTPUT_DIR = Path("_site")
1213

1314
from dataclasses import dataclass
1415

1516
@dataclass
1617
class Post:
1718
title: str
1819
author: str
19-
md_path: str
20+
md_path: Path
2021
date: date
2122

2223
@property
2324
def html_path(self):
24-
return self.md_path.replace('blog/', 'posts/').replace('.md', '.html')
25+
return OUTPUT_DIR / "posts" / self.md_path.name.replace('.md', '.html')
2526

27+
@property
28+
def url(self):
29+
return f"posts/{self.html_path.name}"
2630

2731

2832

2933
def main():
30-
md_post_paths = sorted(glob.glob("blog/*.md"))
34+
md_post_paths = sorted(BLOG_POSTS_PATH.glob("*.md"))
3135
extensions = ['extra', 'smarty', 'meta', 'codehilite']
3236
_md = markdown.Markdown(extensions=extensions, output_format='html5')
3337

@@ -36,34 +40,30 @@ def main():
3640

3741
all_posts = []
3842
for md_post_path in md_post_paths:
39-
print("rendering", md_post_path)
40-
post_date = date.fromisoformat(md_post_path[5:15])
41-
with open(md_post_path) as f:
42-
html = _md.convert(f.read())
43+
# print("rendering", md_post_path)
44+
post_date = date.fromisoformat(md_post_path.name[:10])
45+
html_content = _md.convert(md_post_path.read_text())
4346
post = Post(
4447
md_path=md_post_path, date=post_date,
4548
author=_md.Meta['author'][0],
4649
title=_md.Meta['title'][0],
4750
)
48-
doc = env.get_template(TEMPLATE_FILE).render(
49-
content=html, baseurl=BASE_URL, url=post.html_path, post=post,
51+
post_html = env.get_template(TEMPLATE_FILE).render(
52+
content=html_content, url=post.html_path, post=post,
5053
)
54+
print("writing", post.html_path)
55+
post.html_path.write_text(post_html)
5156

52-
with open(post.html_path, "w") as f:
53-
f.write(doc)
54-
# all_posts.append(dict(**_md.Meta, date=post_date, rfc2822_date=format_datetime(post_date), link="{0}{1}".format(BASE_URL, url)))
55-
all_posts.append(post) # TODO fix date
57+
all_posts.append(post) # TODO rfc2822_date=format_datetime(post_date),
5658

5759
# index
58-
print("rendering index.html")
59-
with open('index.md') as f:
60-
index_html = _md.convert(f.read())
61-
doc = env.get_template('templates/index.html').render(
62-
content=index_html,
60+
# print("rendering index.html")
61+
index_html = env.get_template('pages/index.html').render(
6362
posts=all_posts,
6463
)
65-
with open('index.html', "w") as f:
66-
f.write(doc)
64+
index_html_path = OUTPUT_DIR / 'index.html'
65+
print("writing", index_html_path)
66+
index_html_path.write_text(index_html)
6767

6868
# Order blog posts by date published
6969
all_posts.sort(key=lambda p: p.date, reverse=True)

index.html

Lines changed: 0 additions & 126 deletions
This file was deleted.

templates/index.html renamed to pages/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ <h3>Recent posts</h3>
4949
{% for post in posts %}
5050
{% if post.date.year != 2017 %}
5151
<li>
52-
<a href="{{ post.html_path }}">{{ post.date }} {{ post.title }}</a>
52+
<a href="{{ post.url }}">{{ post.date }} {{ post.title }}</a>
5353
</li>
5454
{% endif %}
5555
{% endfor %}
@@ -62,7 +62,7 @@ <h3>Classic 2017 Episodes on Ports & Adapters, by Bob</h3>
6262
{% for post in posts %}
6363
{% if post.date.year == 2017 %}
6464
<li>
65-
<a href="{{ post.html_path }}">{{ post.date }} {{ post.title }}</a>
65+
<a href="{{ post.url }}">{{ post.date }} {{ post.title }}</a>
6666
</li>
6767
{% endif %}
6868
{% endfor %}

0 commit comments

Comments
 (0)