100 lines
3.0 KiB
Python
100 lines
3.0 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 # , rem_readme
|
|
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"]
|
|
STYLESHEET = "stylesheet.css"
|
|
STYLESHEET_AUTO_COMPLETE = "auto-complete.css"
|
|
project_name = app.config["husk"]["project"]["name"]
|
|
project_title = app.config["husk"]["project"]["title"]
|
|
|
|
app.config["husk"]["style"] = toml.load("style.toml")
|
|
colors = app.config["husk"]["style"][highlight_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"):
|
|
md_file_path = os.path.join(app.root_path, content_path, f'{path}.md')
|
|
with open(md_file_path, "r") as _f:
|
|
md_file = _f.read()
|
|
md_extensions = [
|
|
'toc',
|
|
TocExtension(toc_class="", title=""),
|
|
"fenced_code",
|
|
"codehilite",
|
|
"tables",
|
|
"mdx_math"
|
|
]
|
|
md_configs = {"mdx_math": {"enable_dollar_delimiter": True}}
|
|
md = markdown.Markdown(
|
|
extensions=md_extensions,
|
|
extension_configs=md_configs
|
|
)
|
|
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,
|
|
colors=colors,
|
|
stylesheet=STYLESHEET,
|
|
stylesheet_auto_complete=STYLESHEET_AUTO_COMPLETE,
|
|
project_name=project_name,
|
|
project_title=project_title,
|
|
tree=cut_path_tree(
|
|
#rem_readme(content_path, make_tree(content_path)),
|
|
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')}")
|