diff --git a/docs/_static/rtd-dummy.js b/docs/_static/rtd-dummy.js deleted file mode 100644 index 4eee3c7..0000000 --- a/docs/_static/rtd-dummy.js +++ /dev/null @@ -1,14 +0,0 @@ -var READTHEDOCS_DATA = { - project: "shibuya", - version: "latest", - language: "en", - builder: "sphinx", - programming_language: "py", - api_host: "https://readthedocs.org", - proxied_api_host: "https://readthedocs.org", - proxied_static_path: "https://assets.readthedocs.org/", - theme: "shibuya", - features: { - docsearch_disabled: false, - }, -} diff --git a/docs/_templates/partials/extra-head.html b/docs/_templates/partials/extra-head.html new file mode 100644 index 0000000..7f74d00 --- /dev/null +++ b/docs/_templates/partials/extra-head.html @@ -0,0 +1,7 @@ +{% if DEBUG_READTHEDOCS %} + + + + + +{% endif %} diff --git a/docs/conf.py b/docs/conf.py index 2e31aac..3be34b7 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -138,6 +138,11 @@ } if "READTHEDOCS" in os.environ: + html_context = { + "source_type": "github", + "source_user": "lepture", + "source_repo": "shibuya", + } html_theme_options["carbon_ads_code"] = "" html_theme_options["announcement"] = ( "This documentation is hosted on Read the Docs only for testing. Please use " @@ -154,37 +159,9 @@ } -DEBUG_RTD = False +DEBUG_RTD = True if DEBUG_RTD: os.environ['READTHEDOCS_PROJECT'] = 'shibuya' - os.environ['READTHEDOCS_VERSION'] = 'latest' - html_context["READTHEDOCS"] = True - html_context["slug"] = "shibuya" - html_css_files = [ - "https://assets.readthedocs.org/static/css/readthedocs-doc-embed.css", - "https://assets.readthedocs.org/static/css/badge_only.css", - ] - html_js_files = [ - "rtd-dummy.js", - ( - "https://assets.readthedocs.org/static/javascript/readthedocs-doc-embed.js", - {"async": "async"}, - ), - ] - + html_context["DEBUG_READTHEDOCS"] = True html_theme_options["carbon_ads_code"] = None - html_context["current_version"] = "latest" - html_context["versions"] = [ - ("latest", "/en/latest/"), - ("stable", "/en/stable/"), - ("v3", "/en/v3/"), - ("v2.0.5", "/en/v2.0.5/"), - ("v2.0.4", "/en/v2.0.4/"), - ("v2", "/en/v2/"), - ("v0.8.4", "/en/v0.8.4/"), - ] - html_context["languages"] = [ - ("πΊπΈ English", "/en/%s/", "en"), - ("π¨π³ δΈζ", "/zh/%s/", "zh"), - ] diff --git a/serve.py b/serve.py index d93090e..d6a3c42 100644 --- a/serve.py +++ b/serve.py @@ -1,8 +1,39 @@ +from tornado.web import RequestHandler +from tornado.httpclient import AsyncHTTPClient from livereload import Server, shell shell("make build-docs")() -app = Server() + +class ProxyHandler(RequestHandler): + proxy_url = "https://shibuya.readthedocs.io/_/" + + async def get(self, path: str) -> None: + query = self.request.query + url = self.proxy_url + path + if query: + url += '?' + query + client = AsyncHTTPClient() + response = await client.fetch(url) + self.set_status(response.code) + if response.body: + for header in response.headers: + if header.lower() == "content-length": + self.set_header(header, str(max(len(response.body), int(response.headers.get(header))))) + elif header.lower() != "transfer-encoding": + self.set_header(header, response.headers.get(header)) + self.write(response.body) + self.finish() + + +class DebugServer(Server): + def get_web_handlers(self, script): + proxy = (r"/_/(.*)", ProxyHandler) + handlers = super().get_web_handlers(script) + return [proxy, *handlers] + + +app = DebugServer() app.watch("src", shell("make build-docs"), delay=2) app.watch("docs", shell("make build-docs"), delay=2) app.serve(root="build/_html") diff --git a/src/shibuya/__init__.py b/src/shibuya/__init__.py index e42dad5..e60304d 100644 --- a/src/shibuya/__init__.py +++ b/src/shibuya/__init__.py @@ -1,3 +1,4 @@ +import os from typing import Dict, Any from pathlib import Path from sphinx.application import Sphinx @@ -8,6 +9,7 @@ normalize_localtoc, normalize_globaltoc, create_edit_source_link, + generate_readthedocs_context, ) from ._sphinx import ( WrapperPostTransform, @@ -51,8 +53,9 @@ def _initialize_builder(app: Sphinx): app.add_css_file("print.css", media='print') if isinstance(app.builder, StandaloneHTMLBuilder): - edit_source_link = create_edit_source_link(app.config.html_context) + app.builder.templates.environment.globals.update(generate_readthedocs_context()) app.builder.templates.environment.globals['expandtoc'] = normalize_globaltoc + edit_source_link = create_edit_source_link(app.config.html_context) app.builder.templates.environment.globals['edit_source_link'] = edit_source_link app.builder.highlighter.formatter = WrapLineFormatter diff --git a/src/shibuya/context.py b/src/shibuya/context.py index 4e0f5ad..a025386 100644 --- a/src/shibuya/context.py +++ b/src/shibuya/context.py @@ -1,4 +1,5 @@ import re +import os from typing import Dict, Any import xml.etree.ElementTree as ET @@ -47,9 +48,6 @@ def normalize_globaltoc(toc: str, depth: int = 0): def create_edit_source_link(context: Dict[str, Any]): - if context.get('READTHEDOCS'): - _normalize_readthedocs_context(context) - source_type = context.get("source_type") source_user = context.get("source_user") source_repo = context.get("source_repo") @@ -78,24 +76,9 @@ def edit_source_link(filename: str) -> str: return edit_source_link -def _normalize_readthedocs_context(context: Dict[str, Any]): - if context.get("display_github"): - source_type = "github" - elif context.get("display_gitlab"): - source_type = "gitlab" - elif context.get("display_bitbucket"): - source_type = "bitbucket" - else: - source_type = None - - if source_type: - context["source_type"] = source_type - context["source_user"] = context.get(f"{source_type}_user") - context["source_repo"] = context.get(f"{source_type}_repo") - context["source_version"] = context.get(f"{source_type}_version") - context["source_docs_path"] = context.get("conf_py_path") - - slug = context.get('slug') - if slug: - context["theme_readthedocs_url"] = f"https://readthedocs.org/projects/{slug}" - return source_type +def generate_readthedocs_context(): + context = {} + project_slug = os.environ.get("READTHEDOCS_PROJECT") + if project_slug: + context["theme_readthedocs_url"] = f"https://readthedocs.org/projects/{project_slug}" + return context diff --git a/src/shibuya/theme/shibuya/layout/default.html b/src/shibuya/theme/shibuya/layout/default.html index e93cb67..6ef1dd4 100644 --- a/src/shibuya/theme/shibuya/layout/default.html +++ b/src/shibuya/theme/shibuya/layout/default.html @@ -24,9 +24,6 @@ {{ expandtoc(globaltoc, theme_globaltoc_expand_depth) }} - {%- if READTHEDOCS -%} -
- {%- endif -%} diff --git a/static/css/extensions/rtd.css b/static/css/extensions/rtd.css deleted file mode 100644 index 74685c6..0000000 --- a/static/css/extensions/rtd.css +++ /dev/null @@ -1,102 +0,0 @@ -.sy-lside { - --height: 42px; -} - -.sy-lside-bottom { - position: sticky; - bottom: 0; - padding: 0; - border-top: 1px solid var(--sy-c-divider); - background-color: var(--sy-c-background); - --tw-shadow: 0 -12px 16px var(--sy-c-background); - box-shadow: var(--tw-ring-offset-shadow,0 0 transparent),var(--tw-ring-shadow,0 0 transparent),var(--tw-shadow); -} - -.sy-lside .rst-versions { - position: static; - width: 100%; - background-color: var(--sy-c-surface); - font-family: var(--sy-f-text); -} - -.sy-lside .rst-versions a { - color: var(--sy-c-link); -} - -.sy-lside .rst-versions a:hover { - color: var(--sy-c-link-hover); -} - -.sy-lside .rst-versions .rst-current-version { - background-color: var(--sy-c-background); - color: var(--sy-c-text); - padding: 0 1rem; - height: var(--height); - line-height: var(--height); -} - -.sy-lside .rst-versions.rst-badge .rst-current-version { - text-align: left; - font-size: 0.94rem; - padding: 0 1rem; - height: var(--height); - line-height: var(--height); -} - -.sy-lside .rst-versions .rst-current-version .fa { - color: var(--sy-c-text); -} - -.sy-lside .rst-versions.rst-badge.shift-up .rst-current-version { - text-align: left; -} -.sy-lside .rst-versions.rst-badge.shift-up .rst-current-version .fa-book { - float: none -} - -.sy-lside .rst-other-versions { - font-size: 0.86rem; - border-top: 1px solid var(--sy-c-divider); -} - -.sy-lside .rst-versions.shift-up .rst-other-versions { - max-height: calc(100vh - 300px); - overflow: auto; -} - -.sy-lside .rst-other-versions dt { - font-size: 0.68rem; - font-weight: 500; - padding: 4px 6px; - color: var(--sy-c-light); -} - -.sy-lside .rst-versions .rst-other-versions dd a { - color: var(--sy-c-text); -} - -.sy-lside .rst-versions .rst-other-versions dd a:hover { - text-decoration: underline; - color: var(--sy-c-link-hover); -} - -.sy-lside .rst-versions input { - width: 100%; - padding: 6px 12px; - font-size: 0.92rem; - font-family: var(--sy-f-text); - border-radius: 6px; - outline: 0; - background: var(--sy-c-background); - border: 1px solid var(--sy-c-border); -} - -.sy-lside .rst-versions .rst-other-versions hr { - border-color: var(--sy-c-divider); -} - -@media (min-width: 90rem) { - .sy-lside .rst-versions { - background: var(--sy-c-background); - } -} diff --git a/static/css/modules.css b/static/css/modules.css index 8a400ca..3c0d382 100644 --- a/static/css/modules.css +++ b/static/css/modules.css @@ -28,7 +28,6 @@ @import "./layout/rside.css"; @import "./layout/navigation.css"; -@import "./extensions/rtd.css"; @import "./extensions/copybutton.css"; @import "./extensions/sphinx-design.css"; @import "./extensions/sphinx-tabs.css"; diff --git a/static/index.js b/static/index.js index ec16951..fd46434 100644 --- a/static/index.js +++ b/static/index.js @@ -6,6 +6,7 @@ import "./js/globaltoc" import "./js/scroller" import "./js/repo-stats" import "./js/carbon" +import "./js/rtd" if (/windows/i.test(navigator.userAgent)) { document.body.classList.add('win') diff --git a/static/js/rtd.js b/static/js/rtd.js new file mode 100644 index 0000000..1d930e7 --- /dev/null +++ b/static/js/rtd.js @@ -0,0 +1,6 @@ +document.addEventListener("readthedocs-addons-data-ready", function (event) { + document.querySelector(".searchbox input").addEventListener("focusin", () => { + const event = new CustomEvent("readthedocs-search-show"); + document.dispatchEvent(event); + }); +});