From bc9a28327b5bf22d0cc218a4e852b128fd940a2b Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Wed, 27 Jan 2010 22:58:46 +0100 Subject: Replaced "Plain text" with pygments' "Text only". This will correctly add the layout formatting including line numbers. Added Paste.message and templates.message for error messages. --- pastepy.py | 26 ++++++++++++++++---------- templates/__init__.py | 1 + templates/message.tmpl | 5 +++++ 3 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 templates/message.tmpl 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() 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 +

New paste

+ $text +#end def -- cgit v1.2.3