import logging from os import getcwd, listdir from os.path import exists, splitext, isfile, join, relpath, isdir import hashlib from .md2html import compile_html log = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) cwd = getcwd() def is_markdown(filepath): _, ext = splitext(filepath) return ext == ".md" cache = dict() def file_hash(filepath, bufsize=4096): if bufsize <= 0: raise ValueError("Buffer size must be greater than 0") md5 = hashlib.md5() with open(filepath, 'rb') as f: while True: buf = f.read(bufsize) if len(buf) == 0: break md5.update(buf) return md5.digest() def application(env, start_response): path = join(cwd, relpath(env['PATH_INFO'], '/')) if exists(path): if isfile(path) and is_markdown(path): if path not in cache: digest = file_hash(path).hex() cache[path] = digest else: digest = cache[path] def parse_etag(etag): if etag is None: return start = etag.find('"') if start < 0: return end = etag.find('"', start + 1) return etag[start + 1: end] etag = parse_etag(env.get('HTTP_IF_NONE_MATCH')) if etag and etag == digest: start_response('304 Not Modified', [ ('Etag', '"%s"' % (digest)), ('Cache-Control', 'no-cache, must-revalidate, max-age=86400'), ]) return [] else: body = compile_html(path, ['extra', 'smarty', 'tables']).encode() start_response('200 OK', [('Content-Type', 'text/html'), ('Etag', '"%s"' % (digest)), ('Cache-Control', 'no-cache, must-revalidate, max-age=86400'), ]) return [body] elif isdir(path): body = directory_listing(env['PATH_INFO'], path).encode() start_response('200 OK', [ ('Content-Type', 'text/html'), ]) return [body] start_response('404 NOT_FOUND', []) return [] def directory_listing(path_info, path): title = "Directory listing for %s" % path_info result = "" result += "" result += "" + title + "" result += "

" + title + "


" result += "