61 lines
2.3 KiB
Python
61 lines
2.3 KiB
Python
#!/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
|
|
from husk_helpers import make_tree, cut_path_tree, cut_filetype_tree, list_files, build_index
|
|
import toml
|
|
|
|
app = Flask(__name__)
|
|
Markdown(app)
|
|
|
|
|
|
app.config["husk"] = toml.load("settings.toml")
|
|
content_path = app.config["husk"]["content"]["path"]
|
|
highlight_style = app.config["husk"]["content"]["style"]
|
|
|
|
|
|
@app.route('/index.json')
|
|
def index():
|
|
searchable = build_index(content_path, ".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'{content_path}{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=highlight_style, 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
|
|
res = render_template("documentation.html", md_doc=md_template, tree=
|
|
cut_path_tree(
|
|
make_tree(content_path), content_path, ".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():
|
|
print(f"Front page is {url_for('content', md_file='README')}")
|