def showfile(root, fpath): ext = '.' + fpath.rsplit('.', 1)[1] if '.' in fpath else '' # attempt to load corresponding .yml file if .html if ext == '.html': yml, d = fpath.replace('.html', '.yml'), {} if os.path.isfile(yml): buf = unicode(open(yml).read(), encoding='utf-8') d = yaml.load(buf) return render_ext(fpath, root, **d) # attempt to render requested template in .yml file if ext == '.yml': d = yaml.load(unicode(open(fpath).read(), encoding='utf-8')) if 'template' in d: return render_ext(d['template'], root, **d) # check if file contains template tags (for templating non-html files) buf = open(fpath).read(1024) if '{%' in buf or '{{' in buf: return render_ext(fpath, root) # render file based on file ext if ext in EXT_MAP: title, body = EXT_MAP[ext](fpath) h1 = title if title else '' title = title if title else fpath return render_sys('page.html', root, title=title, body=body, h1=h1) try: lexer = pygments.lexers.get_lexer_for_filename(fpath) except: lexer = None if lexer and ext != '.txt': body = highlight(lexer, fpath) return render_sys('page.html', root, title=fpath, body=body) else: # just try for text as <pre> block buf = unicode(open(fpath).read(), encoding='utf-8') body = u'<pre>%s</pre>' % cgi.escape(buf) return render_sys('page.html', root, title=fpath, body=body)
def test_tmpl(): body = render_sys('files/tmpl.html', 'root', title='Title', year=2014) open('files/last.html', 'w').write(body) assert body.rstrip() == open('files/rendered.html').read().rstrip()
def error(root, err): body = u'<pre>%s</pre>' % cgi.escape(err) return render_sys('page.html', root, title='Error', body=body)
def dirlist(root, fpath): fpath = os.path.normpath(fpath) return render_sys('dirlist.html', root, title=fpath, rows=rows(root, fpath))