summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pastepy.py37
-rw-r--r--static/paste.css1
-rw-r--r--templates/view.tmpl6
3 files changed, 35 insertions, 9 deletions
diff --git a/pastepy.py b/pastepy.py
index 25d8746..5e0e404 100644
--- a/pastepy.py
+++ b/pastepy.py
@@ -9,6 +9,11 @@ import random
import settings
import templates
+try:
+ import markdown
+ has_markdown = True
+except ImportError:
+ has_markdown = False
from pygments import highlight
from pygments.lexers import get_all_lexers, get_lexer_by_name
from pygments.formatters import HtmlFormatter
@@ -38,6 +43,10 @@ class Paste(object):
del lexers[l]
except KeyError:
pass
+ rendered = []
+ if has_markdown:
+ rendered.append(('Rendered markdown', 'md-render'))
+ self.lexers.append(('Rendered', rendered))
self.lexers.append(('Others' if settings.categories else 'Syntax', sorted(lexers.items(), cmp = lambda a, b: cmp(a[0], b[0]))))
self.formatter = CustomHtmlFormatter(linenos = 'table', lineanchors = 'line', anchorlinenos = True)
@@ -72,12 +81,16 @@ class Paste(object):
}))]
def preview(self, mp):
- try:
- lex = get_lexer_by_name(mp['syntax'].value.decode('utf8'))
- lexername = lex.name
- text = highlight(mp['text'].value.decode('utf8'), lex, self.formatter)
- except:
- return self.message('Could not find the lexer "%s".' % mp['syntax'].value, 'Error')
+ if mp['syntax'].value == 'md-render' and has_markdown:
+ text = markdown.markdown(mp['text'].value)
+ lexername = 'Rendered markdown'
+ else:
+ try:
+ lex = get_lexer_by_name(mp['syntax'].value.decode('utf8'))
+ lexername = lex.name
+ text = highlight(mp['text'].value.decode('utf8'), lex, self.formatter)
+ except:
+ return self.message('Could not find the lexer "%s".' % mp['syntax'].value, 'Error')
self.start_response('200 OK', [('Content-Type', 'text/html; charset=utf-8')])
return [str(templates.view(searchList = {
@@ -89,6 +102,7 @@ class Paste(object):
'syntax': lexername,
'pastetitle': mp['title'].value.decode('utf8') or 'Untitled',
'text': text,
+ 'rendered': (lexername or '').startswith('Rendered '),
}))]
def add_paste(self, mp):
@@ -118,9 +132,13 @@ class Paste(object):
def view(self):
def get_formatted(syntax, text):
- lex = get_lexer_by_name(syntax or 'text')
- lexername = lex.name
- text = highlight(text, lex, self.formatter)
+ if syntax == 'md-render' and has_markdown:
+ text = markdown.markdown(text)
+ lexername = 'Rendered markdown'
+ else:
+ lex = get_lexer_by_name(syntax or 'text')
+ lexername = lex.name
+ text = highlight(text, lex, self.formatter)
return (lexername, text)
hash = self.path[1]
@@ -158,6 +176,7 @@ class Paste(object):
'syntax': cache.syntax_name,
'pastetitle': paste.title or 'Untitled',
'text': cache.text,
+ 'rendered': (cache.syntax_name or '').startswith('Rendered '),
}))]
def raw(self):
diff --git a/static/paste.css b/static/paste.css
index 36df2f2..7bd00c8 100644
--- a/static/paste.css
+++ b/static/paste.css
@@ -11,6 +11,7 @@ div#info { font-size: small; color: #bbb; }
span#nick, span#date, span#syntax-type { color: #888; }
div#alt-links { font-size: small; }
div#alt-links a { color: #44f; }
+div.paste-rendered { background-color: #fff; }
.highlighttable { width: 100%; padding-top: 1em; }
.highlighttable td.code { width: 100%; background-color: #fff; }
.highlighttable td { vertical-align: top; }
diff --git a/templates/view.tmpl b/templates/view.tmpl
index 445a1ad..a23c36e 100644
--- a/templates/view.tmpl
+++ b/templates/view.tmpl
@@ -18,6 +18,12 @@
<a href="/raw/$hash">Download as plain text</a>
#end if
</div>
+#if $rendered
+ <div class="paste-rendered">
+#end if
$text
+#if $rendered
+ </div>
+#end if
</div>
#end def