forked from andrewnelder/hobo
/
hobo.py
210 lines (158 loc) · 6.18 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
209
###############################################################################
# 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 ConfigParser import ConfigParser
from lib.blogpost import BlogPost
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 #################################################
# 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)$''')
RE_ARTICLE_TITLE = re.compile(r'''(?:\:title )(.*?)[\n\r]''')
# Constants
CONFIG = ConfigParser({
'title': 'The Littlest Blog Engine',
'subtitle': '',
'postsperpage': 15,
'author': 'Hobo',
'summarydelim': '~~',
'heroku': 'on',
'disqus_shortname': ''})
CONFIG.read('config.ini')
TITLE = CONFIG.get('blog', 'title')
SUBTITLE = CONFIG.get('blog', 'subtitle')
POSTS_PER_PAGE = CONFIG.getint('blog', 'postsperpage')
AUTHOR = CONFIG.get('blog', 'author')
SUMMARY_DELIM = CONFIG.get('blog', 'summarydelim')
HEROKU = CONFIG.getboolean('blog', 'heroku')
DISQUS_SHORTNAME = CONFIG.get('blog', 'disqus_shortname')
POSTS = {}
KEY_LIST = []
# 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.
'''
global POSTS
global KEY_LIST
# 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')
# Extract metadata
slug = input_file.split('-', 3)[-1]
slug = RE_VALID_FILE_EXTENSIONS.sub('', slug)
article_title = slug
try:
article_title = RE_ARTICLE_TITLE.findall(contents)[0]
contents = RE_ARTICLE_TITLE.sub('', contents)
except:
LOGGER.warning('Ignoring file <%s>. Invalid metadata.' %
(input_file,))
continue
# 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_DELIM, contents): # Use delimiter
summary = contents.split(SUMMARY_DELIM)[0]
contents = re.sub(SUMMARY_DELIM, '', 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),\
title=article_title,\
author=AUTHOR,\
summary=html_summary,\
contents=html_contents,\
locator=locator\
)
# Remove the file
file_handle.close()
KEY_LIST = 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,
'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 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 #######################################################