-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjemblog.py
269 lines (252 loc) · 8.27 KB
/
jemblog.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/python
# author: travis johnson ([email protected])
# date: sept 4, 2011
#
# Copyright (C) 2011 Travis Johnson
# jemblog is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# jemblog is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
import glob
short_months = ["", "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]
really_short_months = " JFMAMJJASOND"
# first, just read in the entry files
blogEntries = []
entryNum = 0
for fileName in glob.glob("entry*.txt"):
f = open(fileName)
title = f.readline()
author = f.readline()
date = f.readline()
category = f.readline()
content = f.readlines()[1:]
junk, title = title[:-1].split(": ",1)
junk, author = author[:-1].split(": ",1)
junk, date = date[:-1].split(": ",1)
junk, category = category[:-1].split(": ",1)
blogEntries.append( (entryNum, title, author, date, category, "".join(content)) )
entryNum += 1
# need to reverse so that newest blog posts show up at the top
blogEntries.reverse()
# figure out how many pages are needed
pageNum,entryNum = 0,0
pageDict = {}
for entry in blogEntries:
# do blog0...n
if pageNum in pageDict:
pageDict[pageNum].append(entry)
else:
pageDict[pageNum] = [entry]
if entryNum % 10 == 9:
pageNum += 1
entryNum += 1
lastPage = len(blogEntries)/10
def friendly(str):
from re import sub
return sub("'|:|!|&|/|\/|\(|\)|\?|,", "", str).replace(" ", "_").replace("__","_").lower()
#return str.replace(" ", "_").replace("'","").replace(":","").replace("!","").replace("&", "and").replace("/","_or_").lower()
dates = {}
byMonthDict = {}
categories = {}
#for entry in blogEntries:
for i in range(len(blogEntries)):
entryNum, title, author, date, category, content = blogEntries[i]
year, month, day = date.split("/")
#print year, month, day
if year in dates:
if month not in dates[year]:
dates[year].append(month)
else:
dates[year] = [month]
byMonth = "%s_%s"%(year, short_months[int(month)])
#print "from %s %s %s, I got %d then %s"%(year,month,day,int(month),byMonth)
if byMonth in byMonthDict:
byMonthDict[byMonth].append(entry)
else:
byMonthDict[byMonth] = [entry]
if category in categories:
categories[category].append(entry)
else:
categories[category] = [entry]
date = date.replace("/", "\/")
filetitle = friendly(title)
f=open(filetitle+".jemdoc",'w')
f.write("# jemdoc: menu{MENU2}{index.html}\n")
f.write("= %s\n"%(title))
f.write("== from %s\n\n"%(date))
f.write(content)
f.write("\n")
if i+1 != len(blogEntries):
prevtitle = friendly(blogEntries[i+1][1])
f.write("[%s.html older]\n"%(prevtitle))
else:
f.write("older\n");
#print entryNum, len(blogEntries)
if i != 0:
nexttitle = friendly(blogEntries[i-1][1])
f.write("[%s.html newer]\n"%(nexttitle))
else:
f.write("newer\n");
f.close()
# Setup the menu file.
MENU = open("MENU2", 'w')
MENU.write("navigation\n")
MENU.write("\tback to main site [..]\n")
MENU.write("\tfirst blog page [index.html]\n")
MENU.write("by month\n")
years = dates.keys()
years.sort()
for year in years:
MENU.write("\t" + year + ": ")
for month in range(1,13):
mStr = "%0.2d"%(month)
if mStr in dates[year]:
MENU.write("[%s_%s.html %s] "%(year, short_months[month],really_short_months[month]))
else:
MENU.write(really_short_months[month] + " ")
MENU.write("\n")
MENU.write("by category\n")
for cat in categories.iterkeys():
MENU.write("\t%s\t\t[category_%s.html]\n"%(cat,cat))
MENU.write("other\n")
MENU.write("\tjemblog [https://github.com/traviscj/jemblog]\n")
MENU.write("\trss [rss.xml]\n")
# setup the main blog layout
for page, entries in pageDict.iteritems():
if page==0:
fileName = "index.jemdoc"
else:
fileName = "index%d.jemdoc"%page
f=open(fileName,'w')
f.write("# jemdoc: menu{MENU2}{index.html}\n")
f.write("= Blog, page %d\n"%(page))
f.write("from the desk of travis johnson.\n\n");
for entry in entries:
entryNum, title, author, date, category, content = entry
date = date.replace("/", "\/")
f.write("== [%s.html %s] (from %s)\n"%(friendly(title), title, date))
f.write(content)
f.write("\n")
f.write("\n\n")
f.write("~~~\n")
f.write("{}{table}{bottomnav}\n")
if page > 1:
f.write("[index%d.html newer page]\n"%(page-1))
elif page == 1:
f.write("[index.html newer page]\n")
else:
f.write("newer page\n")
first, last = "newest", "[index%d.html oldest]"%(lastPage)
if page != 0:
first = "[index.html newest]"
if page == lastPage:
last = "last"
f.write(" | %s %s | "%(first,last))
if page != lastPage:
f.write("[index%d.html older page]\n"%(page+1))
else:
f.write("older page")
f.write("\n")
f.write("~~~\n")
f.close()
#print byMonthDict
# month view... last thing!
yearmonthList = byMonthDict.keys()
for yearmonth in yearmonthList:
entries = byMonthDict[yearmonth]
#print yearmonth
f=open("%s.jemdoc"%(yearmonth),'w')
f.write("# jemdoc: menu{MENU2}{%s.html}\n"%(yearmonth))
f.write("= Blog, by date: %s\n"%(yearmonth))
f.write("from the desk of travis johnson.\n\n")
for entry in entries:
entryNum, title, author, date, category, content = entry
date = date.replace("/", "\/")
f.write("== [%s.html %s] (from %s)\n"%(friendly(title), title, date))
f.write(content)
f.write("\n")
f.close()
catList = categories.keys()
catList.sort()
for category in catList:
entries = categories[category]
#print yearmonth
f=open("category_%s.jemdoc"%(category),'w')
f.write("# jemdoc: menu{MENU2}{%s.html}\n"%(category))
f.write("= Blog, by category: %s\n"%(category))
f.write("from the desk of travis johnson.\n\n")
for entry in entries:
entryNum, title, author, date, category, content = entry
date = date.replace("/", "\/")
f.write("== [%s.html %s] (from %s)\n"%(friendly(title), title, date))
f.write(content)
f.write("\n")
f.close()
html_escape_table = {
"&": "&",
'"': """,
"'": "'",
">": ">",
"<": "<",
}
def html_escape(text):
"""Produce entities within text."""
return "".join(html_escape_table.get(c,c) for c in text)
import datetime, tempfile
rss = open("html/rss.xml",'w')
rssDate = "%a, %d %b %Y %X +0000"
lastYearS,lastMonthS,lastDayS = blogEntries[0][3].split("/")
lastYear,lastMonth,lastDay = int(lastYearS), int(lastMonthS), int(lastDayS)
rss.write("""<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>%s</title>
<description>%s</description>
<link>%s</link>
<lastBuildDate>%s</lastBuildDate>
<pubDate>%s</pubDate>""" % ("~traviscj/", "feed for traviscj.com/blog", "http://traviscj.com/blog",
datetime.datetime.utcnow().strftime(rssDate),datetime.date(lastYear,lastMonth,lastDay).strftime(rssDate)))
import subprocess,os
from xml.sax.saxutils import escape
for entry in blogEntries[0:10]:
entryNum, title, author, date, category, content = entry
yearS, monthS, dayS = date.split("/")
year,month,day = int(yearS), int(monthS), int(dayS)
print title
title = escape(title)
print title
#content = html_escape(content)
tmpfd,tmpFileName = tempfile.mkstemp()
parsedfd,parsedFileName = tempfile.mkstemp()
tmpFile = open(tmpFileName,'w')
parsedFile = open(parsedFileName)
tmpFile.write("# jemdoc: \n")
tmpFile.write(content)
tmpFile.close()
subprocess.call(['/usr/bin/python','jemdoc.py','-o',parsedFile.name,tmpFile.name])
formattedLines = parsedFile.readlines()
parsedFile.close()
#print parsedFileName, tmpFileName
os.remove(parsedFileName)
os.remove(tmpFileName)
formattedcontent = "\n".join(formattedLines[10:-2])
formattedcontent = escape(formattedcontent)
rss.write("""
<item>
<title>%s</title>
<description>%s</description>
<link>%s</link>
<guid>%s</guid>
<pubDate>%s </pubDate>
</item>
""" % (title, formattedcontent, "http://traviscj.com/blog/"+friendly(title)+".html", friendly(title), datetime.date(year,month,day).strftime(rssDate)))
rss.write("""
</channel>
</rss>""")