From 6861fa637c15eac2d5924c0bd20c1d73f28b4039 Mon Sep 17 00:00:00 2001 From: gurkenhabicht Date: Wed, 24 May 2023 22:22:53 +0200 Subject: [PATCH] optimization --- husk_helpers.py | 147 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 103 insertions(+), 44 deletions(-) diff --git a/husk_helpers.py b/husk_helpers.py index 7226744..40b3a84 100644 --- a/husk_helpers.py +++ b/husk_helpers.py @@ -4,45 +4,78 @@ from collections import deque #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: - if path.endswith(".md"): - files.append(path) - else: - dirs.append(path) +# def sort_branch( lst ): +# ''' +# 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] + + +def sort_branch(lst): + """ + Puts directories in front of files and sorts both alphabetically. + """ + files = [path for path in lst if path.endswith(".md")] + dirs = [path for path in lst if not path.endswith(".md")] files.sort() dirs.sort() - return [*dirs, *files] + 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: +# pass +# try: lst = os.listdir(path) +# except OSError: +# pass #ignore errors +# else: +# lst = sort_branch(lst) +# for name in lst: +# fn = os.path.join(path, name) +# if os.path.isdir(fn) and not fn.endswith(".git"): +# if os.listdir(fn): # this line is experimental and needs to be tested +# tree['children'].append(make_tree(fn)) +# else: +# if fn.endswith('.md'): +# tree['children'].append(dict(name=fn)) +# return tree 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: - pass - try: lst = os.listdir(path) - except OSError: - pass #ignore errors - else: + """ + Creates a dictionary of the directories and files of the doc repository. + """ + tree = {'name': path, 'children': [], 'type': 'directory'} + + try: + lst = os.listdir(path) lst = sort_branch(lst) for name in lst: fn = os.path.join(path, name) - if os.path.isdir(fn) and not fn.endswith(".git"): - if os.listdir(fn): # this line is experimental and needs to be tested - tree['children'].append(make_tree(fn)) - else: - if fn.endswith('.md'): - tree['children'].append(dict(name=fn)) + if os.path.isdir(fn) and not fn.endswith(".git") and os.listdir(fn): + tree['children'].append(make_tree(fn)) + elif fn.endswith('.md'): + tree['children'].append({'name': fn}) + except OSError: + pass # Ignore errors + return tree + def rem_readme(path, tree): ''' This functions puts out the path of the README.md and @@ -101,21 +134,47 @@ def list_files(path): 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 +# ''' +# 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 + + 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) + """ + Builds the searchable JSON object containing + all markdown files with metadata. + """ + searchable = {'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) + with open(item, 'r') as file: + data = file.readlines() + title = data[0].strip('# \n') + uri = '/' + item[len(path):-len(file_ending)] + '.html' + content = ''.join(data) + searchable['index'].append({ + 'uri': uri, + 'title': title, + 'tags': [], + 'content': content, + 'description': '' + }) + elif isinstance(item, dict): + build_index(item, file_ending) return searchable