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 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_BASE_URL"] = "http://localhost/"
#app.config["FREEZER_RELATIVE_URLS"] = False # app.config["FREEZER_RELATIVE_URLS"] = False
_path = app.config["husk"]["content"]["path"] _path = app.config["husk"]["content"]["path"]
freezer = Freezer(app) freezer = Freezer(app)
_files = list_files(_path) _files = list_files(_path)
_files = [file[len(_path):-len(".md")] for file in _files] _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: for file in _files:
print(f"[+] included {file}") print(f"[+] included {file}")
@freezer.register_generator @freezer.register_generator
def content(): def content():
for _file in _files: for _file in _files:
yield { "path": _file.encode('utf-8')} yield {"path": _file.encode('utf-8')}
if __name__ == '__main__': if __name__ == '__main__':
freezer.freeze() freezer.freeze()
print("[*] Great Success!") print("[*] Great Success!")
#freezer.run(debug=True) # freezer.run(debug=True)

View File

@ -1,14 +1,15 @@
import glob
import os import os
from collections import deque # from collections import deque
#import toml # import toml
#base_url = toml.load('settings.toml')["general"]["base_url"] # base_url = toml.load('settings.toml')["general"]["base_url"]
# def sort_branch( lst ): # def sort_branch( lst ):
# ''' # '''
# put directories in front of files and # put directories in front of files and
# sorts both alphabetically # sorts both alphabetically
# ''' # '''
# files = [] # files = []
# dirs = [] # dirs = []
# for path in lst: # for path in lst:
@ -18,7 +19,7 @@ from collections import deque
# dirs.append(path) # dirs.append(path)
# files.sort() # files.sort()
# dirs.sort() # dirs.sort()
# return [*dirs, *files] # return [*dirs, *files]
def sort_branch(lst): def sort_branch(lst):
@ -35,7 +36,7 @@ def sort_branch(lst):
# def make_tree(path="templates/content"): # def make_tree(path="templates/content"):
# ''' # '''
# creates a dictionary of the directories and files of the doc repository # creates a dictionary of the directories and files of the doc repository
# ''' # '''
# if '/' in path: # if '/' in path:
# tree = dict(name=path, children=[], type="directory") # tree = dict(name=path, children=[], type="directory")
# else: # else:
@ -82,17 +83,16 @@ def rem_readme(path, tree):
which will be index.html which will be index.html
path can be dynamically configured and may have a / at the end nor not path can be dynamically configured and may have a / at the end nor not
''' '''
#if path.endswith('/'): index = f"{path.rstrip('/')}/README.md"
# path = path[:-1] for item in tree['children']:
#index = f"{path}/README.md" if isinstance(item, dict):
#for item in tree['children']: for k, v in item.items():
# if isinstance(item, dict): if k == "name":
# for k,v in item.items(): if v == index:
# if k == "name": del tree['children'][-1][k]
# if v == index:
# del tree['children'][-1][k]
return tree return tree
def cut_path_tree(tree, subdir, file_ending): def cut_path_tree(tree, subdir, file_ending):
''' '''
pruning of the tree structure from make_tree() 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) cut_path_tree(item, subdir, file_ending)
return tree return tree
def cut_filetype_tree(tree, filetype): def cut_filetype_tree(tree, filetype):
''' '''
removes file type of the links stored in make_tree() 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) cut_filetype_tree(item, filetype)
return tree 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 # creates a simple, one dimensional list of the doc repository
# with metadata # filters markdown files only
# ''' # '''
# searchable = dict(index=[]) # doc_files = []
# file_list = list_files(path) # for root, dirs, files in os.walk(path):
# for item in file_list: # for file in files:
# if item.endswith(file_ending): # if file.endswith(".md") and not ".git" in root:
# with open(item, 'r') as _f: # doc_files.append(os.path.join(root, file))
# data = _f.readlines() # return doc_files
# 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="")) def list_files(path):
# #searchable[str(uuid.uuid1())] = (dict(href=item[len(path):-len(file_ending)], title=data[0],tags=[],content=''.join(data))) """
# if isinstance(item, dict): Creates a simple, one-dimensional list of the doc repository.
# build_index(item) Filters markdown files only.
# return searchable """
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): def build_index(path, file_ending):

View File

@ -10,7 +10,7 @@ import markdown.extensions.codehilite
import markdown.extensions.toc import markdown.extensions.toc
from markdown.extensions.toc import TocExtension from markdown.extensions.toc import TocExtension
from pygments.formatters import HtmlFormatter 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 import toml
app = Flask(__name__) app = Flask(__name__)
@ -21,7 +21,7 @@ content_path = app.config["husk"]["content"]["path"]
highlight_style = app.config["husk"]["content"]["style"] highlight_style = app.config["husk"]["content"]["style"]
STYLESHEET = "stylesheet.css" STYLESHEET = "stylesheet.css"
STYLESHEET_AUTO_COMPLETE = "auto-complete.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"] project_title = app.config["husk"]["project"]["title"]
app.config["husk"]["style"] = toml.load("style.toml") app.config["husk"]["style"] = toml.load("style.toml")
@ -33,38 +33,67 @@ def index():
searchable = build_index(content_path, ".md") searchable = build_index(content_path, ".md")
response = make_response(searchable) response = make_response(searchable)
response.headers["Content-Type"] = "application/json" response.headers["Content-Type"] = "application/json"
#response.headers["Content-Encoding"] = "gzip" # response.headers["Content-Encoding"] = "gzip"
response.cache_control.max_age = 420 response.cache_control.max_age = 420
return response return response
@app.route('/', defaults={'path': 'README'}) @app.route('/', defaults={'path': 'README'})
@app.route('/<path:path>.html') @app.route('/<path:path>.html')
def content(path="README"): 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_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) 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() css_string = formatter.get_style_defs()
md_css_string = "<style>" + css_string + "</style>" md_css_string = "<style>" + css_string + "</style>"
md_template = md_css_string + html md_template = md_css_string + html
res = render_template( res = render_template(
"documentation.html", "documentation.html",
toc=md.toc, md_doc=md_template, toc=md.toc,
colors = colors, md_doc=md_template,
stylesheet=STYLESHEET, stylesheet_auto_complete=STYLESHEET_AUTO_COMPLETE, colors=colors,
project_name=project_name, project_title=project_title, stylesheet=STYLESHEET,
tree=cut_path_tree( stylesheet_auto_complete=STYLESHEET_AUTO_COMPLETE,
rem_readme(content_path, make_tree(content_path)) project_name=project_name,
, content_path, ".md") 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 = make_response(res)
response.headers["Content-Type"]: "text/html; charset=utf-8" response.headers["Content-Type"] = "text/html; charset=utf-8"
return response return response
@app.route('/favicon.ico') @app.route('/favicon.ico')
def favicon(): 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(): with app.test_request_context():
print(f"Front page is {url_for('content', md_file='README')}") print(f"Front page is {url_for('content', md_file='README')}")

View File

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