File size: 2,052 Bytes
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
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"

CSS_STYLE = """
<style>
    body {
        font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
        max-width: 800px;
        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;
    }
</style>
"""

HTML_TEMPLATE = """<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Markdown Viewer</title>
  {css}
</head>
<body>
  {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"])
    return HTML_TEMPLATE.format(css=CSS_STYLE, content=html_content)


@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)