diff options
-rw-r--r-- | pastepy.py | 27 |
1 files changed, 20 insertions, 7 deletions
@@ -6,6 +6,7 @@ from pygments.formatters import HtmlFormatter 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: @@ -38,8 +39,14 @@ class Paste(object): }))] def preview(self, mp): - lex = get_lexer_by_name(mp['syntax'].value) - text = highlight(mp['text'].value, lex, self.formatter) + try: + lex = get_lexer_by_name(mp['syntax'].value) + lexername = lex.name + text = highlight(mp['text'].value, lex, self.formatter) + except: + lexername = 'Plain text' + text = mp['text'].value + self.start_response('200 OK', [('Content-Type', 'text/html')]) return [str(templates.view(searchList = { 'title': settings.pastebin_name, @@ -47,14 +54,14 @@ class Paste(object): 'hash': None, 'date': datetime.datetime.utcnow().ctime(), 'nick': mp['nick'].value or 'Anoynmous', - 'syntax': lex.name, + 'syntax': lexername, 'pastetitle': mp['title'].value or 'Untitled', 'text': text, }))] def add_paste(self, mp): nick = mp['nick'].value - syntax = mp['syntax'].value + syntax = mp['syntax'].value or None title = mp['title'].value text = mp['text'].value @@ -80,8 +87,14 @@ class Paste(object): finally: session.close() - lex = get_lexer_by_name(paste.syntax) - text = highlight(paste.text, lex, self.formatter) + 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), @@ -89,7 +102,7 @@ class Paste(object): 'hash': hash, 'date': paste.date.ctime(), 'nick': paste.nick or 'Anonymous', - 'syntax': lex.name, + 'syntax': lexername, 'pastetitle': paste.title or 'Untitled', 'text': text, }))] |