husk/start_site.py

100 lines
3.0 KiB
Python
Raw Permalink 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
2023-05-26 10:53:34 +02:00
from husk_helpers import make_tree, cut_path_tree, cut_filetype_tree, list_files, build_index # , rem_readme
2022-09-09 15:41:05 +02:00
import toml
2022-09-02 09:05:59 +02:00
app = Flask(__name__)
Markdown(app)
2022-09-09 15:41:05 +02:00
app.config["husk"] = toml.load("settings.toml")
content_path = app.config["husk"]["content"]["path"]
highlight_style = app.config["husk"]["content"]["style"]
2022-10-29 00:15:24 +02:00
STYLESHEET = "stylesheet.css"
STYLESHEET_AUTO_COMPLETE = "auto-complete.css"
2023-05-26 10:53:34 +02:00
project_name = app.config["husk"]["project"]["name"]
project_title = app.config["husk"]["project"]["title"]
2022-09-09 15:41:05 +02:00
2022-10-27 00:52:17 +02:00
app.config["husk"]["style"] = toml.load("style.toml")
colors = app.config["husk"]["style"][highlight_style]
2022-09-09 15:41:05 +02:00
2022-09-02 09:05:59 +02:00
@app.route('/index.json')
def index():
2022-09-09 15:41:05 +02:00
searchable = build_index(content_path, ".md")
2022-09-02 09:05:59 +02:00
response = make_response(searchable)
response.headers["Content-Type"] = "application/json"
2023-05-26 10:53:34 +02:00
# response.headers["Content-Encoding"] = "gzip"
2022-09-02 09:05:59 +02:00
response.cache_control.max_age = 420
return response
2023-05-26 10:53:34 +02:00
2022-09-02 09:05:59 +02:00
@app.route('/', defaults={'path': 'README'})
@app.route('/<path:path>.html')
def content(path="README"):
2023-05-26 10:53:34 +02:00
md_file_path = os.path.join(app.root_path, content_path, f'{path}.md')
with open(md_file_path, "r") as _f:
2022-09-02 09:05:59 +02:00
md_file = _f.read()
2023-05-26 10:53:34 +02:00
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
)
2022-09-18 21:00:35 +02:00
html = md.convert(md_file)
2023-05-26 10:53:34 +02:00
formatter = HtmlFormatter(
style=highlight_style,
full=True,
cssclass="codehilite"
)
2022-09-02 09:05:59 +02:00
css_string = formatter.get_style_defs()
md_css_string = "<style>" + css_string + "</style>"
2022-09-18 21:00:35 +02:00
md_template = md_css_string + html
res = render_template(
2023-05-26 10:53:34 +02:00
"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"
2022-09-02 09:05:59 +02:00
)
2023-05-26 10:53:34 +02:00
)
2022-09-02 09:05:59 +02:00
response = make_response(res)
2023-05-26 10:53:34 +02:00
response.headers["Content-Type"] = "text/html; charset=utf-8"
2022-09-02 09:05:59 +02:00
return response
2023-05-26 10:53:34 +02:00
2022-09-02 09:05:59 +02:00
@app.route('/favicon.ico')
def favicon():
2023-05-26 10:53:34 +02:00
return send_from_directory(
os.path.join(app.root_path, 'static'),
'favicon.ico'
)
2022-09-02 09:05:59 +02:00
with app.test_request_context():
2022-09-09 15:41:05 +02:00
print(f"Front page is {url_for('content', md_file='README')}")