forked from andrewnelder/hobo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hobo.py
209 lines (157 loc) · 6.42 KB
/
hobo.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
###############################################################################
# Copyright (c) 2012 Andrew Nelder. Licensed under the MIT License. #
# See LICENSE.txt for full details. #
###############################################################################
# I M P O R T S ###############################################################
import logging
import re
import os
import datetime
from collections import namedtuple
import blog_config
from lib.markdown2 import markdown
from lib.bottle import route, run, view, template, error, static_file, abort
# I N I T I A L I Z A T I O N #################################################
# Named tuple pseudo-class
BlogPost = namedtuple('BlogPost', 'date, meta, summary, contents, locator')
# Logger
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
LOGGER = logging.getLogger()
LOGGER.addHandler(ch)
# Regular Expressions
RE_VALID_FILE_EXTENSIONS = re.compile(r'''(?:.md|.markdown|.txt)$''')
# Constants
POSTS = {}
KEY_LIST = []
# Configuration Parameters w/ Defaults
TITLE = blog_config.TITLE \
if hasattr(blog_config, 'TITLE') else ''
SUBTITLE = blog_config.SUBTITLE \
if hasattr(blog_config, 'SUBTITLE') else ''
AUTHOR = blog_config.AUTHOR \
if hasattr(blog_config, 'AUTHOR') else ''
METADATA_TAGS = blog_config.METADATA_TAGS \
if hasattr(blog_config, 'METADATA_TAGS') else ['title', 'author']
POSTS_PER_PAGE = blog_config.POSTS_PER_PAGE \
if hasattr(blog_config, 'POSTS_PER_PAGE') else 15
SUMMARY_DELIMITER = blog_config.SUMMARY_DELIMITER \
if hasattr(blog_config, 'SUMMARY_DELIMITER') else '~~'
USE_HEROKU = blog_config.USE_HEROKU \
if hasattr(blog_config, 'USE_HEROKU') else True
DISQUS_SHORTNAME = blog_config.DISQUS_SHORTNAME \
if hasattr(blog_config, 'DISQUS_SHORTNAME') else ''
# F U N C T I O N S ###########################################################
def process_blog_posts():
'''
Migrates the text files contained in the 'posts' directory to the database.
These files will have their meta-data removed and their markdown
interpreted and converted to HTML.
NOTE: This is only run once -- when the server starts.
'''
# Open every blog post
path = 'posts/'
for input_file in os.listdir(path):
if RE_VALID_FILE_EXTENSIONS.search(input_file):
# Extract date from filename
try:
(yy, mm, dd) = input_file.split('-')[:3]
yy = int('20' + yy) if len(yy) is 2 else int(yy)
mm = int(mm)
dd = int(dd)
except:
LOGGER.warning('Ignoring file <%s>. Invalid formatting.' %
(input_file,))
continue
# Validate date
if yy > 2500 or mm > 12 or dd > 31:
LOGGER.warning('Ignoring file <%s>. Invalid date range.' %
(input_file,))
continue
# Open the file
file_handle = open(path + input_file, 'r')
contents = file_handle.read().decode('utf-8')
# Find the slug
slug = input_file.split('-', 3)[-1]
slug = RE_VALID_FILE_EXTENSIONS.sub('', slug)
# Extract metadata
meta = {'title': slug, 'author': AUTHOR}
for tag in METADATA_TAGS:
re_meta = re.compile(r'''(?:\:%s )(.*?)[\n\r]''' % tag)
if re_meta.search(contents):
meta[tag] = re_meta.findall(contents)[0]
contents = re_meta.sub('', contents)
# Strip the contents of supurfluous whitespace -- now that the
# metatags have been removed.
contents = contents.strip()
# Populate the summary
# Look for the SUMMARY_DELIM character sequence and use it to
# form the summary, if it exists. Otherwise, simply take the first
# paragraph of the post.
summary = None
if re.search(SUMMARY_DELIMITER, contents): # Use delimiter
summary = contents.split(SUMMARY_DELIMITER)[0]
contents = re.sub(SUMMARY_DELIMITER, '', contents)
else: # Use first paragraph
summary = re.split(r'''[\r\n]{2}''', contents)[0]
html_summary = markdown(summary)
locator = '/%04d/%02d/%02d/%s' % (yy, mm, dd, slug, )
# Enter the file into the database
html_contents = markdown(contents)
POSTS[locator] = \
BlogPost(\
date=datetime.date(yy, mm, dd),\
meta=meta,\
summary=html_summary,\
contents=html_contents,\
locator=locator\
)
# Remove the file
file_handle.close()
KEY_LIST.extend(POSTS.keys())
KEY_LIST.sort(reverse=True)
# P A G E R O U T I N G #####################################################
@route('/')
@route('/<page:int>')
@view('index')
def index(page=0):
posts = [POSTS[key] for key in \
KEY_LIST[page * POSTS_PER_PAGE:(page + 1) * POSTS_PER_PAGE]]
return {'title': TITLE,
'subtitle': SUBTITLE,
'page': page,
'posts': posts,
'has_prev': (page > 0),
'has_next': (len(POSTS) > (page + 1) * POSTS_PER_PAGE)}
@route('/<yy:int>/<mm:int>/<dd:int>/<slug>')
@view('readpost')
def readpost(yy, mm, dd, slug):
locator = '/%04d/%02d/%02d/%s' % (yy, mm, dd, slug, )
if locator not in POSTS:
abort(404, 'Article not found!')
post = POSTS[locator]
return {'title': TITLE,
'post': post,
'disqus_shortname': DISQUS_SHORTNAME}
@route('/files/<filepath:path>')
@route('/static/<filepath:path>')
def server_static(filepath):
return static_file(filepath, root='files')
@route('/favicon.ico')
def favicon():
return static_file('favicon.ico', root='files')
@error(403)
@view('error')
def error403(code=None):
return {'code': code}
@error(404)
@view('error')
def error404(code=None):
return {'code': code}
# E X E C U T I O N ###########################################################
process_blog_posts()
if USE_HEROKU:
run(host="0.0.0.0", port=int(os.environ.get("PORT", 80)))
else:
run(host="localhost", port=8080)
# E N D O F F I L E #######################################################