added tables extension, sorting of dirs and files in order

This commit is contained in:
gurkenhabicht 2022-09-26 17:15:05 +02:00
parent 82beabb1cc
commit 4029452654
2 changed files with 36 additions and 2 deletions

View File

@ -1,9 +1,29 @@
import os
from collections import deque
#import toml
#base_url = toml.load('settings.toml')["general"]["base_url"]
def sort_branch( lst ):
'''
sorts directories before files and
sorts both alphabetically
'''
files = []
dirs = []
for path in lst:
if path.endswith(".md"):
files.append(path)
else:
dirs.append(path)
files.sort()
dirs.sort()
return [*dirs, *files]
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:
@ -12,7 +32,7 @@ def make_tree(path="templates/content"):
except OSError:
pass #ignore errors
else:
lst.sort()
lst = sort_branch(lst)
for name in lst:
fn = os.path.join(path, name)
if os.path.isdir(fn) and not fn.endswith(".git"):
@ -23,6 +43,9 @@ def make_tree(path="templates/content"):
return tree
def cut_path_tree(tree, subdir, file_ending):
'''
pruning of the tree structure from make_tree()
'''
if subdir.endswith('/'):
subdir = subdir[:-1]
for key, val in tree.items():
@ -36,6 +59,9 @@ def cut_path_tree(tree, subdir, file_ending):
return tree
def cut_filetype_tree(tree, filetype):
'''
removes file type of the links stored in make_tree()
'''
for key, val in tree.items():
if isinstance(val, list):
for item in val:
@ -46,6 +72,10 @@ def cut_filetype_tree(tree, 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:
@ -54,6 +84,10 @@ def list_files(path):
return doc_files
def build_index(path, file_ending):
'''
builds the searchable JSON object containing all markdown files
with metadata
'''
searchable = dict(index=[])
file_list = list_files(path)
for item in file_list:

View File

@ -40,7 +40,7 @@ def index():
def content(path="README"):
with open(os.path.join(app.root_path, content_path, f'{path}.md'), "r") as _f:
md_file = _f.read()
md = markdown.Markdown(extensions=['toc',TocExtension(toc_class="HolyGrail-ads", title=""),"fenced_code", "codehilite", "mdx_math"], extension_configs={"mdx_math": {"enable_dollar_delimiter": True}})
md = markdown.Markdown(extensions=['toc',TocExtension(toc_class="HolyGrail-ads", title=""),"fenced_code", "codehilite", "tables", "mdx_math"], extension_configs={"mdx_math": {"enable_dollar_delimiter": True}})
html = md.convert(md_file)
formatter = HtmlFormatter(style=highlight_style, full=True, cssclass="codehilite")
css_string = formatter.get_style_defs()