ittywiki / ittywiki.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from settings import ROOT_PATH, STATIC_PATH, DATA_PATH
from formatter import get_formatted_doc, get_formatted_doc_from_string, get_formatted_s5_doc

import mimetypes
import fnmatch
import os

from itty import *

@get('/')
def root_redirect(request):
    return Response('', status=302, content_type='text/plain',
                    headers=[('Location', '/IndexPage')])


def _static_asset_response(sfile, asset_name):
    content_type = 'text/plain'
    content_type_guess = mimetypes.guess_type(asset_name)
    if(content_type_guess[0] is not None):
        content_type = content_type_guess[0]

    return Response(sfile, content_type=content_type)


@get('/static/(?P<asset_name>.+)')
def static_asset(request, asset_name):
    sfile = static_file(asset_name, root=STATIC_PATH)

    return _static_asset_response(sfile, asset_name)


#hack to get s5 static assets working
@get('/ui/(?P<asset_name>.+)')
def static_s5_asset(request, asset_name):
    sfile = static_file(asset_name, root=STATIC_PATH + '/ui')

    return _static_asset_response(sfile, asset_name)


@get('/(AllPages|AllPages\.rst)')
def all_pages(request):
    pages = [page.split('.')[0]
             for page in os.listdir(DATA_PATH)
             if fnmatch.fnmatch(page, '*.rst')]

    pages.sort()

    pages_list = ['<li><a href="%s">%s</a></li>' % (page, page) for page in pages]
    
    all_pages = """<h1>All Pages</h1>
    <ul>%s</ul>""" % "".join(pages_list)
    return get_formatted_doc_from_string(all_pages, 'AllPages')


@get('/(?P<page_name>\w+)')
def wiki_page(request, page_name='IndexPage'):
    page = get_formatted_doc(page_name)

    return page


@get('/(?P<page_name>\w+\.rst)')
def wiki_page_src(request, page_name):
    sfile = static_file(page_name, root=DATA_PATH)

    return Response(sfile, content_type='text/plain')


@get('/(?P<page_name>\w+)\.slide')
def wiki_page_slide(request, page_name):
    page = get_formatted_s5_doc(page_name)

    return page


if __name__ == '__main__':
    run_itty()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.