1. Marc Schlaich
  2. gbottle-wiki

Source

gbottle-wiki / app.py

#!/usr/bin/python
# coding: utf-8

'''
Prototype wiki app.
'''

import re

import bottle
from bottle import route, redirect, template
bottle.debug(True)

import markdown
MD_PARSER = markdown.Markdown(
    extensions=['codehilite(force_linenos=True)', 'toc',
               'wikilinks(base_url=/wiki/,end_url=)'],
    safe_mode='replace')

from gae import GAE_ENV

from model import db, get_page, create_page


WIKIWORD = re.compile(r'^([A-Z][a-z]+)+$')

__author__ = 'Marc Schlaich'
__version__ = '0.1.2'
    
@route('/')
@route('/wiki')
def index():
    redirect('/wiki/Home')


@route('/wiki/:page_name')
def wiki(page_name='Home'):
    if not WIKIWORD.match(page_name):
        return 'No wikiword.'
    page = get_page(page_name)
    if not page:
        create_page(page_name)
        return edit_wiki(page_name)
    else:
        html = MD_PARSER.convert(page.content)
        return template('wikipage', html=html, page_name=page_name)
    
@route('/wiki/:page_name', method='POST')
def save_content(page_name):
    new_content = bottle.request.POST.get('content', '')
    page = get_page(page_name)
    page.update_record(content=new_content)
    db.commit()
    return wiki(page_name)
    
@route('/wiki/:page_name/edit')
def edit_wiki(page_name):
    page = get_page(page_name)
    return '''<form method="POST" action="/wiki/%s">
    <textarea name="content" rows="20" cols="80">%s</textarea><br />
    <input type="submit" value="Save"> 
    </form>''' % (page_name, page.content)
    
# Route static files if not on GAE
if not GAE_ENV:
    @route('/css/:filename#.*#')
    def static_css(filename):
        return bottle.static_file(filename, root='css')

if __name__ == '__main__':
    if GAE_ENV:
        from google.appengine.ext.webapp import util 
        util.run_wsgi_app(bottle.default_app())
    else:
        bottle.run(reloader=True)