diff options
-rw-r--r-- | pastepy.py | 26 | ||||
-rw-r--r-- | templates/__init__.py | 1 | ||||
-rw-r--r-- | templates/message.tmpl | 5 |
3 files changed, 22 insertions, 10 deletions
@@ -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() diff --git a/templates/__init__.py b/templates/__init__.py index 8353176..defb657 100644 --- a/templates/__init__.py +++ b/templates/__init__.py @@ -1,2 +1,3 @@ from paste import paste from view import view +from message import message diff --git a/templates/message.tmpl b/templates/message.tmpl new file mode 100644 index 0000000..9f69ebc --- /dev/null +++ b/templates/message.tmpl @@ -0,0 +1,5 @@ +#extends templates.base +#def content + <p><a href="/">New paste</a></p> + $text +#end def |