-
Notifications
You must be signed in to change notification settings - Fork 0
/
chisel.py
executable file
·169 lines (153 loc) · 5.59 KB
/
chisel.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
# encoding: utf-8
#!/usr/bin/env python
# Chisel
# David Zhou, github.com/dz
#
# Updates and enhancements added/included by ckunte:
# 14.05.2012:
# - RSS feed generator script, hat-tip: Ronan Jouchet, github.com/ronjouch
# - Smartypants content parsing included by ckunte, github.com/ckunte
# - Permalink url updated to include only the year followed by post title,
# i.e., http://staticsite.com/2012/post.html or http://staticsite.com/2012/post
#
# Requires:
# jinja2 mdx_smartypants, PyRSS2Gen
import sys, re, time, os, codecs
import jinja2, markdown, mdx_smartypants, PyRSS2Gen
import datetime
#Settings
# For folders that look like the following (this is primarily
# done to keep native post files and the generated html files
# separate; don't have to be -- just my personal preference):
# Sites
# /chisel (the generator)
# /posts (in markdown stored offline)
# /antinode (the generated html site from post files)
# the locations are used thus:
#
BASEURL = "http://ckunte.net/log/" #end with slash
# The following tells chisel where to look for native posts:
SOURCE = "/Users/ckunte/Sites/posts/" #end with slash
# The following tells chisel where to generate site:
DESTINATION = "/Users/ckunte/Sites/ckunte.github.io/log/" #end with slash
HOME_SHOW = 3 #numer of entries to show on homepage
TEMPLATE_PATH = "/Users/ckunte/Sites/chisel/themevanilla/" #end with slash
TEMPLATE_OPTIONS = {}
TEMPLATES = {
'home': "home.html",
'detail': "detail.html",
'archive': "archive.html",
'colophon': "colophon.html",
'404': "404.html",
}
TIME_FORMAT = "%B %d, %Y"
ENTRY_TIME_FORMAT = "%m/%d/%Y"
#FORMAT should be a callable that takes in text
#and returns formatted text
FORMAT = lambda text: markdown.markdown(text, ['footnotes','smartypants',])
# default URLEXT = ".html"
# set URLEXT = "" if server recognizes .html URLs and can be linked-to without the extension part.
URLEXT = ""
# default PATHEXT = ""
# set PATHEXT = "" if URLEXT = ".html" and vice versa.
PATHEXT = ".html"
RSS = PyRSS2Gen.RSS2(
title = "ckunte.net log",
link = BASEURL + "rss.xml",
description = "Journal by Chyetanya Kunte.",
lastBuildDate = datetime.datetime.now(),
items = [])
#########
STEPS = []
def step(func):
def wrapper(*args, **kwargs):
print "Starting " + func.__name__ + "...",
func(*args, **kwargs)
print "Done."
STEPS.append(wrapper)
return wrapper
def get_tree(source):
files = []
for root, ds, fs in os.walk(source):
for name in fs:
if name[0] == ".": continue
path = os.path.join(root, name)
f = open(path, "rU")
title = f.readline()
date = time.strptime(f.readline().strip(), ENTRY_TIME_FORMAT)
year, month, day = date[:3]
files.append({
'title': title,
'epoch': time.mktime(date),
'content': FORMAT(''.join(f.readlines()[1:]).decode('UTF-8')),
#'url': '/'.join([str(year), "%.2d" % month, "%.2d" % day, os.path.splitext(name)[0] + ".html"]),
# Uncheck the following line if you have no rewrite (URLs end with .html).
'url': '/'.join([str(year), os.path.splitext(name)[0] + URLEXT]),
'pretty_date': time.strftime(TIME_FORMAT, date),
'date': date,
'year': year,
'month': month,
'day': day,
'filename': name,
})
f.close()
return files
def compare_entries(x, y):
result = cmp(-x['epoch'], -y['epoch'])
if result == 0:
return -cmp(x['filename'], y['filename'])
return result
def write_file(url, data):
path = DESTINATION + url + PATHEXT
dirs = os.path.dirname(path)
if not os.path.isdir(dirs):
os.makedirs(dirs)
file = open(path, "w")
file.write(data.encode('UTF-8'))
file.close()
@step
def generate_homepage(f, e):
"""Generate homepage"""
template = e.get_template(TEMPLATES['home'])
write_file("index" + URLEXT, template.render(entries=f[:HOME_SHOW]))
@step
def generate_rss(f, e):
"""Generate rss"""
for file in f[:HOME_SHOW]:
RSS.items.append(PyRSS2Gen.RSSItem(title=file['title'], link=BASEURL + file['url'], description=file['content'], author="Chyetanya Kunte", guid = PyRSS2Gen.Guid(BASEURL + file['url']), pubDate=datetime.datetime(file['year'], file['month'], file['day'])))
RSS.write_xml(open(DESTINATION + "rss.xml", "w"))
@step
def master_archive(f, e):
"""Generate master archive list of all entries"""
template = e.get_template(TEMPLATES['archive'])
write_file("archive" + URLEXT, template.render(entries=f))
@step
def generate_colophon(f, e):
"""Generate a colophon page"""
template = e.get_template(TEMPLATES['colophon'])
write_file("colophon" + URLEXT, template.render(entries=f))
@step
def generate_404(f, e):
"""Generate a 404 page"""
template = e.get_template(TEMPLATES['404'])
write_file("404" + URLEXT, template.render(entries=f))
@step
def detail_pages(f, e):
"""Generate detail pages of individual posts"""
template = e.get_template(TEMPLATES['detail'])
for file in f:
write_file(file['url'], template.render(entry=file))
def main():
print "Chiseling..."
print "\tReading files...",
files = sorted(get_tree(SOURCE), cmp=compare_entries)
env = jinja2.Environment(loader=jinja2.FileSystemLoader(TEMPLATE_PATH), **TEMPLATE_OPTIONS)
print "Done."
print "\tRunning steps..."
for step in STEPS:
print "\t\t",
step(files, env)
print "\tDone."
print "Done."
if __name__ == "__main__":
sys.exit(main())