Spaces:
Running
Running
from flask import Flask, Response | |
import requests | |
import markdown | |
import re | |
app = Flask(__name__) | |
BASE_URL = "https://raw.githubusercontent.com/huggingface/xet-core/refs/heads/assaf/spec/spec/" | |
ENTRY_FILE = "spec.md" | |
# header links filenames | |
HEADER_FILES = [ | |
"spec.md", | |
"upload_protocol.md", | |
"download_protocol.md", | |
"api.md", | |
"auth.md", | |
"chunking.md", | |
"hashing.md", | |
"deduplication.md", | |
"file_reconstruction.md", | |
"xorb.md", | |
"shard.md", | |
"file_id.md" | |
] | |
CSS_STYLE = """ | |
<style> | |
body { | |
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; | |
max-width: 1000px; | |
margin: 2rem auto; | |
padding: 1rem; | |
line-height: 1.6; | |
color: #333; | |
background: #fafafa; | |
} | |
h1, h2, h3, h4 { | |
border-bottom: 1px solid #ddd; | |
padding-bottom: 0.3rem; | |
margin-top: 2rem; | |
} | |
a { | |
color: #007acc; | |
text-decoration: none; | |
} | |
a:hover { | |
text-decoration: underline; | |
} | |
pre { | |
background: #f4f4f4; | |
padding: 1rem; | |
border-radius: 6px; | |
overflow-x: auto; | |
} | |
code { | |
background: #f4f4f4; | |
padding: 0.2rem 0.4rem; | |
border-radius: 4px; | |
} | |
.header { | |
margin-top: 3rem; | |
padding: 2rem 0 1rem 0; | |
border-top: 1px solid #ddd; | |
background: #f8f8f8; | |
} | |
.header-links { | |
display: flex; | |
flex-wrap: wrap; | |
gap: 1rem; | |
justify-content: center; | |
list-style: none; | |
margin: 0; | |
padding: 0; | |
} | |
.header-links li { | |
margin: 0; | |
} | |
.header-links a { | |
padding: 0.5rem 1rem; | |
background: #fff; | |
border: 1px solid #ddd; | |
border-radius: 6px; | |
color: #333; | |
font-size: 0.9rem; | |
transition: all 0.2s ease; | |
} | |
.header-links a:hover { | |
background: #007acc; | |
color: white; | |
text-decoration: none; | |
border-color: #007acc; | |
} | |
</style> | |
""" | |
HTML_TEMPLATE = """<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Markdown Viewer</title> | |
{css} | |
</head> | |
<body> | |
<header class="header"> | |
<ul class="header-links"> | |
{header_links} | |
</ul> | |
</header> | |
{content} | |
</body> | |
</html> | |
""" | |
def fetch_and_render_md(path: str = ENTRY_FILE) -> str: | |
url = BASE_URL + path | |
resp = requests.get(url) | |
if resp.status_code != 200: | |
return f"<h1>Error {resp.status_code}</h1><p>Could not fetch {url}</p>" | |
md_text = resp.text | |
# Rewrite links: ../spec/foo.md → /view/foo.md | |
md_text = re.sub( | |
r"\.\./spec/([^\s)]+)", | |
r"/view/\1", | |
md_text | |
) | |
# Convert to HTML | |
html_content = markdown.markdown(md_text, extensions=["extra", "toc"]) | |
# Generate header links | |
header_links = "" | |
for filename in HEADER_FILES: | |
header_links += f'<li><a href="/view/{filename}">{filename}</a></li>\n ' | |
return HTML_TEMPLATE.format(css=CSS_STYLE, content=html_content, header_links=header_links) | |
def index(): | |
html = fetch_and_render_md() | |
return Response(html, mimetype="text/html") | |
def view(subpath): | |
html = fetch_and_render_md(subpath) | |
return Response(html, mimetype="text/html") | |
if __name__ == "__main__": | |
app.run(port=8988) | |