aboutsummaryrefslogtreecommitdiffstats
path: root/pasteme.py
diff options
context:
space:
mode:
authorVG <vg@devys.org>2014-11-28 17:17:34 +0100
committerVG <vg@devys.org>2014-11-28 17:17:34 +0100
commitea7170f068785f2fbe7e6857a94792e0483f9a6e (patch)
tree9331a129386aeb26c4dc19da9126b8ee76a8e165 /pasteme.py
parent3622c92f6c03ef21ea0fbb9a9e75598d2b2581cf (diff)
downloadpasteme-ea7170f068785f2fbe7e6857a94792e0483f9a6e.tar.gz
pasteme-ea7170f068785f2fbe7e6857a94792e0483f9a6e.tar.bz2
pasteme-ea7170f068785f2fbe7e6857a94792e0483f9a6e.zip
Add automatic coloring of text with pygments
Diffstat (limited to 'pasteme.py')
-rwxr-xr-xpasteme.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/pasteme.py b/pasteme.py
index fce1655..55270ad 100755
--- a/pasteme.py
+++ b/pasteme.py
@@ -1,11 +1,15 @@
#!/usr/bin/python3
import bottle
+import pygments
+import pygments.formatters
+import pygments.lexers
import identigen
import config
from pathlib import Path
pathbase = Path(config.pastedir)
+pygment_formater = pygments.formatters.HtmlFormatter()
if not pathbase.exists():
pathbase.mkdir(mode=0o700, parents=True)
@@ -23,6 +27,10 @@ def route_paste_post():
fd.write(content.encode('utf8'))
bottle.redirect('/' + pid)
+@bottle.route('/static/<path:path>')
+def route_static(path):
+ return bottle.static_file(path, root='static')
+
@bottle.route('/<pid>')
@bottle.route('/<pid>/<pformat>')
def route_paste_get(pid, pformat='colored'):
@@ -35,6 +43,12 @@ def route_paste_get(pid, pformat='colored'):
except IOError:
# use this template for all file based exception
return bottle.template('not_found')
+ if pformat == 'colored':
+ try:
+ lexer = pygments.lexers.guess_lexer(content)
+ content = pygments.highlight(content, lexer, pygment_formater)
+ except pygments.util.ClassNotFound:
+ pass
return bottle.template('paste', content=content)
if __name__ == '__main__':