Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ci] Fix version matching between RTD pages and R-package pages #6673

Merged
merged 7 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions docs/_static/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,12 @@ $(function() {

/* List each class property item on a new line
https://github.com/microsoft/LightGBM/issues/5073 */
if(window.location.pathname.toLocaleLowerCase().indexOf('pythonapi') != -1) {
if(window.location.pathname.toLocaleLowerCase().indexOf('pythonapi') !== -1) {
$('.py.property').each(function() { this.style.setProperty('display', 'inline', 'important'); });
}

/* Point to the same version of R API as the current docs version */
var current_version_elems = $('.rst-current-version');
if(current_version_elems.length !== 0) {
var current_version = $(current_version_elems[0]).contents().filter(function() {
return this.nodeType == 3;
}).text().trim().split(' ').pop();
if(current_version !== 'latest') {
$('a.reference.external[href$="/latest/R/reference/"]').each(function() {
$(this).attr('href', function (_, val) { return val.replace('/latest/', '/' + current_version + '/'); });
});
}
}

/* Collapse specified sections in the installation guide */
if(window.location.pathname.toLocaleLowerCase().indexOf('installation-guide') != -1) {
if(window.location.pathname.toLocaleLowerCase().indexOf('installation-guide') !== -1) {
$('<style>.closed, .opened {cursor: pointer;} .closed:before, .opened:before {font-family: FontAwesome; display: inline-block; padding-right: 6px;} .closed:before {content: "\\f078";} .opened:before {content: "\\f077";}</style>').appendTo('body');
var collapsable = [
'#build-threadless-version-not-recommended',
Expand Down
19 changes: 19 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
sys.path.insert(0, str(LIB_PATH))

INTERNAL_REF_REGEX = compile(r"(?P<url>\.\/.+)(?P<extension>\.rst)(?P<anchor>$|#)")
RTD_R_REF_REGEX = compile(r"(?P<begin>https://.+/)(?P<rtd_version>latest)(?P<end>/R/reference/)")


class InternalRefTransform(Transform):
Expand Down Expand Up @@ -69,6 +70,7 @@ def run(self) -> List:
os.environ["LIGHTGBM_BUILD_DOC"] = "1"
C_API = os.environ.get("C_API", "").lower().strip() != "no"
RTD = bool(os.environ.get("READTHEDOCS", ""))
RTD_VERSION = os.environ.get("READTHEDOCS_VERSION", "stable")

# If your documentation needs a minimal Sphinx version, state it here.
needs_sphinx = "2.1.0" # Due to sphinx.ext.napoleon, autodoc_typehints
Expand Down Expand Up @@ -309,6 +311,22 @@ def generate_r_docs(app: Sphinx) -> None:
raise Exception(f"An error has occurred while generating documentation for R-package\n{e}")


def replace_reference_to_r_docs(app: Sphinx) -> None:
"""Make reference to R-package documentation point to the actual version.

Parameters
----------
app : sphinx.application.Sphinx
The application object representing the Sphinx process.
"""
index_doc_path = CURR_PATH / "index.rst"
with open(index_doc_path, "r+t", encoding="utf-8") as index_doc:
content = index_doc.read()
content = RTD_R_REF_REGEX.sub(rf"\g<begin>{RTD_VERSION}\g<end>", content)
index_doc.seek(0)
index_doc.write(content)


def setup(app: Sphinx) -> None:
"""Add new elements at Sphinx initialization time.

Expand All @@ -330,6 +348,7 @@ def setup(app: Sphinx) -> None:
app.connect(
"build-finished", lambda app, _: copytree(CURR_PATH.parent / "lightgbm_r" / "docs", Path(app.outdir) / "R")
)
app.connect("builder-inited", replace_reference_to_r_docs)
app.add_transform(InternalRefTransform)
add_js_file = getattr(app, "add_js_file", False) or app.add_javascript
add_js_file("js/script.js")
Loading