From ea7170f068785f2fbe7e6857a94792e0483f9a6e Mon Sep 17 00:00:00 2001 From: VG Date: Fri, 28 Nov 2014 17:17:34 +0100 Subject: Add automatic coloring of text with pygments --- pasteme.py | 14 ++++++++++++ readme.rst | 15 +++++++++++++ static/pygments.css | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ views/paste.tpl | 3 ++- 4 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 static/pygments.css 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/') +def route_static(path): + return bottle.static_file(path, root='static') + @bottle.route('/') @bottle.route('//') 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__': diff --git a/readme.rst b/readme.rst index 9108a99..9ad7be8 100644 --- a/readme.rst +++ b/readme.rst @@ -17,9 +17,23 @@ It features the following: It can be used directly (more for development) or from a wsgi compatible web server. +Dependencies +============ + +- python3 (3.4 at least) +- bottle (included as submodule in this repository) +- pygments (packaged on a lot of distributions) + Installation ============ +Dependencies: + +.. code: shell + + # under deb based systems (debian/ubuntu for example) + sudo apt-get install python3 python3-pygments + Clone this repository recursively: .. code: shell @@ -28,3 +42,4 @@ Clone this repository recursively: cd pasteme # to run it with dev mode, just run it ./pastme.py + diff --git a/static/pygments.css b/static/pygments.css new file mode 100644 index 0000000..251b50d --- /dev/null +++ b/static/pygments.css @@ -0,0 +1,62 @@ +.hll { background-color: #ffffcc } +.c { color: #408080; font-style: italic } /* Comment */ +.err { border: 1px solid #FF0000 } /* Error */ +.k { color: #008000; font-weight: bold } /* Keyword */ +.o { color: #666666 } /* Operator */ +.cm { color: #408080; font-style: italic } /* Comment.Multiline */ +.cp { color: #BC7A00 } /* Comment.Preproc */ +.c1 { color: #408080; font-style: italic } /* Comment.Single */ +.cs { color: #408080; font-style: italic } /* Comment.Special */ +.gd { color: #A00000 } /* Generic.Deleted */ +.ge { font-style: italic } /* Generic.Emph */ +.gr { color: #FF0000 } /* Generic.Error */ +.gh { color: #000080; font-weight: bold } /* Generic.Heading */ +.gi { color: #00A000 } /* Generic.Inserted */ +.go { color: #888888 } /* Generic.Output */ +.gp { color: #000080; font-weight: bold } /* Generic.Prompt */ +.gs { font-weight: bold } /* Generic.Strong */ +.gu { color: #800080; font-weight: bold } /* Generic.Subheading */ +.gt { color: #0044DD } /* Generic.Traceback */ +.kc { color: #008000; font-weight: bold } /* Keyword.Constant */ +.kd { color: #008000; font-weight: bold } /* Keyword.Declaration */ +.kn { color: #008000; font-weight: bold } /* Keyword.Namespace */ +.kp { color: #008000 } /* Keyword.Pseudo */ +.kr { color: #008000; font-weight: bold } /* Keyword.Reserved */ +.kt { color: #B00040 } /* Keyword.Type */ +.m { color: #666666 } /* Literal.Number */ +.s { color: #BA2121 } /* Literal.String */ +.na { color: #7D9029 } /* Name.Attribute */ +.nb { color: #008000 } /* Name.Builtin */ +.nc { color: #0000FF; font-weight: bold } /* Name.Class */ +.no { color: #880000 } /* Name.Constant */ +.nd { color: #AA22FF } /* Name.Decorator */ +.ni { color: #999999; font-weight: bold } /* Name.Entity */ +.ne { color: #D2413A; font-weight: bold } /* Name.Exception */ +.nf { color: #0000FF } /* Name.Function */ +.nl { color: #A0A000 } /* Name.Label */ +.nn { color: #0000FF; font-weight: bold } /* Name.Namespace */ +.nt { color: #008000; font-weight: bold } /* Name.Tag */ +.nv { color: #19177C } /* Name.Variable */ +.ow { color: #AA22FF; font-weight: bold } /* Operator.Word */ +.w { color: #bbbbbb } /* Text.Whitespace */ +.mb { color: #666666 } /* Literal.Number.Bin */ +.mf { color: #666666 } /* Literal.Number.Float */ +.mh { color: #666666 } /* Literal.Number.Hex */ +.mi { color: #666666 } /* Literal.Number.Integer */ +.mo { color: #666666 } /* Literal.Number.Oct */ +.sb { color: #BA2121 } /* Literal.String.Backtick */ +.sc { color: #BA2121 } /* Literal.String.Char */ +.sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */ +.s2 { color: #BA2121 } /* Literal.String.Double */ +.se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */ +.sh { color: #BA2121 } /* Literal.String.Heredoc */ +.si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */ +.sx { color: #008000 } /* Literal.String.Other */ +.sr { color: #BB6688 } /* Literal.String.Regex */ +.s1 { color: #BA2121 } /* Literal.String.Single */ +.ss { color: #19177C } /* Literal.String.Symbol */ +.bp { color: #008000 } /* Name.Builtin.Pseudo */ +.vc { color: #19177C } /* Name.Variable.Class */ +.vg { color: #19177C } /* Name.Variable.Global */ +.vi { color: #19177C } /* Name.Variable.Instance */ +.il { color: #666666 } /* Literal.Number.Integer.Long */ diff --git a/views/paste.tpl b/views/paste.tpl index 99fad8c..814cccf 100644 --- a/views/paste.tpl +++ b/views/paste.tpl @@ -1,8 +1,9 @@ Paste snippets + -{{content}} +{{!content}} -- cgit v1.2.3