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

script: inject version selector to HTML docs #349

Merged
merged 1 commit into from
Dec 28, 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
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ lint.per-file-ignores."tests/**" = [
lint.mccabe.max-complexity = 10
# Use Google-style docstrings.
lint.pydocstyle.convention = "google"
lint.ignore-init-module-imports = true

[tool.codespell]
#skip = '*.py'
Expand Down
46 changes: 46 additions & 0 deletions scripts/inject-selector-script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# http://www.apache.org/licenses/LICENSE-2.0
#
"""Simple script to inject a custom JS script into all HTML pages in given folder.

Sample usage:
$ python scripts/inject-selector-script.py "/path/to/folder" torchmetrics

"""

import logging
import os
import sys


def inject_selector_script_into_html_file(file_path: str, script_url: str) -> None:
"""Inject a custom JS script into the given HTML file."""
with open(file_path) as fopen:
html_content = fopen.read()
html_content = html_content.replace(
"</head>",
f'<script src="{script_url}" crossorigin="anonymous" referrerpolicy="no-referrer"></script>{os.linesep}</head>',
)
with open(file_path, "w") as fopen:
fopen.write(html_content)


def main(folder: str, selector_name: str) -> None:
"""Inject a custom JS script into all HTML files in the given folder."""
# Sample: https://lightning.ai/docs/torchmetrics/version-selector.js
script_url = f"https://lightning.ai/docs/{selector_name}/version-selector.js"
html_files = [
os.path.join(root, file) for root, _, files in os.walk(folder) for file in files if file.endswith(".html")
]
for file_path in html_files:
inject_selector_script_into_html_file(file_path, script_url)


if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
try:
from fire import Fire

Fire(main)
except (ModuleNotFoundError, ImportError):
main(*sys.argv[1:])
Loading