husk/start_site.py

59 lines
2.2 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, content_path, f'{path}.md'), "r") as _f:
md_file = _f.read()
md = markdown.Markdown(extensions=['toc',TocExtension(toc_class="HolyGrail-ads", title=""),"fenced_code", "codehilite", "mdx_math"], extension_configs={"mdx_math": {"enable_dollar_delimiter": True}})
html = md.convert(md_file)
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 + html
res = render_template("documentation.html", toc=md.toc, 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')}")