From 801231514cc99c0cfdb07e34797fcc49ea6f63d8 Mon Sep 17 00:00:00 2001 From: solusipse Date: Mon, 9 Oct 2017 19:58:21 +0200 Subject: [PATCH] Created very simple script for serving pastes with numbering (#51) --- extras/lines/__init__.py | 0 extras/lines/lines.py | 47 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 extras/lines/__init__.py create mode 100644 extras/lines/lines.py diff --git a/extras/lines/__init__.py b/extras/lines/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/extras/lines/lines.py b/extras/lines/lines.py new file mode 100644 index 0000000..5d46b6a --- /dev/null +++ b/extras/lines/lines.py @@ -0,0 +1,47 @@ +from flask import Flask, abort, redirect +app = Flask(__name__) + +import argparse, os, pygments +from pygments import highlight +from pygments.lexers import guess_lexer +from pygments.formatters import HtmlFormatter + +parser = argparse.ArgumentParser() +parser.add_argument("root_dir", help="Path to directory with pastes") +args = parser.parse_args() + + +@app.route('/') +def main(): + return redirect("http://termbin.com", code=302) + + +@app.route('/') +def beautify(slug): + # Return 404 in case of urls longer than 64 chars + if (len(slug) > 64): + abort(404) + + # Create path for the target dir + target_dir = os.path.join(args.root_dir, slug) + + # Check if directory with requested slug exists + if (os.path.isdir(target_dir)): + target_file = os.path.join(target_dir, "index.txt") + + # File index.txt found inside that dir + with open(target_file) as f: + code = f.read() + # Identify language + lexer = guess_lexer(code) + # Create formatter with line numbers + formatter = HtmlFormatter(linenos=True, full=True) + # Return parsed code + return highlight(code, lexer, formatter) + + # Not found + abort(404) + + +if __name__ == '__main__': + app.run()