diff options
Diffstat (limited to 'main.py')
| -rw-r--r-- | main.py | 75 |
1 files changed, 54 insertions, 21 deletions
@@ -11,6 +11,8 @@ from bs4 import BeautifulSoup import datetime from dotenv import load_dotenv from os import getenv +import markdown_renderer as md # markdown_renderer.py +import requests print("Hello, world!") load_dotenv() @@ -60,8 +62,15 @@ def folder(path: str): return fastapi.responses.Response(status_code=403, content="Access denied.") if target_path.is_file(): -# if target_path.suffix == ".md": -# return fastapi.responses.Response(content=markdown(target_path), media_type="text/html", status_code=200) + if target_path.suffix == ".md": + return fastapi.responses.Response(content=markdown(absolute_path=target_path, url_path=path), media_type="text/html", status_code=200) + if target_path.suffix == ".nextcloudmd": + content = "" + with target_path.open(mode="r") as f: + content = f.read() + r = requests.get(content.rstrip(), stream=True) + + return fastapi.responses.Response(content=markdown(mdstr=r.text, url_path=path), media_type="text/html", status_code=200) return fastapi.responses.FileResponse(target_path) elif not target_path.is_dir(): return fastapi.responses.Response(status_code=404) @@ -82,33 +91,36 @@ def folder(path: str): for f in files: add_item_html(f, path, False, soup) - local_path_list = path.split("/") + render_breadcrumb(soup, path) - home_btn = soup.new_tag("a") - home_btn["class"] = "link" - home_btn["href"] = "/" - home_btn.string = "🏠 altaf-files" - soup.select_one("#breadcrumb-container").append(home_btn) + soup.select_one("#file-content-section").decompose() - for i in range(len(local_path_list)): - new_sep = soup.new_tag("span") - new_sep.string = "/" - soup.select_one("#breadcrumb-container").append(new_sep) + return fastapi.responses.Response(content=str(soup), media_type="text/html", status_code=200) - new_btn = soup.new_tag("a") - new_btn["class"] = "link" - new_btn["href"] = "/" + "/".join(local_path_list[0:i + 1]) - new_btn.string = local_path_list[i] - soup.select_one("#breadcrumb-container").append(new_btn) - return fastapi.responses.Response(content=str(soup), media_type="text/html", status_code=200) +def markdown(mdstr: str = None, absolute_path: Path = None, url_path: str = None): + content = "" + if absolute_path: + with absolute_path.open(mode="r") as f: + content = f.read() + else: + content = mdstr + + _, mdhtml = md.md_to_html(content) + + soup = BeautifulSoup(HTML_FOLDER_TEMPLATE, "html.parser") + + soup.select_one("#list-container-section").decompose() + md_soup = BeautifulSoup(mdhtml, "html.parser") -def markdown(absolute_path: str): - pass + soup.select_one("#file-content").append(md_soup) + render_breadcrumb(soup, url_path) -def add_item_html(child_path: str, current_path: str, is_directory: bool, soup: BeautifulSoup): + return str(soup) + +def add_item_html(child_path: str, current_path: str, is_directory: bool, soup: BeautifulSoup) -> None: href_path = "" if current_path == "": @@ -152,6 +164,27 @@ def add_item_html(child_path: str, current_path: str, is_directory: bool, soup: soup.select_one("#list-container").append(new_btn) +def render_breadcrumb(soup: BeautifulSoup, path: str) -> None: + local_path_list = path.split("/") + + home_btn = soup.new_tag("a") + home_btn["class"] = "link" + home_btn["href"] = "/" + home_btn.string = "🏠 altaf-files" + soup.select_one("#breadcrumb-container").append(home_btn) + + for i in range(len(local_path_list)): + new_sep = soup.new_tag("span") + new_sep.string = "/" + soup.select_one("#breadcrumb-container").append(new_sep) + + new_btn = soup.new_tag("a") + new_btn["class"] = "link" + new_btn["href"] = "/" + "/".join(local_path_list[0:i + 1]) + new_btn.string = local_path_list[i] + soup.select_one("#breadcrumb-container").append(new_btn) + + def format_size(byte_size): for unit in ['B', 'KiB', 'MiB', 'GiB', 'TiB']: if byte_size < 1024.0: |
