summaryrefslogtreecommitdiff
path: root/pastepy.py
diff options
context:
space:
mode:
Diffstat (limited to 'pastepy.py')
-rw-r--r--pastepy.py26
1 files changed, 16 insertions, 10 deletions
diff --git a/pastepy.py b/pastepy.py
index 1aab7df..6485c24 100644
--- a/pastepy.py
+++ b/pastepy.py
@@ -8,7 +8,6 @@ base62_alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY
class Paste(object):
def __init__(self):
lexers = dict([(x[0], x[1][0]) for x in get_all_lexers()])
- lexers['Plain text'] = ''
self.lexers = []
removed = []
for cat, ls in settings.categories:
@@ -23,6 +22,14 @@ class Paste(object):
self.lexers.append(('Others' if settings.categories else 'Syntax', sorted(lexers.items(), cmp = lambda a, b: cmp(a[0], b[0]))))
self.formatter = HtmlFormatter(linenos = 'table', lineanchors = 'line', anchorlinenos = True)
+ def message(self, msg, title = 'Message'):
+ start_response('200 OK', [('Content-Type', 'text/html')])
+ return [str(templates.message(searchList = {
+ 'title': '%s – %s' % (settings.pastebin_name, title),
+ 'header': title,
+ 'text': msg,
+ }))]
+
def paste(self):
c = Cookie.SimpleCookie(self.environ['HTTP_COOKIE'] if 'HTTP_COOKIE' in self.environ else None)
if self.environ['REQUEST_METHOD'] == 'POST':
@@ -50,8 +57,7 @@ class Paste(object):
lexername = lex.name
text = highlight(mp['text'].value, lex, self.formatter)
except:
- lexername = 'Plain text'
- text = mp['text'].value
+ return self.message('Could not find the lexer "%s".' % mp['syntax'].value, 'Error')
self.start_response('200 OK', [('Content-Type', 'text/html')])
return [str(templates.view(searchList = {
@@ -92,12 +98,9 @@ class Paste(object):
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'
+ lex = get_lexer_by_name(syntax)
+ lexername = lex.name
+ text = highlight(text, lex, self.formatter)
return (lexername, text)
hash = self.path[1]
@@ -109,7 +112,10 @@ class Paste(object):
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)
+ try:
+ lexername, text = get_formatted(paste.syntax, paste.text)
+ except:
+ return self.message('Could not find the lexer "%s".' % paste.syntax, 'Error')
cache = db.Cache(hash, lexername, text)
session.add(cache)
session.commit()