-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
70 lines (56 loc) · 2.03 KB
/
main.py
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
from pathlib import Path
import shutil
import os
import frontmatter
import markdown
import datetime
from operator import itemgetter
from jinja2 import Environment, FileSystemLoader, select_autoescape
env = Environment(
loader=FileSystemLoader("templates"),
autoescape=select_autoescape()
)
env.globals['BASE_URL'] = 'https://bubbathings.com'
# build dir
dist = Path('./dist')
# static dir
static = Path('./static')
# content dir
content = Path('./content')
# Remove previous build if it exists
shutil.rmtree(dist, ignore_errors=True)
# Create destination folder/build dir
dist.mkdir()
# copy /static/* to /dist/*
shutil.copytree(static, dist, dirs_exist_ok=True)
# read posts
posts = []
for post in os.listdir(content/"posts"):
meta = frontmatter.load(content/"posts"/post/"index.md")
post_dict = {
'slug': post,
'title': meta['title'] if 'title' in meta.keys() else "Hello world",
'description': meta['description'] if 'description' in meta.keys() else "Description of hello world.",
'categories': meta['categories'] if 'categories' in meta.keys() else [],
'comments': meta['comments'] if 'comments' in meta.keys() else [],
'date': meta['date'].strftime("%B %d, %Y"),
'markdown': meta.content,
'body': markdown.markdown(meta.content)
}
posts.append(post_dict)
sorted_posts = sorted(posts, key=itemgetter('date'), reverse=True)
# build home page
template = env.get_template("posts.html")
template.stream(posts=sorted_posts).dump(str(dist/"index.html"))
# build each post
template = env.get_template("post.html")
for post in posts:
post_dist = dist / post['slug']
post_dist.mkdir()
images_dist = post_dist / 'images'
images_dist.mkdir()
try:
shutil.copytree(content/post['slug']/'images', images_dist, dirs_exist_ok=True)
except FileNotFoundError as e:
print("~Exception excepted~", e)
template.stream(post=post).dump(str(dist / post['slug'] / "index.html"))