creole - WikiCreole parser written in Python

version:
1.0.1
depending module:
topdwon
license:
MIT
published date:
Jun 11 2011
repository:
https://bitbucket.org/ckuwata/creole

example:

Very simple stand alone wiki server.

import creole

import os
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler

ENCOING = 'utf-8'
PATH = '.'
class WikiRequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        if '?' in self.path:
            path, query = self.path.split('?', 1)
        else:
            path = self.path
            query = ''
        if path == '/':
            self.list_directories()
            return
        if not os.path.exists(PATH + path):
            self.send_error(404)
            return
        if query == 'mode=edit':
            c = open(PATH + path).read()
            form = u"""
            <form action="/" method=POST>
            <input type="hidden" name="name" value="%s">
            <textarea rows=40 cols=60 name="body">%s</textarea><br>
            <input type="submit">
            </form>
            <a href="%s">back</a>
            """ % (path.strip('/').rsplit('.', 1)[0], c, path)
            self.response(path, form)
        else:
            processor = creole.Processor(wiki_url='/')
            body = processor.process(unicode(open(PATH + path).read(), ENCOING))
            footer = """
            <hr/>
            <p><a href="%s?mode=edit">edit</a> |
            <a href="./">index</a></p>
            """ % path
            self.response(path, body + footer)

    def do_POST(self):
        import urlparse
        length = self.headers.getheader('content-length')
        input = self.rfile.read(int(length))
        req = urlparse.parse_qs(input)
        path = req['name'][0] + '.wiki'
        body = req['body'][0]
        open(PATH + '/' + path, 'wb').write(body)
        self.send_response(302)
        self.send_header("Location", '/' + path)

    def list_directories(self):
        buff = []
        for p in os.listdir(PATH):
            if p.endswith('.wiki'):
                buff.append('<li><a href="%s">%s</a></li>' % (p, p))
        form =  """<hr>
        <form action="/" method=POST>
            <input type="text" name="name"><br>
            <textarea rows=40 cols=60 name="body"></textarea><br>
            <input type="submit">
        </form>"""
        buff.append(form)
        self.response('index', '<h1>index</h1><ul>%s</ul>' % ('\n'.join(buff)))

    def response(self, title, body):
        res = (u"""<html>
    <head>
        <title>%s</title>
    </head>
    <body>%s</body>
</html>""" % (title, body)).encode(ENCOING)
        self.send_response(200)
        self.send_header("Content-type", 'text/html')
        self.send_header("Content-Length", len(res))
        self.end_headers()
        self.wfile.write(res)
        self.wfile.flush()

def main():
    httpd = HTTPServer(('', 8080), WikiRequestHandler)
    sa = httpd.socket.getsockname()
    print "Serving HTTP on", sa[0], "port", sa[1], "..."
    httpd.serve_forever()

if __name__ == '__main__':
    main()

ChangeLog

Jun 11 2011
version 1.0.1
fixed some imcompatibility to Creole 1.0 specification
Nov 1 2010
version 1.0