husk/husk_helpers.py

136 lines
4.3 KiB
Python
Raw Normal View History

2022-09-18 21:00:35 +02:00
import os
from collections import deque
2022-09-18 21:00:35 +02:00
#import toml
#base_url = toml.load('settings.toml')["general"]["base_url"]
2022-09-06 23:45:26 +02:00
def sort_branch( lst ):
'''
2022-10-20 23:39:10 +02:00
put directories in front of 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]
2022-09-06 23:45:26 +02:00
def make_tree(path="templates/content"):
'''
creates a dictionary of the directories and files of the doc repository
'''
2022-09-06 23:45:26 +02:00
if '/' in path:
tree = dict(name=path, children=[], type="directory")
else:
pass
try: lst = os.listdir(path)
except OSError:
pass #ignore errors
else:
lst = sort_branch(lst)
2022-09-06 23:45:26 +02:00
for name in lst:
fn = os.path.join(path, name)
if os.path.isdir(fn) and not fn.endswith(".git"):
2022-10-20 23:39:10 +02:00
if os.listdir(fn): # this line is experimental and needs to be tested
tree['children'].append(make_tree(fn))
2022-09-06 23:45:26 +02:00
else:
if fn.endswith('.md') and not name == "README.md":
tree['children'].append(dict(name=fn))
return tree
2022-10-20 23:39:10 +02:00
#def clean_up_tree(tree):
# for key, val in tree.items():
# if isinstance(val, list):
# val = [ x for x in val if len(val) > 0]
# if isinstance(val, dict):
# clean_tree(tree)
# return tree
#def clean_up_tree(tree):
# '''
# create a clean directory structure inside the dict without empty lists
# '''
# import copy
# for key, val in tree.items():
# if isinstance(val, list):
# if len(val) < 1:
# del tree[key]
# else:
# for item in val:
# if isinstance(item, dict):
# clean_up_tree(item)
# return tree
#def clean_up_tree(tree):
# '''
# delete directories without content
# '''
# for key, val in tree.items():
# if isinstance(val, list) and len(val) == 0:
# del tree[key]
# return tree
2022-09-06 23:45:26 +02:00
def cut_path_tree(tree, subdir, file_ending):
'''
pruning of the tree structure from make_tree()
'''
2022-09-09 15:41:05 +02:00
if subdir.endswith('/'):
subdir = subdir[:-1]
2022-09-06 23:45:26 +02:00
for key, val in tree.items():
if isinstance(val, list):
for item in val:
item["name"] = item["name"][len(subdir):]
if item["name"].endswith(file_ending):
item["name"] = item["name"][:-len(file_ending)]
if isinstance(item, dict):
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()
'''
2022-09-06 23:45:26 +02:00
for key, val in tree.items():
if isinstance(val, list):
for item in val:
if item["name"].endswith(filetype):
item["name"] = item["name"][:-len(filetype)]
if isinstance(item, dict):
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
'''
2022-09-06 23:45:26 +02:00
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):
'''
builds the searchable JSON object containing all markdown files
with metadata
'''
2022-09-06 23:45:26 +02:00
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)]]
2022-09-18 21:00:35 +02:00
searchable["index"].append(dict(uri= "/" + item[len(path):-len(file_ending)] + '.html', title=data[0],tags=[],content=''.join(data), description=""))
2022-09-06 23:45:26 +02:00
#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