from markdown import markdown from smartypants import smartyPants as smartypants ###### Decorators ###### def template(t): """ Wraps the decorator function's output with a template engine like Markdown or SmartyPants """ def wrap(func): def d(*a, **k): return t(func(*a, **k)) return d return wrap class Nervous(object): def __init__(self): self.data = {} def add(self, id): """ Does not modify the function but does place it in our `data` dict for happiness' sake """ def tally(func): if id in self.data: self.data[id] += [func] else: self.data[id] = [func] return func return tally def fire(self, id, *args): """ Iterate over the dict, applying each function and storing the results, passing a cumulative results `r` string each time """ r = ('',) for f in self.data[id]: r = f(*(args+r)) return r nervous = Nervous() ###### Logic ###### chdire = '' def index(req): import os global chdire chdire = os.path.dirname(req.filename) art = nervous.fire('find_art', req.args[4:]) template = nervous.fire('find_template') return template % { 'site': 'http://localhost:8080/baker', 'content': art, } @nervous.add('find_art') @template(markdown) @template(smartypants) def folder_search(file, result): if not file: file = 'HomePage' file = '/art/' + file try: return open(chdire + file, 'r').read() except IOError: return "Cannot find %s" % file @nervous.add('find_template') def header_footer(result): file = chdire + '/templates/header_footer.html' return open(file).read()