diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2010-01-25 19:36:35 +0100 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2010-01-25 19:36:35 +0100 |
commit | fe7223827b8e9f33e2d5d1e50331491f8018ae7f (patch) | |
tree | 4845ab66bec03eeaf5cf478986792e9148be2b65 | |
parent | 73ced64bb666b163f76afaeb33cae4e22bc96811 (diff) |
Support plain text (no syntax).
-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, }))] |