marry util.make and util.mako => excellent hack
[wrigit.git] / wrigit.py
blob728262f1bdf4dc207dc99c5aeea4e8b28fe5ccf1
1 #!/usr/bin/env python
2 # -*- mode: python -*-
4 import os, time, datetime
5 from os.path import join, abspath
7 import rst
8 from cfg import o, setup_cfg
9 cfg = setup_cfg([
10 o('-c', '--conf', dest='conf',
11 help='Configuration file'),
12 o('-r', '--rstdir', dest='rstdir',
13 help='Directory where source .rst files are found'),
14 o('-t', '--templatedir', dest='templatedir',
15 help='Directory where mako templates can be found'),
16 o('-b', '--baseurl', dest='baseurl',
17 help='The base URL of the site'),
18 o('-T', '--title', dest='title',
19 help='Site title that goes into <title>'),
20 o('', '--regen', dest='regen', action="store_true",
21 help='Regenerate the whole site (all files)'),
22 o('-d', '--disqus-forum', dest='disqus_forum',
23 help='The name of the disqus forum. For example, ' +
24 'if http://foo.disqus.com is the forum, then do --disqus-forum=foo'),
25 o('', '--blog-title', dest='blog.title',
26 help='Blog title'),
27 o('', '--blog-subtitle', dest='blog.subtitle',
28 help='Blog title'),
29 ], {'regen': False})
31 cfg['rstdir'] = abspath(cfg['rstdir'])
32 cfg['templatedir'] = abspath(cfg['templatedir'])
34 from util import make_mako
36 class Page(object):
37 """A rst page"""
39 def __init__(self, rstfile):
40 parts, meta = rst.process(rstfile)
42 self.rstfile = rstfile
43 self.title = parts['title']
44 self.fragment = parts['fragment']
45 self.meta = meta
47 def title_html(self):
48 return '%s - %s' % (self.title, cfg['title'])
50 def date(self):
51 if '|' in self.meta['date']:
52 format = '%Y-%m-%d|%H:%M'
53 else:
54 format = '%Y-%m-%d'
56 return datetime.datetime(
57 *time.strptime(self.meta['date'],
58 format)[0:6])
60 def tags(self):
61 tags = self.meta.get('tags', '').split()
62 tags.sort()
63 return tags
65 def url(self):
66 path_info = rst.htmlpath(self.rstfile[len(cfg['rstdir']):])
67 return cfg['baseurl'] + path_info
69 def commentable(self):
70 return False
72 class BlogPost(Page):
74 def title_html(self):
75 return self.title
77 def commentable(self):
78 return True
80 def __cmp__(self, other):
81 return cmp(self.date(), other.date())
83 def __repr__(self):
84 return '<BlogPost "%s" on %s>' % (self.title, self.date())
87 if __name__ == '__main__':
88 # Import Psyco if available
89 try:
90 import psyco
91 psyco.full()
92 except ImportError:
93 pass
95 print '[%s - %s] rstdir:%s [using templatedir:%s]' % (
96 cfg['baseurl'], cfg['title'], cfg['rstdir'], cfg['templatedir'])
98 rstdir = cfg['rstdir']
99 blog_posts = []
100 blog_path = join(rstdir, 'blog')
102 for f in rst.rstfiles(rstdir):
103 if blog_path in f:
104 page = BlogPost(f)
105 blog_posts.append(page)
106 else:
107 page = Page(f)
109 make_mako(rst.htmlpath(f),
110 [f],
111 'page.mako')
113 blog_posts.sort()
114 blog_posts.reverse()
116 # generate blog index
117 make_mako(join(rstdir, 'blog', 'index.html'),
118 [p.rstfile for p in blog_posts],
119 'blog/index.mako')
121 # generate blog feed
122 recent_blog_posts = blog_posts[:10]
123 make_mako(join(rstdir, 'blog', 'atom.xml'),
124 [p.rstfile for p in recent_blog_posts],
125 'blog/atom.xml.mako')