-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrew.py
148 lines (123 loc) · 4.55 KB
/
brew.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os
import glob
import codecs
import datetime
import PyRSS2Gen
import re
import shutil
from hops.blogpost import BlogPost
import settings
def write_blog(post, filename):
f = codecs.open(filename, "w", "utf-8")
blog = post.get_header() + post.html
f.write(blog)
f.close()
def write_index(posts, filename, archive=None, reverse=False):
f = codecs.open(filename, "w", "utf-8")
index = ""
for post in posts:
index += post.get_header() + post.html
if archive:
index += archive
f.write(index)
f.close()
def generate_archive(index):
archive_html = u"\n<div class='archive'>\n<h2>Archives</h2>\n<ul>\n"
for year in sorted(index, reverse=True):
for month in sorted(index[year], reverse=True):
archive_date = datetime.date(year, month, 1)
archive_line = u"\t<li><a href='/%s/%02d/'>%s</a></li>\n" % (
year,
month,
archive_date.strftime('%B %Y')
)
archive_html += archive_line
return archive_html + u"</ul></div>"
def write_rss(posts, filename):
rss_items = []
for post in posts:
if 'link' in post.meta:
the_link = post.meta['link']
html = post.html + u"\n\n<p><strong><a href='%s'>β</a></strong></p>" % post.get_absolute_url()
else:
the_link = post.get_absolute_url()
html = post.html
item = PyRSS2Gen.RSSItem(
title=post.meta['title'], link=the_link, description=html,
# Temporary fix: I'm on Brussels time, which is 2 hours ahead of
# UTC. Proper timezone support will be added later.
pubDate=post.meta['pubdate'] - datetime.timedelta(0, 0, 0, 0, 0, 2, 0),
)
rss_items.append(item)
rss = PyRSS2Gen.RSS2(
title="bjornssaga.com", link="http://bjornssaga.com",
description="Latest entries from bjornssaga.com",
lastBuildDate=posts[0].meta['pubdate'] - datetime.timedelta(0, 0, 0, 0, 0, 2, 0),
items=rss_items,
)
rss_file = open(filename, "w")
rss.write_xml(rss_file)
rss_file.close()
def get_blogposts(path):
blogposts = []
blog_index = {}
for infile in glob.glob(os.path.join(
path, '*.txt')
):
blog = BlogPost(infile)
blogposts.append(blog)
year = blog.meta['pubdate'].year
month = blog.meta['pubdate'].month
if not year in blog_index:
blog_index[year] = {}
if not month in blog_index[year]:
blog_index[year][month] = []
blog_index[year][month].append(blog)
return blogposts, blog_index
def publish_drafts():
for f in glob.glob(os.path.join(settings.SOURCES['drafts'], '*.txt')):
filename = f.split('/')[-1]
if re.match('^pub_', filename):
post = BlogPost(f)
new_name = "%s%02d%02d_%s.txt" % (
post.meta['pubdate'].year,
post.meta['pubdate'].month,
post.meta['pubdate'].day,
post.meta['slug'])
shutil.move(f, os.path.join(settings.SOURCES['published'], new_name))
def main():
publish_drafts()
drafts, draft_index = get_blogposts(settings.SOURCES['drafts'])
blogposts, blog_index = get_blogposts(settings.SOURCES['published'])
blogs = sorted(blogposts, key=lambda blog: blog.meta['pubdate'], reverse=True)
# Write individual blog posts
for blogpost in blogs:
filename = "%s%s%02d%02d_%s.php" % (
settings.SERVER_ROOT,
blogpost.meta['pubdate'].year,
blogpost.meta['pubdate'].month,
blogpost.meta['pubdate'].day,
blogpost.meta['slug'])
write_blog(blogpost, filename)
# Write draft
for draft in drafts:
filename = "%sdrafts/%s.php" % (
settings.SERVER_ROOT,
draft.meta['slug']
)
write_blog(draft, filename)
# Generate archive index block
archive = generate_archive(blog_index)
# Write month index
for year in blog_index:
for month in blog_index[year]:
filename = "%s%s%02d.php" % (settings.SERVER_ROOT, year, month)
blogs_by_month = sorted(blog_index[year][month], key=lambda blog: blog.meta['pubdate'])
write_index(blogs_by_month, filename, archive)
# Write RSS
write_rss(blogs[:10], "%sfeed.rss" % settings.SERVER_ROOT)
# Write main index
filename = settings.SERVER_ROOT + "main.php"
write_index(blogs[:20], filename, archive, True)
if __name__ == "__main__":
main()