Upload app.py
Browse files
app.py
CHANGED
|
@@ -591,13 +591,34 @@ def initial_payload_for_path(rest_of_path: str) -> str | None:
|
|
| 591 |
return None
|
| 592 |
return payload_path.read_text(encoding="utf-8")
|
| 593 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 594 |
def render_index_html(rest_of_path: str = "") -> str:
|
| 595 |
html = (BASE_DIR / "static/index.html").read_text(encoding="utf-8")
|
| 596 |
initial_payload = initial_payload_for_path(rest_of_path)
|
| 597 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 598 |
return html
|
| 599 |
-
|
| 600 |
-
script = f'<script id="initial-search-data" type="application/json">{initial_payload}</script>\n'
|
| 601 |
marker = '<script src="/static/app.js"></script>'
|
| 602 |
if marker in html:
|
| 603 |
return html.replace(marker, script + marker, 1)
|
|
|
|
| 591 |
return None
|
| 592 |
return payload_path.read_text(encoding="utf-8")
|
| 593 |
|
| 594 |
+
def sidebar_payload_for_path(rest_of_path: str) -> str | None:
|
| 595 |
+
source_slug = unquote((rest_of_path or "").strip("/"))
|
| 596 |
+
if source_slug:
|
| 597 |
+
payload_path = BASE_DIR / "data" / "sidebar" / "sources" / f"{quote(source_slug, safe='')}.json"
|
| 598 |
+
else:
|
| 599 |
+
payload_path = BASE_DIR / "data" / "sidebar" / "global.json"
|
| 600 |
+
if not payload_path.exists():
|
| 601 |
+
return None
|
| 602 |
+
return payload_path.read_text(encoding="utf-8")
|
| 603 |
+
|
| 604 |
def render_index_html(rest_of_path: str = "") -> str:
|
| 605 |
html = (BASE_DIR / "static/index.html").read_text(encoding="utf-8")
|
| 606 |
initial_payload = initial_payload_for_path(rest_of_path)
|
| 607 |
+
sidebar_payload = sidebar_payload_for_path(rest_of_path)
|
| 608 |
+
global_sidebar_payload = sidebar_payload_for_path("")
|
| 609 |
+
scripts = []
|
| 610 |
+
if initial_payload:
|
| 611 |
+
initial_payload = initial_payload.replace("</", "<\\/")
|
| 612 |
+
scripts.append(f'<script id="initial-search-data" type="application/json">{initial_payload}</script>')
|
| 613 |
+
if sidebar_payload:
|
| 614 |
+
sidebar_payload = sidebar_payload.replace("</", "<\\/")
|
| 615 |
+
scripts.append(f'<script id="initial-sidebar-data" type="application/json">{sidebar_payload}</script>')
|
| 616 |
+
if global_sidebar_payload:
|
| 617 |
+
global_sidebar_payload = global_sidebar_payload.replace("</", "<\\/")
|
| 618 |
+
scripts.append(f'<script id="initial-sidebar-sources" type="application/json">{global_sidebar_payload}</script>')
|
| 619 |
+
if not scripts:
|
| 620 |
return html
|
| 621 |
+
script = "\n".join(scripts) + "\n"
|
|
|
|
| 622 |
marker = '<script src="/static/app.js"></script>'
|
| 623 |
if marker in html:
|
| 624 |
return html.replace(marker, script + marker, 1)
|