Skip to content
This repository was archived by the owner on Dec 7, 2018. It is now read-only.

Commit 3db4da1

Browse files
committed
Support for templating
1 parent 5051a86 commit 3db4da1

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

TODO.md

-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
# TODO
22

3-
## Must Have
4-
* Templating
5-
63
## Nice to Have
74
* Diff between objects

gitblog.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
# Output formatting
5353
from markdown2 import markdown
5454
from BeautifulSoup import BeautifulSoup
55+
from string import Template
5556

5657
# Available output text encodings (request parameter key => output format)
5758
available_output_type = { 'html': 'html',
@@ -67,6 +68,7 @@ def handler(req):
6768
config = {
6869
'gitblog.report_errors': 'False',
6970
'gitblog.www_repo': '/dev/null',
71+
'gitblog.template_path': 'templates',
7072
'gitblog.default_ref': 'HEAD',
7173
'gitblog.default_output_type': 'html',
7274
'gitblog.footer': 'True',
@@ -220,10 +222,10 @@ def handler(req):
220222
for i, l in enumerate(requested_path[0:-1]):
221223
breadcrumb += '[%s](/%s)/' % (l, '/'.join(requested_path[:i+1]))
222224

223-
content += '\n\n---\n'
225+
footer = '\n\n---\n'
224226
if not output_type == 'plain':
225-
content += '[Home](/) - '
226-
content += '%s%s - Updated on %s by %s - Git Reference [%s](?ref=%s)\n' % \
227+
footer += '[Home](/) - '
228+
footer += '%s%s - Updated on %s by %s - Git Reference [%s](?ref=%s)\n' % \
227229
(breadcrumb, last_path_entry,
228230
datetime.fromtimestamp(git_obj.committed_date).strftime(
229231
config['gitblog.date_format']),
@@ -233,25 +235,35 @@ def handler(req):
233235
# Return markdown
234236
if output_type == 'markdown':
235237
req.headers_out.add('Content-Type', 'text/markdown; charset=UTF-8')
236-
req.headers_out.add('Content-Length', str(len(content)))
237-
req.write(content)
238+
req.headers_out.add('Content-Length', str(len(content) + len(footer)))
239+
req.write(content + footer)
238240
return(apache.OK)
239241

240242
# Convert markdown to html
241243
content = markdown(content, extras=config['gitblog.markdown2_extras'])
244+
footer = markdown(footer, extras=config['gitblog.markdown2_extras'])
242245

243246
# Return plain
244247
if output_type == 'plain':
245248
content = ''.join(BeautifulSoup(content).findAll(text=True))
246249
req.headers_out.add('Content-Type', 'text/plain; charset=UTF-8')
247-
req.headers_out.add('Content-Length', str(len(content)))
248-
req.write(content)
250+
req.headers_out.add('Content-Length', str(len(content) + len(footer)))
251+
req.write(content + footer)
249252
return(apache.OK)
250253

254+
# Add templating
255+
if not config['gitblog.template_path'] == '':
256+
try:
257+
template = git_obj.tree[config['gitblog.template_path'] + '/site.tpl'].data_stream.read()
258+
content = Template(template).safe_substitute(dict(content = content, footer = footer))
259+
except:
260+
content = content + footer
261+
content = content.encode('utf-8')
262+
251263
# Return html
252264
req.headers_out.add('Content-Type', 'text/html; charset=UTF-8')
253265
req.headers_out.add('Content-Length', str(len(content)))
254-
req.write(content.encode('utf-8'))
266+
req.write(content)
255267
return(apache.OK)
256268

257269
# vim: set syntax=python tabstop=4 expandtab:

0 commit comments

Comments
 (0)