Skip to content

Commit a8d65f9

Browse files
authored
RTD remap trick since sphinx hides the config (#2521)
* RTD remap trick since sphinx hides the config * Comments + we are no longer asking nicely to copy files
1 parent d672539 commit a8d65f9

File tree

3 files changed

+71
-23
lines changed

3 files changed

+71
-23
lines changed

doc/conf.py

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""Sphinx configuration file"""
33
from __future__ import annotations
44

5+
import os
56
from functools import cache
67
import logging
78
from pathlib import Path
@@ -27,8 +28,23 @@
2728
for i in range(2):
2829
log.info(f" {i}: {sys.path[i]!r}")
2930

31+
32+
# Grab readthedocs env variables for logging + use
33+
# https://docs.readthedocs.com/platform/stable/reference/environment-variables.html
34+
# Their GH comments suggest they want to move away from "magic" injection as
35+
# part of the readthedocs theme, so this seems like the best option for us.
36+
log.info(" Env variables...")
37+
col_width = max(map(len, os.environ.keys()))
38+
READTHEDOCS = dict()
39+
ENV = dict()
40+
for k, v in os.environ.items():
41+
if k.startswith('READTHEDOCS_'):
42+
READTHEDOCS[k.removeprefix('READTHEDOCS_')] = v
43+
ENV[k] = v
44+
3045
from util.doc_helpers.real_filesystem import copy_media
3146

47+
3248
# As of pyglet==2.1.dev7, this is no longer set in pyglet/__init__.py
3349
# because Jupyter / IPython always load Sphinx into sys.modules. See
3450
# the following for more info:
@@ -44,6 +60,10 @@
4460
log.info(f"Absolute path for the arcade module : {str(REPO_LOCAL_ROOT)!r}")
4561
log.info(f"Absolute path for the util dir : {str(UTIL_DIR)!r}")
4662

63+
print()
64+
for k, v in ENV.items():
65+
log.info(f"Env variable {k:{col_width}} : {v!r}")
66+
4767
# _temp_version = (REPO_LOCAL_ROOT / "arcade" / "VERSION").read_text().replace("-",'')
4868

4969
# Don't change to
@@ -52,26 +72,23 @@
5272
from version import VERSION # pyright: ignore [reportMissingImports]
5373
log.info(f" Got version {VERSION!r}")
5474

55-
5675
# Check whether the version ends in an all-digit string
57-
VERSION_PARTS = []
76+
ARCADE_VERSION_PARTS = []
5877
for part in VERSION.split('.'):
5978
if part.isdigit():
60-
VERSION_PARTS.append(int(part))
79+
ARCADE_VERSION_PARTS.append(part)
6180
else:
62-
VERSION_PARTS.append(part)
81+
ARCADE_VERSION_PARTS.append(part)
6382

6483
print()
65-
if VERSION_PARTS[-1].isdigit():
66-
GIT_REF = VERSION
67-
log.info(" !!!!! APPEARS TO BE A REAL RELEASE !!!!!")
84+
GIT_REF = 'development'
85+
if READTHEDOCS:
86+
if READTHEDOCS.get('VERSION') in ('latest', 'stable'):
87+
log.info(" !!!!! APPEARS TO BE A REAL RELEASE !!!!!")
88+
else:
89+
log.info(" +++++ Building a PR or development +++++")
6890
else:
69-
GIT_REF = 'development'
70-
log.info(" - - - Building as a dev release - - -")
71-
72-
print()
73-
print(f" {GIT_REF=!r}")
74-
print(f" {VERSION=!r}")
91+
log.info(" - - - Building outside readthedocs +++++")
7592
print()
7693

7794

@@ -80,11 +97,14 @@
8097
FMT_URL_REF_BASE=f"{REPO_URL_BASE}/blob/{GIT_REF}"
8198

8299
RESOURCE_GLOBALS = dict(
83-
GIT_REF=GIT_REF,
100+
GIT_REF=GIT_REF, # pending: post-3.0 clean-up, not sure if things use it now?
101+
# This may be more useful according to some doc? (It's unclear)
102+
# https://docs.readthedocs.com/platform/stable/reference/environment-variables.html#envvar-READTHEDOCS_GIT_COMMIT_HASH
84103
BASE_URL_REPO=REPO_URL_BASE,
85104
# This double-bracket escapes brackets in f-strings
86105
FMT_URL_REF_PAGE=f"{FMT_URL_REF_BASE}/{{}}",
87106
FMT_URL_REF_EMBED=f"{FMT_URL_REF_BASE}/{{}}?raw=true",
107+
RTD_EVIL=READTHEDOCS['CANONICAL_URL'] if READTHEDOCS else "" # pending: post-3.0 cleanup
88108
)
89109

90110
def run_util(filename, run_name="__main__", init_globals=None):
@@ -119,6 +139,8 @@ def run_util(filename, run_name="__main__", init_globals=None):
119139
run_util('../util/update_quick_index.py')
120140

121141

142+
OUT_STATIC = REPO_LOCAL_ROOT / 'build/html/_static/'
143+
122144
src_res_dir = ARCADE_MODULE / 'resources/assets'
123145
out_res_dir = REPO_LOCAL_ROOT / 'build/html/_static/assets'
124146

@@ -133,6 +155,25 @@ def run_util(filename, run_name="__main__", init_globals=None):
133155
}
134156
copy_media(src_res_dir, out_res_dir, copy_what)
135157

158+
# We are no longer asking. We are copying.
159+
copy_media(
160+
REPO_LOCAL_ROOT / "doc/_static/icons",
161+
OUT_STATIC / "icons" ,
162+
{
163+
'tabler': ("*.svg",)
164+
}
165+
)
166+
copy_media(
167+
REPO_LOCAL_ROOT / "doc/_static/",
168+
OUT_STATIC ,
169+
{
170+
'filetiles': ("*.png",)
171+
}
172+
)
173+
#copy_media(
174+
# REP / ""
175+
#)
176+
136177

137178
autodoc_inherit_docstrings = False
138179
autodoc_default_options = {

util/create_resources_listing.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ def announce_templating(var_name):
4343

4444
# The following are provided via runpy.run_path's init_globals keyword
4545
# in conf.py. Uncomment for easy debugger run without IDE config.
46+
_ = RTD_EVIL # noqa # explode ASAP or the links will all be broken
47+
log.info(f" RTD EVIL: {RTD_EVIL!r}") # noqa
4648
try:
49+
4750
_ = GIT_REF # noqa
4851
except Exception as _:
4952
GIT_REF = "development"
@@ -61,6 +64,10 @@ def announce_templating(var_name):
6164
announce_templating("FMT_URL_REF_EMBED")
6265

6366

67+
def src_kludge(strpath): # pending: post-3.0 cleanup: # evil evil evil evil
68+
"""We inject what RTD says the canonical domain is up top + the version"""
69+
return f"{RTD_EVIL}{strpath}"
70+
6471
MODULE_DIR = Path(__file__).parent.resolve()
6572
ARCADE_ROOT = MODULE_DIR.parent
6673
RESOURCE_DIR = ARCADE_ROOT / "arcade" / "resources"
@@ -495,7 +502,7 @@ def html_copyable(
495502
f" <span class=\"pre\">{escaped}</span>\n"
496503
f" </code>\n"
497504
f" <button class=\"arcade-ezcopy\" data-clipboard-text=\"{resource_handle}\">\n"
498-
f" <img src=\"/{COPY_BUTTON_PATH}\"/>\n"
505+
f" <img src=\"/{src_kludge(COPY_BUTTON_PATH)}\"/>\n"
499506
# + indent(" " * 2, COPY_BUTTON_RAW) + # pending: post-3.0 cleanup
500507
f" </button>\n"
501508
f"</span>\n"
@@ -621,17 +628,16 @@ def do_filetile(out, suffix: str | None = None, state: str = None):
621628
p = FILETILE_DIR / f"type-{suffix.strip('.')}.png"
622629
log.info(f" FILETILE: {p}")
623630
if p.exists():
624-
print(" KNOWN!")
631+
print(f" KNOWN! {p.name!r}")
625632
name = p.name
626633
else:
627634
name = f"type-unknown.png"
628635
print(" ... unknown :(")
629636
else:
630637
name = "state-error.png"
631-
632638
out.write(indent(f" ",
633639
f".. raw:: html\n\n"
634-
f" <img class=\"resource-thumb\" src=\"/_static/filetiles/{name}\"/>\n\n"))
640+
f" <img class=\"resource-thumb\" src=\"{src_kludge('/_static/filetiles/' + name)}\"/>\n\n"))
635641

636642

637643
def process_resource_files(
@@ -723,7 +729,7 @@ def start():
723729
out.write(indent(" ",
724730
# Using preload="none" is gentler on GitHub and readthedocs
725731
f"<{kind} class=\"resource-thumb\" controls preload=\"none\">\n"
726-
f" <source src='{file_path}' type='{kind}/{mime_suffix}'>\n"
732+
f" <source src=\"{src_kludge(file_path)}\" type=\"{kind}/{mime_suffix}\">\n"
727733
f"</{kind}>\n\n"))
728734

729735
# Fonts
@@ -743,7 +749,7 @@ def start():
743749

744750
# File tiles we don't have previews for
745751
else:# suffix == ".json":
746-
file_path = FMT_URL_REF_PAGE.format(resource_path)
752+
# file_path = FMT_URL_REF_PAGE.format(resource_path)
747753
out.write(f" {start()} - .. raw:: html\n\n")
748754
out.write(indent(" ",
749755
resource_copyable))
@@ -802,7 +808,7 @@ def resources():
802808
f" <ol>\n"
803809
f" <li>A <strong>file name</strong> as a single-quoted string (<code class=\"docutils literal notranslate\"><span class=\"pre\">{logo}</span></code>)</li>\n"
804810
f" <li>A <strong>copy button</strong> to the right of the string (<div class=\"arcade-ezcopy doc-ui-example-dummy\" style=\"display: inline-block;\">"
805-
f"<img src=\"/_static/icons/tabler/copy.svg\"></div>)</li>\n"
811+
f"<img src=\"{src_kludge('/_static/icons/tabler/copy.svg')}\"></div>)</li>\n"
806812
f" </ol>\n\n"
807813
+
808814
"Click the button above a preview to copy the **resource handle** string for loading the asset.\n"

util/doc_helpers/real_filesystem.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
FILE = Path(__file__)
1515
REPO_ROOT = Path(__file__).parent.parent.resolve()
16-
log = logging.getLogger(str(FILE.relative_to(REPO_ROOT)))
16+
log = logging.getLogger(FILE.name)
1717

1818

1919
def dest_older(src: Path | str, dest: Path | str) -> bool:
@@ -89,7 +89,8 @@ def sync_dir(src_dir: Path, dest_dir: Path, *globs: str, done: set | None = None
8989
log.info(f' Copying media file {src_file} to {dest_file}')
9090

9191
shutil.copyfile(src_file, dest_file)
92-
92+
else:
93+
log.info(f" Skipping media file {src_file} to {dest_file}")
9394

9495
def copy_media(
9596
src_root: Path | str,

0 commit comments

Comments
 (0)