Update docs building code (#7601)
Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com> Co-authored-by: Muhammad Rizwan Munawar <chr043416@gmail.com> Co-authored-by: Muhammad Rizwan Munawar <muhammadrizwanmunawar123@gmail.com> Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
This commit is contained in:
parent
5f5f5d08f2
commit
d021524e85
9 changed files with 86 additions and 40 deletions
|
|
@ -23,14 +23,17 @@ Usage:
|
|||
Note:
|
||||
- This script is built to be run in an environment where Python and MkDocs are installed and properly configured.
|
||||
"""
|
||||
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
from tqdm import tqdm
|
||||
|
||||
DOCS = Path(__file__).parent.resolve()
|
||||
SITE = DOCS.parent / "site"
|
||||
LANGUAGES = True
|
||||
|
||||
|
||||
def build_docs():
|
||||
|
|
@ -44,9 +47,10 @@ def build_docs():
|
|||
subprocess.run(f"mkdocs build -f {DOCS}/mkdocs.yml", check=True, shell=True)
|
||||
|
||||
# Build other localized documentations
|
||||
for file in DOCS.glob("mkdocs_*.yml"):
|
||||
print(f"Building MkDocs site with configuration file: {file}")
|
||||
subprocess.run(f"mkdocs build -f {file}", check=True, shell=True)
|
||||
if LANGUAGES:
|
||||
for file in DOCS.glob("mkdocs_*.yml"):
|
||||
print(f"Building MkDocs site with configuration file: {file}")
|
||||
subprocess.run(f"mkdocs build -f {file}", check=True, shell=True)
|
||||
print(f"Site built at {SITE}")
|
||||
|
||||
|
||||
|
|
@ -100,19 +104,51 @@ def update_page_title(file_path: Path, new_title: str):
|
|||
file.write(updated_content)
|
||||
|
||||
|
||||
def update_html_head(key=""):
|
||||
"""Update the HTML head section of each file."""
|
||||
html_files = Path(SITE).rglob("*.html")
|
||||
for html_file in tqdm(html_files, desc="Processing HTML files"):
|
||||
with html_file.open("r", encoding="utf-8") as file:
|
||||
html_content = file.read()
|
||||
|
||||
script = f"""
|
||||
<script type="text/javascript" src="https://cdn.weglot.com/weglot.min.js"></script>
|
||||
<script>
|
||||
Weglot.initialize({{
|
||||
api_key: '{key}'
|
||||
}});
|
||||
</script>
|
||||
"""
|
||||
if script in html_content: # script already in HTML file
|
||||
return
|
||||
|
||||
head_end_index = html_content.lower().rfind("</head>")
|
||||
if head_end_index != -1:
|
||||
# Add the specified JavaScript to the HTML file just before the end of the head tag.
|
||||
new_html_content = html_content[:head_end_index] + script + html_content[head_end_index:]
|
||||
with html_file.open("w", encoding="utf-8") as file:
|
||||
file.write(new_html_content)
|
||||
|
||||
|
||||
def main():
|
||||
# Build the docs
|
||||
build_docs()
|
||||
|
||||
# Update titles
|
||||
update_page_title(SITE / "404.html", new_title="Ultralytics Docs - Not Found")
|
||||
|
||||
# Update .md in href links
|
||||
update_html_links()
|
||||
if LANGUAGES:
|
||||
update_html_links()
|
||||
|
||||
# Update HTML file head section
|
||||
key = os.environ.get("WEGLOT_KEY")
|
||||
if not LANGUAGES and key:
|
||||
update_html_head(key)
|
||||
|
||||
# Show command to serve built website
|
||||
print('Serve site at http://localhost:8000 with "python -m http.server --directory site"')
|
||||
|
||||
# Update titles
|
||||
update_page_title(SITE / "404.html", new_title="Ultralytics Docs - Not Found")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue