Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
69 lines (56 sloc) 1.99 KB
import os
import mimetypes
mimetypes.add_type('text/html', '.stx')
mimetypes.add_type('application/pdf', '.pdf')
from zope.structuredtext import stx2html
from pyramid.response import Response
from pyramid.httpexceptions import HTTPFound
from pyramid.view import render_view_to_response
from pyramid.view import view_config
from virginia.models import File
from virginia.models import Directory
# default views: router will call these
def file_view(context, request):
dirname, filename = os.path.split(context.path)
name, ext = os.path.splitext(filename)
result = render_view_to_response(context, request, ext)
return result
def directory_view(context, request):
path_info = request.environ['PATH_INFO']
if not path_info.endswith('/'):
response = HTTPFound(location=path_info + '/')
return response
defaults = ('index.html', 'index.stx', '')
for name in defaults:
index = context[name]
except KeyError:
return file_view(index, request)
response = Response('No default view for %s' % context.path)
response.content_type = 'text/plain'
return response
# custom views: FileView will call these
@view_config(context=File, name='.stx')
def structured_text_view(context, request):
""" Filesystem-based STX view
result = stx2html(context.source)
response = Response(result)
response.content_type = 'text/html'
return response
@view_config(context=File, name='.html')
@view_config(context=File, name='.pdf')
@view_config(context=File, name='.txt')
@view_config(context=File, name='.jpg')
def raw_view(context, request):
""" Just return the source raw.
response = Response(context.source)
dirname, filename = os.path.split(context.path)
name, ext = os.path.splitext(filename)
mt, encoding = mimetypes.guess_type(filename)
response.content_type = mt or 'text/plain'
return response
Something went wrong with that request. Please try again.