Namespaces: added feature to show folder structure

Namespaces are an important feature to have a hierarchic structure for the wiki.
A new site was added that can display and navigate the folder structure.
pull/1/head
TiynGER 4 years ago
parent 91d2e940b0
commit 5a9ed40251

@ -18,8 +18,8 @@ I however just want to put my markdown files in a directory and get a working wi
- [x] Full-text search - [x] Full-text search
- [x] Show first few lines of each match (preview) - [x] Show first few lines of each match (preview)
- [ ] Better CSS - [ ] Better CSS
- [ ] Navigation - [x] Navigation
- [ ] More advanced namespaces - [x] More advanced namespaces
- [x] Header - [x] Header
- [ ] Random article - [ ] Random article
- [ ] Search bar in header - [ ] Search bar in header

@ -43,6 +43,13 @@ def entry(fullurl):
content = cont.gen_stand_string(fullurl) content = cont.gen_stand_string(fullurl)
return render_template('entry.html', title=TITLE, style=STYLE, content=content), 200 return render_template('entry.html', title=TITLE, style=STYLE, content=content), 200
@app.route('/namespace/', defaults={'fullurl': ''})
@app.route('/namespace/<path:fullurl>')
def namespace(fullurl=''):
content = cont.gen_arch_string(fullurl)
if content == None:
return render_template('error.html', title=TITLE, errorcode='404', style=STYLE), 404
return render_template('entry.html', title=TITLE, style=STYLE, content=content), 200
if __name__ == '__main__': if __name__ == '__main__':
app.run(host='0.0.0.0') app.run(host='0.0.0.0')

@ -14,10 +14,21 @@ WEBSITE = config.WEBSITE
def gen_stand_string(path_ex): def gen_stand_string(path_ex):
"""
Creates a html-string for a file.
If the file is markdown it will convert it.
This functions ensures upscaling for future formats.
Parameters:
path_ex: path to a file.
Returns:
string: html-formatted string string equivalent to the file
"""
filename = os.path.join(ENTRY_DIR, path_ex) filename = os.path.join(ENTRY_DIR, path_ex)
result = '' result = ''
if path.exists(filename): if path.exists(filename):
title = open(filename,encoding='utf-8').readline().rstrip('\n') title = open(filename,encoding='utf-8').readline().rstrip('\n').lstrip('#').lstrip(' ')
text = open(filename,encoding='utf-8').readlines()[1:] text = open(filename,encoding='utf-8').readlines()[1:]
filename_no_end = filename.split('.', 1)[0] filename_no_end = filename.split('.', 1)[0]
result += '<h1>' + title + '</h1>\n' result += '<h1>' + title + '</h1>\n'
@ -27,6 +38,16 @@ def gen_stand_string(path_ex):
def gen_md_content(path_ex, depth): def gen_md_content(path_ex, depth):
"""
Convert a markdown file to a html string.
Parameters:
path_ex (string): path to the markdown file
depth (int): starting depth for markdown headings
Returns:
string: html-formatted string string equivalent to the markdown file
"""
result = '' result = ''
if path.exists(path_ex): if path.exists(path_ex):
filename = path_ex.split('.', 1) filename = path_ex.split('.', 1)
@ -44,7 +65,57 @@ def gen_md_content(path_ex, depth):
return result return result
def gen_arch_string(path_ex):
"""
Creates and returns an index string of every file in the path_ex.
Parameter:
path_ex: path to the dir to show
Returns:
string: html-formatted archive-string
"""
full_path = os.path.join(ENTRY_DIR, path_ex)
if path.exists(full_path):
name_list = os.listdir(full_path)
full_list = [os.path.join(full_path, i) for i in name_list]
contents = sorted(full_list, key=os.path.getctime)
content_string = ''
last_month = ''
for file in reversed(contents):
filename = pathlib.PurePath(file)
if os.path.isfile(filename):
title = open(filename).readline().rstrip('\n').lstrip('#').lstrip(' ')
entry_or_namespace = 'entry'
elif os.path.isdir(filename):
title = filename.name
entry_or_namespace = 'namespace'
else:
continue
filename = filename.name
if filename[0] != '.' and filename.__contains__('.'):
filename = filename.split('.', 1)[0]
content_string += '<li>'
content_string += title + ' ['
content_string += '<a href="' + '/'+ entry_or_namespace +'/' + \
path_ex.rstrip("/") + '/' + pathlib.PurePath(file).name + '">' + 'standalone' + '</a>'
content_string += '] <br>'
content_string += '</li>\n'
content_string += '</ul>\n'
return content_string
def gen_query_res_string(query_str): def gen_query_res_string(query_str):
"""
Return the results of a query.
Parameters:
query_str (string): term to search
Returns:
string: html-formated search result
"""
src_results = search.search(query_str) src_results = search.search(query_str)
res_string = '' res_string = ''
res_string += '<ul>\n' res_string += '<ul>\n'
@ -53,7 +124,7 @@ def gen_query_res_string(query_str):
path = result['path'] path = result['path']
preview = create_preview(path) preview = create_preview(path)
path = '/entry/' + path.split('/', 2)[2] path = '/entry/' + path.split('/', 2)[2]
res_string += '<li><a href="' + path + '">' + title + '</a><br>' res_string += '<li><a href="' + path + '">' + markdown.markdown(title) + '</a><br>'
res_string += '<div class="description">' + preview + '</div>' res_string += '<div class="description">' + preview + '</div>'
res_string += '</li>' res_string += '</li>'
res_string += '</ul>\n' res_string += '</ul>\n'
@ -61,15 +132,29 @@ def gen_query_res_string(query_str):
def create_preview(path): def create_preview(path):
"""
Create a preview of a given article and return it.
Parameters:
path (string): path to the article
Returns:
string: html-formated preview
"""
file = open(path, 'r',encoding='utf-8') file = open(path, 'r',encoding='utf-8')
first_lines = file.readlines() first_lines = file.readlines()
preview = '' preview = ''
preview_length = 3 preview_length = 3
for i, line in enumerate(first_lines): for i, line in enumerate(first_lines):
if i == 0:
continue
if i > preview_length: if i > preview_length:
break break
if not line.isspace(): if not line.isspace():
preview += line + '<br>' preview += markdown.markdown(line) + '<br>'
else: else:
preview_length += 1 preview_length += 1
preview += '...<br>'
return preview return preview
print(gen_arch_string('system-software'))

