diff options
Diffstat (limited to 'pastepy.py')
-rw-r--r-- | pastepy.py | 33 |
1 files changed, 22 insertions, 11 deletions
@@ -89,22 +89,33 @@ class Paste(object): return [] def view(self): + def get_formatted(syntax, text): + try: + lex = get_lexer_by_name(syntax) + lexername = lex.name + text = highlight(text, lex, self.formatter) + except: + lexername = 'Plain text' + return (lexername, text) + hash = self.path[1] try: session = db.Session() - paste = session.query(db.Paste).filter_by(hash = hash).one() + try: + cache = session.query(db.Cache).filter_by(paste_hash = hash).one() + paste = cache.paste + except: # No cache found, generate it. + paste = session.query(db.Paste).filter_by(hash = hash).one() + lexername, text = get_formatted(paste.syntax, paste.text) + cache = db.Cache(hash, lexername, text) + session.add(cache) + session.commit() + # Workaround for attribute refresh. + paste = cache.paste finally: session.close() - try: - lex = get_lexer_by_name(paste.syntax) - lexername = lex.name - text = highlight(paste.text, lex, self.formatter) - except: - lexername = 'Plain text' - text = paste.text - self.start_response('200 OK', [('Content-Type', 'text/html')]) return [str(templates.view(searchList = { 'title': '%s – View paste – %s' % (settings.pastebin_name, paste.title), @@ -112,9 +123,9 @@ class Paste(object): 'hash': hash, 'date': paste.date.ctime(), 'nick': paste.nick or 'Anonymous', - 'syntax': lexername, + 'syntax': cache.syntax_name, 'pastetitle': paste.title or 'Untitled', - 'text': text, + 'text': cache.text, }))] def raw(self): |