|
| 1 | +from flask import Flask, request, send_from_directory, render_template, send_file |
| 2 | +app = Flask(__name__, static_url_path='') |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +from io import BytesIO |
| 6 | +import glob |
| 7 | + |
| 8 | +from AnsiGraphics import AnsiGraphics |
| 9 | +from AnsiImage import AnsiImage |
| 10 | +from AnsiPalette import AnsiPalette |
| 11 | + |
| 12 | +ansi_graphics = AnsiGraphics('config/cp866_8x16.fnt', 8, 16) |
| 13 | + |
| 14 | +pal_styles = "" |
| 15 | +for i in range(16): |
| 16 | + col = np.floor(np.array(ansi_graphics.cga_colour(i)) * 255.0) |
| 17 | + pal_entry = ".fg" + str(i) + "{\n" |
| 18 | + pal_entry += " color: rgb(" + str(col[0]) + ", " + str(col[1]) + ", " + str(col[2]) + ");\n" |
| 19 | + pal_entry += "}\n\n" |
| 20 | + pal_entry += ".bg" + str(i) + "{\n" |
| 21 | + pal_entry += " background: rgb(" + str(col[0]) + ", " + str(col[1]) + ", " + str(col[2]) + ");\n" |
| 22 | + pal_entry += "}\n\n" |
| 23 | + pal_styles += pal_entry |
| 24 | + |
| 25 | + |
| 26 | +@app.route('/') |
| 27 | +def file_list(): |
| 28 | + file_list = list(glob.glob('*.ans')) |
| 29 | + list_html = '<h1>Files</h1><div style="text-align: left; font-size: 28px; width: 600px;">' |
| 30 | + for ansi_file in file_list: |
| 31 | + list_html += '--> <a href="/view/' + ansi_file + '">' + ansi_file + '</a><br/>' |
| 32 | + list_html += "</div>" |
| 33 | + return(render_template("default.html", title="files", content=list_html)) |
| 34 | + |
| 35 | +@app.route('/view/<path:path>') |
| 36 | +def render_ansi(path): |
| 37 | + try: |
| 38 | + ansi_image = AnsiImage(ansi_graphics) |
| 39 | + ansi_image.clear_image(80, 24) |
| 40 | + ansi_image.load_ans(path, False) |
| 41 | + width, height = ansi_image.get_size() |
| 42 | + except: |
| 43 | + return(render_template("default.html", title="Loading error, sorry.")) |
| 44 | + |
| 45 | + html_ansi = "" |
| 46 | + for y in range(height): |
| 47 | + for x in range(width): |
| 48 | + char = ansi_image.get_cell(x, y) |
| 49 | + html_ansi += '<span class="fg' + str(char[1]) + ' bg' + str(char[2]) + '">' |
| 50 | + html_ansi += chr(char[0]) |
| 51 | + html_ansi += '</span>' |
| 52 | + html_ansi += "\n" |
| 53 | + return(render_template("default.html", title=path, styles=pal_styles, content=html_ansi, show_dl=True)) |
| 54 | + |
| 55 | +@app.route('/image/<path:path>') |
| 56 | +def render_ansi_png(path): |
| 57 | + transparent = False |
| 58 | + if request.args.get('transparent', False) != False: |
| 59 | + transparent = True |
| 60 | + try: |
| 61 | + ansi_image = AnsiImage(ansi_graphics) |
| 62 | + ansi_image.clear_image(80, 24) |
| 63 | + ansi_image.load_ans(path, False) |
| 64 | + bitmap = ansi_image.to_bitmap(transparent = transparent) |
| 65 | + except: |
| 66 | + return(render_template("default.html", title="Loading error, sorry.")) |
| 67 | + |
| 68 | + img_io = BytesIO() |
| 69 | + bitmap.save(img_io, 'PNG') |
| 70 | + img_io.seek(0) |
| 71 | + |
| 72 | + return send_file(img_io, mimetype='image/png') |
| 73 | + |
| 74 | +@app.route('/webfont/<path:path>') |
| 75 | +def webfont(path): |
| 76 | + return send_from_directory('webfont', path) |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + port = 5000 |
| 80 | + app.run(host='0.0.0.0', port=port) |
| 81 | + |
0 commit comments