from flask import Flask, Response, request, render_template import subprocess import os import re import sys import tempfile import io import requests app = Flask(__name__) title = 'Cobbled paths' fonts_directory = 'static/db/' possible_extensions = [".flf"] etherpad = 'https://pad.constantvzw.org/p/' prefix = 'cobbled-pad-' # VARIABLES 4 CATALOGUE # ------------------------------ collection = { 'stroke': { 'ascii': ' | ' , 'fonts': [] }, 'block': { 'ascii': '|_|' , 'fonts': [] }, 'open': { 'ascii': '/ /' , 'fonts': [] }, 'pattern': { 'ascii': '(((' , 'fonts': [] }, 'fill': { 'ascii': '###' , 'fonts': [] }, '3d': { 'ascii': '_|/' , 'fonts': [] }, # 'directions': { 'ascii': '_/', 'fonts': [] }, # 'frame': { 'ascii': '_/', 'fonts': [] }, # 'code': { 'ascii': '_/', 'fonts': [] }, } databases = { 'default': 'fonts made by the figlet developpers and given with the program, early 1993', 'contributed': 'fonts made by figlet amateurs and submitted to the official figlet ftp, from before 1993 to 2005', 'jave': 'figlet font library of JavE (a free Ascii drawing Editor)', } # VARIABLES 4 REGEX # ------------------------------ # all the character that svgbob understand spec = [".", ",", "’", "'", "`", "+", "*", "o", "O", "V", "\\-", "|", "~", "_", ":", "!", "<", ">", "v", "^", "/", "\\\\", '\\”', '\\"', "(", ")", "=", "#"] r_spec = "".join(spec) r_nspec = "[^" + r_spec + "\€\$\s]" # autofix regex autofix = [ # every arrowshead into lines [re.compile("[<{]"), "("], [re.compile("[>}]"), ")"], [re.compile("[vV]"), "-"], [re.compile("[\\^]"), "-"], [";", ":"], ["7", "/"], [re.compile("[1Tlj\\[\\]]"), "|"], [re.compile("[Y]"), "+"], # every not in the spec --> block [re.compile(r_nspec), "#"], ] # FUNCTIONS # ------------------------------ def most_common(lst): return max(set(lst), key=lst.count) def text2figlet(text, figfont): print('--- FIGLET SUBPROCESS') figlet = subprocess.run(["figlet", text, "-f", figfont, "-w", "160"], stdout = subprocess.PIPE, stderr = subprocess.PIPE, text=True) print(figlet.stdout) if figlet.returncode == 0: answer = (True, figlet.stdout) else: answer = (False, figlet.stderr) return answer def image2ascii(img, chars, width): print('--- JP2A SUBPROCESS') jp2a = subprocess.run([ "jp2a", img, "--background=light", "--width="+width, "--chars="+chars], stdout = subprocess.PIPE, stderr = subprocess.PIPE, text=True) # "--background", "light", # "--width", width, # "--chars", chars print(jp2a) if jp2a.returncode == 0: answer = (True, jp2a.stdout) else: answer = (False, jp2a.stderr) return answer def ascii2svg(ascii, weight='2', scale='1'): if ascii: print('--- SVGBOB SUBPROCESS') svgbob = subprocess.run([ "svgbob_cli", '--stroke-width', weight, '--scale', scale], input = ascii, stdout = subprocess.PIPE, text=True) return svgbob.stdout else: return "ERROR: etherpad request failed" def styleSVG(svg): with open('static/css/svg.css', 'r') as svg_css: css = svg_css.read() svg = svg.replace('