1 # forestcms - Filesystem-Oriented reST CMS
2 # Or, for long, Filesystem-Oriented reStrcuturedText Content Management System
4 from __future__ import with_statement
8 from wsgiref.simple_server import make_server
9 from pprint import pprint
10 from StringIO import StringIO
11 from docutils.core import publish_parts
14 local = threading.local()
20 conf.docroot = '.' # 'docroot'
21 conf.sysroot = 'sysroot'
28 class HttpError (Exception):
29 def __init__(self, status):
33 if not page or page == '/':
35 return path.join(conf.docroot, page[1:]) + '.rst'
37 def status2rstfile(status):
38 return path.join(conf.sysroot, str(status)) + '.rst'
40 def render_rst(rst, whole=True):
41 what = 'whole' if whole else 'html_body'
42 content = publish_parts(rst, writer_name="html")[what]
43 return content.encode('utf-8')
45 def render_file(fname, whole=True):
47 with file(fname) as f:
50 if e.errno == errno.ENOENT:
52 return render_rst(rst, whole)
54 def render_page(page, whole=True):
55 fname = page2fname(page)
56 return render_file(fname, whole)
58 def forestcms(environ, start_response):
59 local.page = environ['PATH_INFO']
61 local.headers = [('Content-type', 'text/html; charset=utf-8')]
65 output = render_page(local.page)
68 output = render_file(status2rstfile(status))
69 status = '%s %s' % (status, STATUS[status])
70 start_response(status, local.headers)
73 httpd = make_server('', 8000, forestcms)