-
Notifications
You must be signed in to change notification settings - Fork 1
/
blogmaadi.py
60 lines (49 loc) · 1.47 KB
/
blogmaadi.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
import mistune
import yaml
import os
from jinja2 import Template
from slugify import Slugify
linkify = Slugify(to_lower=True)
mdparser = mistune.Markdown()
blogfolder = '_posts/blog/'
templatefolder = 'templates/'
templates = {}
tags = set()
blogs = []
# load templates
for templatefile in os.listdir(templatefolder):
with open(templatefolder+templatefile) as f:
templates[templatefile[:-5]] = Template(f.read())
# load blog data
for blogfilename in os.listdir(blogfolder):
with open(blogfolder+blogfilename) as blogfile:
contents = blogfile.read().split('---')
blog = yaml.load(contents[1])
blog['date'] = blog['date'].strftime("%d %B, %Y")
blog['html'] = mdparser(contents[2])
blog['file'] = 'post/'+linkify(blog['title'])+'.html'
blog['link'] = '/' + blog['file']
blogs.append(blog)
tags.update(blog['tags'])
# generate index page
with open('index.html','w') as f:
html = templates['index'].render(blogs=blogs,tags=list(tags))
f.write(html)
f.close()
print("Generated index.html")
print('')
# generate tag pages
for tag in tags:
html = templates['tag'].render(blogs=blogs,tag=tag)
with open('tag/'+tag+'.html','w') as f:
f.write(html)
f.close()
print("Generated Tag Page: tag/" + tag + '.html')
print('')
# generate blog pages
for blog in blogs:
html = templates['blog'].render(blog=blog)
with open(blog['file'],'w') as f:
f.write(html)
f.close()
print("Generated Blog Page: " + blog['file'] + '.html')