This commit is contained in:
gurkenhabicht 2023-05-26 10:53:34 +02:00
parent 6861fa637c
commit f4757dcd4d
4 changed files with 106 additions and 80 deletions

View File

@ -1,11 +1,12 @@
from flask_frozen import Freezer
from start_site import app, list_files
from start_site import app
from husk_helpers import list_files, cut_filetype_tree
#app.config["FREEZER_BASE_URL"] = "http://localhost/"
#app.config["FREEZER_RELATIVE_URLS"] = False
# app.config["FREEZER_BASE_URL"] = "http://localhost/"
# app.config["FREEZER_RELATIVE_URLS"] = False
_path = app.config["husk"]["content"]["path"]
freezer = Freezer(app)
freezer = Freezer(app)
_files = list_files(_path)
_files = [file[len(_path):-len(".md")] for file in _files]
@ -13,12 +14,14 @@ _files = [file[len(_path):-len(".md")] for file in _files]
for file in _files:
print(f"[+] included {file}")
@freezer.register_generator
def content():
for _file in _files:
yield { "path": _file.encode('utf-8')}
yield {"path": _file.encode('utf-8')}
if __name__ == '__main__':
freezer.freeze()
print("[*] Great Success!")
#freezer.run(debug=True)
# freezer.run(debug=True)

View File

@ -1,8 +1,9 @@
import glob
import os
from collections import deque
#import toml
# from collections import deque
# import toml
#base_url = toml.load('settings.toml')["general"]["base_url"]
# base_url = toml.load('settings.toml')["general"]["base_url"]
# def sort_branch( lst ):
# '''
@ -82,17 +83,16 @@ def rem_readme(path, tree):
which will be index.html
path can be dynamically configured and may have a / at the end nor not
'''
#if path.endswith('/'):
# path = path[:-1]
#index = f"{path}/README.md"
#for item in tree['children']:
# if isinstance(item, dict):
# for k,v in item.items():
# if k == "name":
# if v == index:
# del tree['children'][-1][k]
index = f"{path.rstrip('/')}/README.md"
for item in tree['children']:
if isinstance(item, dict):
for k, v in item.items():
if k == "name":
if v == index:
del tree['children'][-1][k]
return tree
def cut_path_tree(tree, subdir, file_ending):
'''
pruning of the tree structure from make_tree()
@ -109,6 +109,7 @@ def cut_path_tree(tree, subdir, file_ending):
cut_path_tree(item, subdir, file_ending)
return tree
def cut_filetype_tree(tree, filetype):
'''
removes file type of the links stored in make_tree()
@ -122,36 +123,31 @@ def cut_filetype_tree(tree, filetype):
cut_filetype_tree(item, filetype)
return tree
def list_files(path):
'''
creates a simple, one dimensional list of the doc repository
filters markdown files only
'''
doc_files = []
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith(".md") and not ".git" in root:
doc_files.append(os.path.join(root,file))
return doc_files
# def build_index(path, file_ending):
# def list_files(path):
# '''
# builds the searchable JSON object containing all markdown files
# with metadata
# creates a simple, one dimensional list of the doc repository
# filters markdown files only
# '''
# searchable = dict(index=[])
# file_list = list_files(path)
# for item in file_list:
# if item.endswith(file_ending):
# with open(item, 'r') as _f:
# data = _f.readlines()
# data[0] = data[0].strip('# \n')
# #searchable[data[0]] = [''.join(data), item[len(path):-len(file_ending)]]
# searchable["index"].append(dict(uri= "/" + item[len(path):-len(file_ending)] + '.html', title=data[0],tags=[],content=''.join(data), description=""))
# #searchable[str(uuid.uuid1())] = (dict(href=item[len(path):-len(file_ending)], title=data[0],tags=[],content=''.join(data)))
# if isinstance(item, dict):
# build_index(item)
# return searchable
# doc_files = []
# for root, dirs, files in os.walk(path):
# for file in files:
# if file.endswith(".md") and not ".git" in root:
# doc_files.append(os.path.join(root, file))
# return doc_files
def list_files(path):
"""
Creates a simple, one-dimensional list of the doc repository.
Filters markdown files only.
"""
return [
file for file in glob.glob(
os.path.join(path, '**/*.md'), recursive=True
)
if '.git' not in file
]
def build_index(path, file_ending):

View File

@ -10,7 +10,7 @@ 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
from husk_helpers import make_tree, cut_path_tree, cut_filetype_tree, list_files, build_index # , rem_readme
import toml
app = Flask(__name__)
@ -21,7 +21,7 @@ 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_name = app.config["husk"]["project"]["name"]
project_title = app.config["husk"]["project"]["title"]
app.config["husk"]["style"] = toml.load("style.toml")
@ -33,38 +33,67 @@ def index():
searchable = build_index(content_path, ".md")
response = make_response(searchable)
response.headers["Content-Type"] = "application/json"
#response.headers["Content-Encoding"] = "gzip"
# 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_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 = markdown.Markdown(extensions=['toc',TocExtension(toc_class="", title=""),"fenced_code", "codehilite", "tables", "mdx_math"], extension_configs={"mdx_math": {"enable_dollar_delimiter": True}})
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")
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))
, content_path, ".md")
"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"
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')
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')}")

View File

@ -4,7 +4,10 @@
{% block head %}
<meta charset="UTF-8">
<script src="https://cdn.jsdelivr.net/npm/fuse.js/dist/fuse.js"></script>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script
src="https://code.jquery.com/jquery-3.7.0.min.js"
integrity="sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g="
crossorigin="anonymous"></script>
<script type="text/javascript" src="{{ url_for('static', filename='js/auto-complete.js') }}"></script>
<script type="text/javascript" src="{{ url_for('static', filename='js/lunr.min.js')}}"></script>
<script type="text/javascript" src="{{ url_for('static', filename='js/search.js')}}"></script>
@ -94,23 +97,18 @@
</body>
<script>
function linkClick(obj) {
if (obj.open) {
//console.log('open');
if (sessionStorage.getItem(obj.id) && !(sessionStorage.getItem(obj.id) === "open")) {
sessionStorage.removeItem(obj.id);
}
sessionStorage.setItem(obj.id,"open");
//console.log(obj.id);
} else {
//console.log('closed');
if (obj.open) {
if (sessionStorage.getItem(obj.id) && !(sessionStorage.getItem(obj.id) === "open")) {
sessionStorage.removeItem(obj.id);
}
sessionStorage.setItem(obj.id,"open");
} else {
sessionStorage.removeItem(obj.id);
}
}
let _keys = Object.keys(sessionStorage);
if (_keys) {
for ( let i = 0; i < _keys.length; i++ ) {
document.getElementById(_keys[i])['open'] = 'open';
}
for (let i = 0; i < sessionStorage.length; i++) {
const key = sessionStorage.key(i);
document.getElementById(key).open = "open";
}
</script>
<script async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript"></script>