lines.py - fiche - A pastebin adjusted for gopher use
(HTM) git clone git://vernunftzentrum.de/fiche.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) LICENSE
---
lines.py (1376B)
---
1 from flask import Flask, abort, redirect
2 app = Flask(__name__)
3
4 import argparse, os, pygments
5 from pygments import highlight
6 from pygments.lexers import guess_lexer
7 from pygments.formatters import HtmlFormatter
8
9 parser = argparse.ArgumentParser()
10 parser.add_argument("root_dir", help="Path to directory with pastes")
11 args = parser.parse_args()
12
13
14 @app.route('/')
15 def main():
16 return redirect("http://termbin.com", code=302)
17
18
19 @app.route('/<slug>')
20 def beautify(slug):
21 # Return 404 in case of urls longer than 64 chars
22 if len(slug) > 64:
23 abort(404)
24
25 # Create path for the target dir
26 target_dir = os.path.join(args.root_dir, slug)
27
28 # Block directory traversal attempts
29 if not target_dir.startswith(args.root_dir):
30 abort(404)
31
32 # Check if directory with requested slug exists
33 if os.path.isdir(target_dir):
34 target_file = os.path.join(target_dir, "index.txt")
35
36 # File index.txt found inside that dir
37 with open(target_file) as f:
38 code = f.read()
39 # Identify language
40 lexer = guess_lexer(code)
41 # Create formatter with line numbers
42 formatter = HtmlFormatter(linenos=True, full=True)
43 # Return parsed code
44 return highlight(code, lexer, formatter)
45
46 # Not found
47 abort(404)
48
49
50 if __name__ == '__main__':
51 app.run()