optimization
This commit is contained in:
parent
51208a02e5
commit
6861fa637c
147
husk_helpers.py
147
husk_helpers.py
|
@ -4,45 +4,78 @@ from collections import deque
|
||||||
|
|
||||||
#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:
|
||||||
if path.endswith(".md"):
|
# if path.endswith(".md"):
|
||||||
files.append(path)
|
# files.append(path)
|
||||||
else:
|
# else:
|
||||||
dirs.append(path)
|
# 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()
|
files.sort()
|
||||||
dirs.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"):
|
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:
|
tree = {'name': path, 'children': [], 'type': 'directory'}
|
||||||
tree = dict(name=path, children=[], type="directory")
|
|
||||||
else:
|
try:
|
||||||
pass
|
lst = os.listdir(path)
|
||||||
try: lst = os.listdir(path)
|
|
||||||
except OSError:
|
|
||||||
pass #ignore errors
|
|
||||||
else:
|
|
||||||
lst = sort_branch(lst)
|
lst = sort_branch(lst)
|
||||||
for name in lst:
|
for name in lst:
|
||||||
fn = os.path.join(path, name)
|
fn = os.path.join(path, name)
|
||||||
if os.path.isdir(fn) and not fn.endswith(".git"):
|
if os.path.isdir(fn) and not fn.endswith(".git") and os.listdir(fn):
|
||||||
if os.listdir(fn): # this line is experimental and needs to be tested
|
tree['children'].append(make_tree(fn))
|
||||||
tree['children'].append(make_tree(fn))
|
elif fn.endswith('.md'):
|
||||||
else:
|
tree['children'].append({'name': fn})
|
||||||
if fn.endswith('.md'):
|
except OSError:
|
||||||
tree['children'].append(dict(name=fn))
|
pass # Ignore errors
|
||||||
|
|
||||||
return tree
|
return tree
|
||||||
|
|
||||||
|
|
||||||
def rem_readme(path, tree):
|
def rem_readme(path, tree):
|
||||||
'''
|
'''
|
||||||
This functions puts out the path of the README.md and
|
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))
|
doc_files.append(os.path.join(root,file))
|
||||||
return doc_files
|
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):
|
def build_index(path, file_ending):
|
||||||
'''
|
"""
|
||||||
builds the searchable JSON object containing all markdown files
|
Builds the searchable JSON object containing
|
||||||
with metadata
|
all markdown files with metadata.
|
||||||
'''
|
"""
|
||||||
searchable = dict(index=[])
|
searchable = {'index': []}
|
||||||
file_list = list_files(path)
|
file_list = list_files(path)
|
||||||
for item in file_list:
|
for item in file_list:
|
||||||
if item.endswith(file_ending):
|
if item.endswith(file_ending):
|
||||||
with open(item, 'r') as _f:
|
with open(item, 'r') as file:
|
||||||
data = _f.readlines()
|
data = file.readlines()
|
||||||
data[0] = data[0].strip('# \n')
|
title = data[0].strip('# \n')
|
||||||
#searchable[data[0]] = [''.join(data), item[len(path):-len(file_ending)]]
|
uri = '/' + item[len(path):-len(file_ending)] + '.html'
|
||||||
searchable["index"].append(dict(uri= "/" + item[len(path):-len(file_ending)] + '.html', title=data[0],tags=[],content=''.join(data), description=""))
|
content = ''.join(data)
|
||||||
#searchable[str(uuid.uuid1())] = (dict(href=item[len(path):-len(file_ending)], title=data[0],tags=[],content=''.join(data)))
|
searchable['index'].append({
|
||||||
if isinstance(item, dict):
|
'uri': uri,
|
||||||
build_index(item)
|
'title': title,
|
||||||
|
'tags': [],
|
||||||
|
'content': content,
|
||||||
|
'description': ''
|
||||||
|
})
|
||||||
|
elif isinstance(item, dict):
|
||||||
|
build_index(item, file_ending)
|
||||||
return searchable
|
return searchable
|
||||||
|
|
Loading…
Reference in New Issue