import flask import jinja2 import os import mimetypes import bleach from markdown_it import MarkdownIt from config import * app = flask.Flask(__name__) md = MarkdownIt("commonmark", {"typographer": True}).enable( ["replacements", "smartquotes"] ) def split_url(url): segments = [""] for character in url: segments[-1] += character if character == "/": segments.append("") if not segments[-1]: del segments[-1] paths = [ [segment, "".join(segments[: i + 1])] for i, segment in enumerate(segments) ] paths[0][0] = f"{title}: {paths[0][0]}" return paths def render(path, main, show_print=True, status_code=200): path = jinja2.filters.escape(path) path_segments = split_url(path) path_html = ( "".join( [ f'
{bleach.clean(text)}") def render_md(text, path): text = bleach.clean( md.render(text.decode("utf-8")), tags=[ "a", "abbr", "acronym", "b", "blockquote", "code", "em", "i", "li", "ol", "strong", "ul", "p", "pre", "img", "h1", "h2", "h3", "h4", "h5", "h6", "hr", ], attributes={ "a": ["href", "title"], "abbr": ["title"], "acronym": ["title"], "ol": ["start"], "li": ["value"], }, ) return render(path, text) def render_directory(contents, path): # TODO: strikethrough forbidden files if path.endswith("/"): contents.sort() listing = "".join( [ f'
{path}
Disallowed file type: {file_type}
",
show_print=False,
status_code=403,
)
elif file_type in renderers:
with open(full_path, "rb") as f:
return renderers[file_type](f.read(), path)
else:
if path.endswith("/"):
return flask.redirect(path[:-1])
else:
with open(full_path, "rb") as f:
return flask.Response(
f.read(),
headers={
"Content-Type": mimetypes.guess_type(full_path)[0]
},
)
except FileNotFoundError:
return render(
path,
f"
{path}
", show_print=False, status_code=404, )