husk/start_site.py

56 lines
2.1 KiB
Python
Raw Normal View History

2022-09-02 09:05:59 +02:00
#!/usr/bin/env python
import os
import uuid
from flask import Flask, url_for, render_template, send_from_directory, jsonify, make_response
from flaskext.markdown import Markdown
import markdown
import markdown.extensions.fenced_code
import markdown.extensions.codehilite
import markdown.extensions.toc
from markdown.extensions.toc import TocExtension
from pygments.formatters import HtmlFormatter
2022-09-06 23:45:26 +02:00
from helpers import make_tree, cut_path_tree, cut_filetype_tree, list_files, build_index
2022-09-02 09:05:59 +02:00
app = Flask(__name__)
Markdown(app)
@app.route('/index.json')
def index():
searchable = build_index("templates/content", ".md")
response = make_response(searchable)
response.headers["Content-Type"] = "application/json"
#response.headers["Content-Encoding"] = "gzip"
response.cache_control.max_age = 420
return response
@app.route('/', defaults={'path': 'README'})
@app.route('/<path:path>.html')
def content(path="README"):
with open(os.path.join(app.root_path, f'templates/content/{path}.md'), "r") as _f:
md_file = _f.read()
if not md_file.startswith("[TOC]"):
md_file = "[TOC]\n" + md_file
md_template_string = markdown.markdown(md_file, extensions=["fenced_code", "codehilite", "toc", TocExtension(toc_class="column column-3", title=""), "mdx_math"],
extension_configs={"mdx_math": {"enable_dollar_delimiter": True}})
formatter = HtmlFormatter(style="material", full=True, cssclass="codehilite")
css_string = formatter.get_style_defs()
md_css_string = "<style>" + css_string + "</style>"
md_template = md_css_string + md_template_string
#return md_templateresponse = make_response(searchable)
res = render_template("documentation.html", md_doc=md_template, tree=
cut_path_tree(
make_tree("templates/content"), "templates/content", ".md")
)
response = make_response(res)
response.headers["Content-Type"]: "text/html; charset=utf-8"
return response
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico')
with app.test_request_context():
2022-09-06 23:45:26 +02:00
print(url_for("content", md_file="README"))