Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

build_docs.py 4.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. """
  3. This Python script is designed to automate the building and post-processing of MkDocs documentation, particularly for
  4. projects with multilingual content. It streamlines the workflow for generating localized versions of the documentation
  5. and updating HTML links to ensure they are correctly formatted.
  6. Key Features:
  7. - Automated building of MkDocs documentation: The script compiles both the main documentation and
  8. any localized versions specified in separate MkDocs configuration files.
  9. - Post-processing of generated HTML files: After the documentation is built, the script updates all
  10. HTML files to remove the '.md' extension from internal links. This ensures that links in the built
  11. HTML documentation correctly point to other HTML pages rather than Markdown files, which is crucial
  12. for proper navigation within the web-based documentation.
  13. Usage:
  14. - Run the script from the root directory of your MkDocs project.
  15. - Ensure that MkDocs is installed and that all MkDocs configuration files (main and localized versions)
  16. are present in the project directory.
  17. - The script first builds the documentation using MkDocs, then scans the generated HTML files in the 'site'
  18. directory to update the internal links.
  19. - It's ideal for projects where the documentation is written in Markdown and needs to be served as a static website.
  20. Note:
  21. - This script is built to be run in an environment where Python and MkDocs are installed and properly configured.
  22. """
  23. import re
  24. import shutil
  25. import subprocess
  26. from pathlib import Path
  27. DOCS = Path(__file__).parent.resolve()
  28. SITE = DOCS.parent / 'site'
  29. def build_docs():
  30. """Build docs using mkdocs."""
  31. if SITE.exists():
  32. print(f'Removing existing {SITE}')
  33. shutil.rmtree(SITE)
  34. # Build the main documentation
  35. print(f'Building docs from {DOCS}')
  36. subprocess.run(f'mkdocs build -f {DOCS}/mkdocs.yml', check=True, shell=True)
  37. # Build other localized documentations
  38. for file in DOCS.glob('mkdocs_*.yml'):
  39. print(f'Building MkDocs site with configuration file: {file}')
  40. subprocess.run(f'mkdocs build -f {file}', check=True, shell=True)
  41. print(f'Site built at {SITE}')
  42. def update_html_links():
  43. """Update href links in HTML files to remove '.md' and '/index.md', excluding links starting with 'https://'."""
  44. html_files = Path(SITE).rglob('*.html')
  45. total_updated_links = 0
  46. for html_file in html_files:
  47. with open(html_file, 'r+', encoding='utf-8') as file:
  48. content = file.read()
  49. # Find all links to be updated, excluding those starting with 'https://'
  50. links_to_update = re.findall(r'href="(?!https://)([^"]+?)(/index)?\.md"', content)
  51. # Update the content and count the number of links updated
  52. updated_content, number_of_links_updated = re.subn(r'href="(?!https://)([^"]+?)(/index)?\.md"',
  53. r'href="\1"', content)
  54. total_updated_links += number_of_links_updated
  55. # Special handling for '/index' links
  56. updated_content, number_of_index_links_updated = re.subn(r'href="([^"]+)/index"', r'href="\1/"',
  57. updated_content)
  58. total_updated_links += number_of_index_links_updated
  59. # Write the updated content back to the file
  60. file.seek(0)
  61. file.write(updated_content)
  62. file.truncate()
  63. # Print updated links for this file
  64. for link in links_to_update:
  65. print(f'Updated link in {html_file}: {link[0]}')
  66. print(f'Total number of links updated: {total_updated_links}')
  67. def update_page_title(file_path: Path, new_title: str):
  68. """Update the title of an HTML file."""
  69. # Read the content of the file
  70. with open(file_path, encoding='utf-8') as file:
  71. content = file.read()
  72. # Replace the existing title with the new title
  73. updated_content = re.sub(r'<title>.*?</title>', f'<title>{new_title}</title>', content)
  74. # Write the updated content back to the file
  75. with open(file_path, 'w', encoding='utf-8') as file:
  76. file.write(updated_content)
  77. def main():
  78. # Build the docs
  79. build_docs()
  80. # Update .md in href links
  81. update_html_links()
  82. # Show command to serve built website
  83. print('Serve site at http://localhost:8000 with "python -m http.server --directory site"')
  84. # Update titles
  85. update_page_title(SITE / '404.html', new_title='Ultralytics Docs - Not Found')
  86. if __name__ == '__main__':
  87. main()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...