From f4757dcd4d5d39ecb9bff1bf2547e54ffcc6beba Mon Sep 17 00:00:00 2001 From: gurkenhabicht Date: Fri, 26 May 2023 10:53:34 +0200 Subject: [PATCH] clean up --- freezer.py | 15 +++++--- husk_helpers.py | 82 ++++++++++++++++++++--------------------- start_site.py | 61 ++++++++++++++++++++++-------- templates/template.html | 28 +++++++------- 4 files changed, 106 insertions(+), 80 deletions(-) diff --git a/freezer.py b/freezer.py index e09d2fc..ec203b8 100644 --- a/freezer.py +++ b/freezer.py @@ -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) diff --git a/husk_helpers.py b/husk_helpers.py index 40b3a84..23e2984 100644 --- a/husk_helpers.py +++ b/husk_helpers.py @@ -1,14 +1,15 @@ +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 ): # ''' # put directories in front of files and # sorts both alphabetically -# ''' +# ''' # files = [] # dirs = [] # for path in lst: @@ -18,7 +19,7 @@ from collections import deque # dirs.append(path) # files.sort() # dirs.sort() -# return [*dirs, *files] +# return [*dirs, *files] def sort_branch(lst): @@ -35,7 +36,7 @@ def sort_branch(lst): # def make_tree(path="templates/content"): # ''' # creates a dictionary of the directories and files of the doc repository -# ''' +# ''' # if '/' in path: # tree = dict(name=path, children=[], type="directory") # else: @@ -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 -# ''' -# 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 +# 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 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): diff --git a/start_site.py b/start_site.py index 49ad8ee..8a24ed7 100644 --- a/start_site.py +++ b/start_site.py @@ -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('/.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 = "" 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')}") diff --git a/templates/template.html b/templates/template.html index 065a758..304cefc 100644 --- a/templates/template.html +++ b/templates/template.html @@ -4,7 +4,10 @@ {% block head %} - + @@ -94,23 +97,18 @@