/
make.py
executable file
·337 lines (280 loc) · 9.09 KB
/
make.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env python
# -*- coding: utf-8
import os
import re
import datetime
import yaml
import codecs
import markdown2
from cStringIO import StringIO
from jinja2 import Environment, FileSystemLoader
CONFIG = {
'content_root': 'content',
'output_to': '.',
'templates': 'templates',
'date_format': '%Y-%m-%d',
'base_url': '/',
'http_port': 3030,
'content_encoding': 'utf-8',
}
GLOBAL_TEMPLATE_CONTEXT = {
'media_url': '/media/',
'site_url' : 'http://cloudartisan.com',
'last_build' : datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")
}
KINDS = {
'post': {
'name': 'post', 'name_plural': 'posts',
},
'note': {
'name': 'note', 'name_plural': 'notes',
},
'link': {
'name': 'link', 'name_plural': 'links',
},
'photo': {
'name': 'photo', 'name_plural': 'photos',
},
'page': {
'name': 'page', 'name_plural': 'pages',
},
}
JINJA_ENV = Environment(loader=FileSystemLoader(CONFIG['templates']))
class Tag(object):
def __init__(self, name):
super(Tag, self).__init__()
self.name = name
self.prepare()
def prepare(self):
_slug = self.name.lower()
_slug = re.sub(r'[;;,. ]', '-', _slug)
self.slug = _slug
class Entry(object):
def __init__(self, path):
super(Entry, self).__init__()
path = path.split('content/')[-1]
self.path = path
self.prepare()
def __str__(self):
return self.path
def __repr__(self):
return self.path
@property
def name(self):
return os.path.splitext(os.path.basename(self.path))[0]
@property
def abspath(self):
return os.path.abspath(os.path.join(CONFIG['content_root'], self.path))
@property
def destination(self):
dest = "%s/%s/index.html" % (KINDS[self.kind]['name_plural'], self.name)
return os.path.join(CONFIG['output_to'], dest)
@property
def title(self):
return self.header['title']
@property
def summary_html(self):
return "%s" % markdown2.markdown(self.header.get('summary').strip())
@property
def credits_html(self):
return "%s" % markdown2.markdown(self.header.get('credits').strip())
@property
def summary_atom(self):
if self.kind == 'link':
more = '<a href="%s%s"> continue reading on %s...</a>' % (GLOBAL_TEMPLATE_CONTEXT['site_url'], self.permalink, self.domain_name)
else:
more = '<a href="%s%s"> continue reading...</a>' % (GLOBAL_TEMPLATE_CONTEXT['site_url'], self.permalink)
return "%s%s" % (
markdown2.markdown(self.header.get('summary').strip()),
more,
)
@property
def published_html(self):
if self.kind in ['link', 'note', 'photo']:
return self.header['published'].strftime("%B %d, %Y %I:%M %p")
return self.header['published'].strftime("%B %d, %Y")
@property
def published_atom(self):
return self.published.strftime("%Y-%m-%dT%H:%M:%SZ")
@property
def atom_id(self):
return "tag:cloudartisan.com,%s:%s" % (
self.published.strftime("%Y-%m-%d"),
self.permalink,
)
@property
def body_html(self):
return markdown2.markdown(self.body, extras=['code-color'])
@property
def permalink(self):
return "/%s/%s" % (KINDS[self.kind]['name_plural'], self.name)
@property
def tags(self):
tags = list()
for t in self.header['tags']:
tags.append(Tag(t))
return tags
def prepare(self):
f = codecs.open(self.abspath, 'r')
header = ['---']
line = f.readline()
line = line.rstrip()
while line:
header.append(line)
line = f.readline()
line = line.rstrip()
self.header = yaml.load(StringIO('\n'.join(header)))
for h in self.header.items():
if h:
try:
setattr(self, h[0], h[1])
except:
pass
body = list()
for line in f.readlines():
body.append(line)
self.body = '\n'.join(body)
f.close()
if self.kind == 'link':
from urlparse import urlparse
self.domain_name = urlparse(self.url).netloc
elif self.kind == 'photo':
pass
elif self.kind == 'note':
pass
elif self.kind == 'writing':
pass
def render(self):
if not self.header.has_key('public') or not self.header['public']:
return False
try:
os.makedirs(os.path.dirname(self.destination))
except:
pass
context = GLOBAL_TEMPLATE_CONTEXT.copy()
context['entry'] = self
template = JINJA_ENV.get_template("entry.html")
html = template.render(context)
destination = codecs.open(self.destination, 'w', CONFIG['content_encoding'])
destination.write(html)
destination.close()
return True
class Link(Entry):
def __init__(self, path):
super(Link, self).__init__(path)
@property
def permalink(self):
return self.url
def entry_factory():
pass
def _sort_entries(entries):
_entries = dict()
sorted_entries = list()
for entry in entries:
_published = entry.header['published'].isoformat()
_entries[_published] = entry
sorted_keys = sorted(_entries.keys())
sorted_keys.reverse()
for key in sorted_keys:
sorted_entries.append(_entries[key])
return sorted_entries
def render_index(entries):
context = GLOBAL_TEMPLATE_CONTEXT.copy()
context['entries'] = entries
template = JINJA_ENV.get_template('entry_index.html')
html = template.render(context)
destination = codecs.open("%s/index.html" % CONFIG['output_to'], 'w', CONFIG['content_encoding'])
destination.write(html)
destination.close()
def render_about():
context = GLOBAL_TEMPLATE_CONTEXT.copy()
template = JINJA_ENV.get_template('about.html')
html = template.render(context)
destination = codecs.open("about/index.html", 'w', CONFIG['content_encoding'])
destination.write(html)
destination.close()
def render_atom_feed(entries, render_to=None):
context = GLOBAL_TEMPLATE_CONTEXT.copy()
context['entries'] = entries[:10]
template = JINJA_ENV.get_template('atom.xml')
html = template.render(context)
if not render_to:
render_to = "%s/atom.xml" % CONFIG['output_to']
destination = codecs.open(render_to, 'w', CONFIG['content_encoding'])
destination.write(html)
destination.close()
def render_tag_pages(tag_tree):
context = GLOBAL_TEMPLATE_CONTEXT.copy()
for t in tag_tree.items():
context['tag'] = t[1]['tag']
context['entries'] = _sort_entries(t[1]['entries'])
destination = "%s/tags/%s" % (CONFIG['output_to'], context['tag'].slug)
try:
os.makedirs(destination)
except:
pass
template = JINJA_ENV.get_template('tag_index.html')
html = template.render(context)
f = codecs.open("%s/index.html" % destination, 'w', CONFIG['content_encoding'])
f.write(html)
f.close()
print " tags/%s" % (context['tag'].slug, )
render_atom_feed(context['entries'], render_to="%s/atom.xml" % destination)
def build():
print
print "Rendering website now..."
print
print " entries:"
entries = list()
tags = dict()
for root, dirs, files in os.walk(CONFIG['content_root']):
for filename in files:
if not os.path.splitext(filename)[-1] in ('.markdown', '.md'):
continue
entry = Entry(os.path.join(root, filename))
if entry.render():
entries.append(entry)
for tag in entry.tags:
if not tags.has_key(tag.name):
tags[tag.name] = {
'tag': tag,
'entries': list(),
}
tags[tag.name]['entries'].append(entry)
print " %s" % entry.path
print " :done"
print
print " tag pages & their atom feeds:"
render_tag_pages(tags)
print " :done"
print
print " site wide index",
render_index(_sort_entries(entries))
print "................ done"
print
print " about page",
render_about()
print "................ done"
print
print " site wide atom feeds",
render_atom_feed(_sort_entries(entries))
print "................ done"
print
print "All done",
if __name__== '__main__':
print
build()
import SimpleHTTPServer
import SocketServer
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", CONFIG['http_port']), Handler)
os.chdir(CONFIG['output_to'])
print "and ready to test at http://127.0.0.1:%d" % CONFIG['http_port']
print "Hit Ctrl+C to exit"
try:
httpd.serve_forever()
except KeyboardInterrupt:
print
print "Shutting Down... Bye!. ☻ ☻ ☻ "
print
httpd.server_close()