@ -14,11 +14,12 @@ ENTRY_DIR = config.ENTRY_DIR
def createSearchableData(root): def createSearchableData(root):
''' """
Schema definition: title(name of file), path(as ID), content(indexed but not stored), textdata (stored text content) Schema definition: title(name of file), path(as ID), content(indexed but not stored), textdata (stored text content)
source: source:
https://appliedmachinelearning.blog/2018/07/31/developing-a-fast-indexing-and-full-text-search-engine-with-whoosh-a-pure-pythhon-library/ https://appliedmachinelearning.blog/2018/07/31/developing-a-fast-indexing-and-full-text-search-engine-with-whoosh-a-pure-pythhon-library/
''' """
schema = Schema(title=TEXT(stored=True), schema = Schema(title=TEXT(stored=True),
path=ID(stored=True), content=TEXT) path=ID(stored=True), content=TEXT)
if not os.path.exists(INDEX_DIR): if not os.path.exists(INDEX_DIR):
@ -37,6 +38,16 @@ def createSearchableData(root):
def search_times(query_str, topN): def search_times(query_str, topN):
"""
Search for a given term and returns a specific amount of results.
Parameters:
query_str (string): term to search for
topN (int): number of results to return
Returns:
string: html-formatted string including the hits of the search
"""
ix = open_dir(INDEX_DIR) ix = open_dir(INDEX_DIR)
results = [] results = []
with ix.searcher(weighting=scoring.BM25F) as s: with ix.searcher(weighting=scoring.BM25F) as s:
@ -49,6 +60,15 @@ def search_times(query_str, topN):
def search(query_str): def search(query_str):
"""
Search for a given term and show the predefined amount of results.
Parameters:
query_str (string): term to search for
Returns:
string: html-formatted string including the hits of the search
"""
return search_times(query_str, DEF_TOPN) return search_times(query_str, DEF_TOPN)

@ -3,7 +3,7 @@
<div class="container"> <div class="container">
<div class="content"> <div class="content">
{% autoescape off %} {% autoescape off %}
<span>{{ content }}</span> {{ content }}
{% endautoescape %} {% endautoescape %}
</div> </div>
</div> </div>

@ -9,7 +9,7 @@
</form> </form>
<div class="content"> <div class="content">
{% autoescape off %} {% autoescape off %}
<span>{{ content }}</span> {{ content }}
{% endautoescape %} {% endautoescape %}
</div> </div>
</div> </div>

@ -14,6 +14,7 @@
<label for="main-menu-check" class="show-menu">&#9776;</label> <label for="main-menu-check" class="show-menu">&#9776;</label>
<div class="main-menu"> <div class="main-menu">
<a href="/">Startpage</a> <a href="/">Startpage</a>
<a href="/namespace">Namespaces</a>
<a href="/search">Search</a> <a href="/search">Search</a>
<label for="main-menu-check" class="hide-menu">X</label> <label for="main-menu-check" class="hide-menu">X</label>
</div> </div>

Loading…
Cancel
Save