summaryrefslogtreecommitdiff
path: root/pastepy.py
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2010-01-26 19:45:27 +0100
committerJon Bergli Heier <snakebite@jvnv.net>2010-01-26 19:45:27 +0100
commitd6337a93a34836fd479839697d9d82c045b01da2 (patch)
tree37db42ee1ae8ad85d2dd52f30d1842ba31a9e196 /pastepy.py
parentdc94a7a45a7fcd6722f40ac0210b1c37c7756bc5 (diff)
Cache formatted text.
Diffstat (limited to 'pastepy.py')
-rw-r--r--pastepy.py33
1 files changed, 22 insertions, 11 deletions
diff --git a/pastepy.py b/pastepy.py
index 3229eb4..570aef6 100644
--- a/pastepy.py
+++ b/pastepy.py
@@ -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 &ndash; View paste &ndash; %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):