summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2010-01-27 22:58:46 +0100
committerJon Bergli Heier <snakebite@jvnv.net>2010-01-27 22:58:46 +0100
commitbc9a28327b5bf22d0cc218a4e852b128fd940a2b (patch)
treea532d6fa1d5c9797d076d3f44bd52223cb951be4
parent99c8cce4e0f06bf5b84c757ce2b5120d176da5d5 (diff)
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.
-rw-r--r--pastepy.py26
-rw-r--r--templates/__init__.py1
-rw-r--r--templates/message.tmpl5
3 files changed, 22 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 &ndash; %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