Skip to content

Commit

Permalink
fix: update integration with readthedocs according to its latest update.
Browse files Browse the repository at this point in the history
  • Loading branch information
lepture committed Oct 14, 2024
1 parent 3513982 commit 47929b1
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 176 deletions.
14 changes: 0 additions & 14 deletions docs/_static/rtd-dummy.js

This file was deleted.

7 changes: 7 additions & 0 deletions docs/_templates/partials/extra-head.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% if DEBUG_READTHEDOCS %}
<script async type="text/javascript" src="https://assets.readthedocs.org/static/javascript/readthedocs-addons.js"></script>
<meta name="readthedocs-project-slug" content="shibuya" />
<meta name="readthedocs-version-slug" content="latest" />
<meta name="readthedocs-resolver-filename" content="{{ pageurl|e }}" />
<meta name="readthedocs-http-status" content="200" />
{% endif %}
37 changes: 7 additions & 30 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand All @@ -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"),
]
33 changes: 32 additions & 1 deletion serve.py
Original file line number Diff line number Diff line change
@@ -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")
5 changes: 4 additions & 1 deletion src/shibuya/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from typing import Dict, Any
from pathlib import Path
from sphinx.application import Sphinx
Expand All @@ -8,6 +9,7 @@
normalize_localtoc,
normalize_globaltoc,
create_edit_source_link,
generate_readthedocs_context,
)
from ._sphinx import (
WrapperPostTransform,
Expand Down Expand Up @@ -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

Expand Down
31 changes: 7 additions & 24 deletions src/shibuya/context.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import os
from typing import Dict, Any
import xml.etree.ElementTree as ET

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
3 changes: 0 additions & 3 deletions src/shibuya/theme/shibuya/layout/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
{{ expandtoc(globaltoc, theme_globaltoc_expand_depth) }}
</div>
</div>
{%- if READTHEDOCS -%}
<div class="sy-lside-bottom px-6" id="readthedocs-embed-flyout"></div>
{%- endif -%}
</div>
</aside>
<div class="lside-overlay js-menu" role="button" aria-label="Close left sidebar" aria-controls="lside" aria-expanded="false"></div>
Expand Down
102 changes: 0 additions & 102 deletions static/css/extensions/rtd.css

This file was deleted.

1 change: 0 additions & 1 deletion static/css/modules.css
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
1 change: 1 addition & 0 deletions static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
6 changes: 6 additions & 0 deletions static/js/rtd.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 47929b1

Please sign in to comment.