diff options
| -rw-r--r-- | index.html | 58 | ||||
| -rw-r--r-- | main.py | 75 | ||||
| -rw-r--r-- | markdown_renderer.py | 71 | ||||
| -rw-r--r-- | style.css | 49 |
4 files changed, 230 insertions, 23 deletions
@@ -7,6 +7,15 @@ <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="/src/style.css"> <title>altaf-files</title> + + <!-- KaTeX --> + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.17.0/dist/katex.min.css" integrity="sha384-vlBdW0r3AcZO/HboRPznQNowvexd3fY8qHOWkBi5q7KGgqJ+F48+DceybYmrVbmB" crossorigin="anonymous"> + <!-- The loading of KaTeX is deferred to speed up page rendering --> + <script defer src="https://cdn.jsdelivr.net/npm/katex@0.17.0/dist/katex.min.js" integrity="sha384-AtrdNsnxl/75rvBneBVH7DtOvCxSVahR2zWqle1coBKd8DEmLoviqNeJSx64gNAs" crossorigin="anonymous"></script> + <!-- To automatically render math in text elements, include the auto-render extension: --> + <script defer src="https://cdn.jsdelivr.net/npm/katex@0.17.0/dist/contrib/auto-render.min.js" integrity="sha384-bjyGPfbij8/NDKJhSGZNP/khQVgtHUE5exjm4Ydllo42FwIgYsdLO2lXGmRBf5Mz" crossorigin="anonymous" onload="renderMathInElement(document.body);"></script> + <!-- https://github.com/KaTeX/KaTeX/blob/main/contrib/mhchem/README.md --> + <script defer src="https://cdn.jsdelivr.net/npm/katex@0.17.0/dist/contrib/mhchem.min.js" integrity="sha384-fB8BH//9nBzROkMUsu/Dr35jWHIbnKesUo9rW0hfEgw8mZGnkAyBAjKX9F98OVuo" crossorigin="anonymous"></script> </head> <body> <div class="sidebar"> @@ -14,14 +23,59 @@ <span class="sidebar-text">altaf-files</span> </div> <div class="content"> - <section> + <section id="breadcrumb-container-section"> <div class="div-sizing" id="breadcrumb-container"> </div> </section> - <section> + <section id="list-container-section"> <div class="list-container" id="list-container"> </div> </section> + <section id="file-content-section"> + <div id="file-content" class="div-sizing"> + </div> + </section> </div> + <!-- https://facelessuser.github.io/pymdown-extensions/extensions/arithmatex/ --> + <script> +(function () { +'use strict'; + +var katexMath = (function () { + var maths = document.querySelectorAll('.arithmatex'), + tex; + + for (var i = 0; i < maths.length; i++) { + tex = maths[i].textContent || maths[i].innerText; + if (tex.startsWith('\\(') && tex.endsWith('\\)')) { + katex.render(tex.slice(2, -2), maths[i], {'displayMode': false}); + } else if (tex.startsWith('\\[') && tex.endsWith('\\]')) { + katex.render(tex.slice(2, -2), maths[i], {'displayMode': true}); + } + } +}); + +(function () { + var onReady = function onReady(fn) { + if (document.addEventListener) { + document.addEventListener("DOMContentLoaded", fn); + } else { + document.attachEvent("onreadystatechange", function () { + if (document.readyState === "interactive") { + fn(); + } + }); + } + }; + + onReady(function () { + if (typeof katex !== "undefined") { + katexMath(); + } + }); +})(); + +}()); + </script> </body> </html> @@ -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: diff --git a/markdown_renderer.py b/markdown_renderer.py new file mode 100644 index 0000000..cf3a05c --- /dev/null +++ b/markdown_renderer.py @@ -0,0 +1,71 @@ +import markdown +import frontmatter +import markdown.treeprocessors +import markdown.extensions + + +class DefaultClassTreeprocessor(markdown.treeprocessors.Treeprocessor): + def run(self, root): + for element in root.iter("a"): + print(element.attrib) + if "class" not in element.attrib: + element.set("class", "link") + elif "button" not in element.attrib["class"]: + element.set("class", element.attrib["class"] + " link") + if "href" in element.attrib and element.attrib["href"] == "": + del element.attrib["href"] + for element in root.iter("img"): + element.set("class", "img") + + +class DefaultClassExtension(markdown.extensions.Extension): + def extendMarkdown(self, md): + md.treeprocessors.register(DefaultClassTreeprocessor(md), "link_class", 2) + + +def strict_identity_slugify(value, separator): + return value + + +def md_to_html(raw_content: str, text_mode: bool = False) -> str: + metadata, content = frontmatter.parse(raw_content) + html_text = markdown.markdown( + content, + output_format="html", + extensions=["attr_list", "fenced_code", "tables", DefaultClassExtension(), "md_in_html", "pymdownx.tilde", "footnotes", "codehilite", "mdx_wikilink_plus", "toc", "pymdownx.mark", "pymdownx.arithmatex"], + extension_configs={ + "toc": { + "slugify": strict_identity_slugify + }, + "pymdownx.arithmatex": { + "generic": True + } + } + ) + + section_class = "" + section_class_only = "" + + if text_mode: + section_class = ' class="text-section"' + section_class_only = ' text-section' + + html_text = html_text.replace("<p>+-+-+-</p>", f'<section{section_class}><div class="center-grid"><div class="div-sizing">') + html_text = html_text.replace("<p>+-+-+-", f'<section{section_class}><div class="center-grid"><div class="div-sizing">') + html_text = html_text.replace("+-+-+-</p>", f'<section{section_class}><div class="center-grid"><div class="div-sizing">') + html_text = html_text.replace("-+-+-+</p>", "</div></div></section>") + html_text = html_text.replace("<p>-+-+-+", "</div></div></section>") + html_text = html_text.replace("<p>+=+=+=</p>", f'<section class="normal-section{section_class_only}"><div class="center-grid"><div class="div-sizing">') + html_text = html_text.replace("<p>+=+=+=", f'<section class="normal-section{section_class_only}"><div class="center-grid"><div class="div-sizing">') + html_text = html_text.replace("+=+=+=</p>", f'<section class="normal-section{section_class_only}"><div class="center-grid"><div class="div-sizing">') + html_text = html_text.replace("<p>{{{</p>", '<div class="flex-container flex-container-dynamic gap-row">') + html_text = html_text.replace("<p>{{{", '<div class="flex-container flex-container-dynamic gap-row">') + html_text = html_text.replace("{{{</p>", '<div class="flex-container flex-container-dynamic gap-row">') + html_text = html_text.replace("<p>}}}</p>", '</div>') + html_text = html_text.replace("}}}</p>", '</div>') + html_text = html_text.replace("<p>}}}", '</div>') + html_text = html_text.replace("<p>{&{</p>", '<div class="flex-container flex-container-dynamic gap">') + html_text = html_text.replace("<p>{&{", '<div class="flex-container flex-container-dynamic gap">') + html_text = html_text.replace("{&{</p>", '<div class="flex-container flex-container-dynamic gap">') + + return (metadata, html_text) @@ -84,6 +84,55 @@ section { justify-content: center; } +ul { + line-height: 1.5rem; +} + +p { + line-height: 1.5rem; +} + +h1 { + font-family: "JetBrains Mono"; + font-size: 2.5rem; + font-weight: 600; +} + +h2 { + font-family: "JetBrains Mono"; + font-size: 2.1rem; + font-weight: 600; + margin-bottom: 1rem; +} + +h3 { + font-family: "JetBrains Mono"; + font-size: 1.8rem; + font-weight: 500; + margin-bottom: 1rem; +} + +h4 { + font-family: "JetBrains Mono"; + font-size: 1.5rem; + font-weight: 400; + margin-bottom: 1rem; +} + +h5 { + font-family: "JetBrains Mono"; + font-size: 1.2rem; + font-weight: 700; + margin-bottom: 1rem; +} + +h6 { + font-family: "JetBrains Mono"; + font-size: 1rem; + font-weight: 700; + margin-bottom: 1rem; +} + .div-sizing { max-width: 1200px; width: 100%; |
