Spaces:
Sleeping
Sleeping
File size: 3,397 Bytes
055a113 d3c5e15 6f05d2a d3c5e15 055a113 d3c5e15 055a113 d3c5e15 055a113 d3c5e15 055a113 d3c5e15 055a113 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
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)
@app.route("/")
def index():
html = fetch_and_render_md()
return Response(html, mimetype="text/html")
@app.route("/view/<path:subpath>")
def view(subpath):
html = fetch_and_render_md(subpath)
return Response(html, mimetype="text/html")
if __name__ == "__main__":
app.run(port=8988)
|