Skip to content

Commit

Permalink
script: inject version selector to HTML docs (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
Borda authored Dec 28, 2024
1 parent dcfa9c9 commit e6c8da4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
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:])

0 comments on commit e6c8da4

Please sign in to comment.