Unverified Commit 9b351e6e authored by Nikita Titov's avatar Nikita Titov Committed by GitHub
Browse files

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

* Update script.js

* Update script.js

* Update script.js

* Update script.js

* replace url at build time

* manipulate with raw files
parent c9d1ac7b
......@@ -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',
......
......@@ -39,6 +39,7 @@ LIB_PATH = CURR_PATH.parent / "python-package"
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):
......@@ -69,6 +70,7 @@ class IgnoredDirective(Directive):
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
......@@ -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.
......@@ -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")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment