impl: Read and write members in DocumentTree

This commit is contained in:
Dendy 2024-02-24 11:18:53 +01:00
parent 3e9cfcd7cf
commit 99911d025a
2 changed files with 28 additions and 11 deletions

View File

@ -33,7 +33,6 @@ app.jinja_env.globals.update(
@app.route('/<path:path>', methods=['POST', 'GET']) @app.route('/<path:path>', methods=['POST', 'GET'])
def index(path): def index(path):
data_path = config.get('data_path') data_path = config.get('data_path')
internal_path = os.path.join(data_path, path + '.md')
document_tree = files.DocumentTree(data_path) document_tree = files.DocumentTree(data_path)
document_tree.root.print_tree() document_tree.root.print_tree()
@ -58,18 +57,16 @@ def index(path):
200, 200,
) )
raw_markdown = request.form.get('text', None) sent_markdown = request.form.get('text', None)
if raw_markdown is not None: if sent_markdown is not None:
with open(internal_path, 'w') as file: document_tree.document_write(path, sent_markdown)
file.write(str(raw_markdown))
# Get the document contents ################################ # Get the document contents ################################
is_edit = 'edit' in request.args and path != '' is_edit = 'edit' in request.args and path != ''
if os.path.exists(internal_path): if True:
with open(internal_path) as file: raw_markdown = document_tree.document_read(path)
raw_markdown = file.read()
elif is_edit: elif is_edit:
raw_markdown = f'# {path}\n\nChangeme' raw_markdown = f'# {path}\n\nChangeme'
else: else:

View File

@ -68,9 +68,29 @@ class DocumentNode():
class DocumentTree(): class DocumentTree():
def __init__(self, path: str): def __init__(self, base: str):
self.path = path self.base = base
self.root = DocumentNode.get_tree(path, '') self.root = DocumentNode.get_tree(base, '')
def get_node(self, path: str) -> DocumentNode | None: def get_node(self, path: str) -> DocumentNode | None:
return self.root.get_child(path) return self.root.get_child(path)
# ----- Document Operations -----
# Split and remerge for Windows
def document_read(self, path: str) -> str | None:
internal_path = os.path.join(self.base, *path.split('/')) + '.md'
if os.path.exists(internal_path):
return open(internal_path).read()
# File doesn't exist
return None
def document_write(self, path: str, text: str) -> None:
internal_path = os.path.join(self.base, *path.split('/')) + '.md'
# TODO: Check if the directory exists before writing
with open(internal_path) as file:
file.write(text)
# -------------------------------