-
-
Notifications
You must be signed in to change notification settings - Fork 301
Expand file tree
/
Copy pathupdate_docs_index.py
More file actions
221 lines (187 loc) · 6.39 KB
/
Copy pathupdate_docs_index.py
File metadata and controls
221 lines (187 loc) · 6.39 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# SPDX-FileCopyrightText: 2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
"""
Update documentation index pages on release.
This script performs two tasks:
1. Generates or updates ``index.html`` in the versioned docs repository
(``PyThaiNLP/docs``) to list all released versions as a landing page.
2. Injects or updates a released-versions section in ``index.html`` of the
development docs repository (``PyThaiNLP/dev-docs``).
Usage::
python update_docs_index.py \\
--docs-dir /path/to/docs \\
--dev-docs-index /path/to/dev-docs/index.html \\
--version 5.3.1
"""
from __future__ import annotations
import argparse
import re
import sys
from pathlib import Path
# Markers used to delimit the injected versions section in dev-docs/index.html.
_VERSIONS_BEGIN: str = "<!-- BEGIN VERSIONS -->"
_VERSIONS_END: str = "<!-- END VERSIONS -->"
_VERSIONS_SECTION_TMPL: str = """\
<!-- BEGIN VERSIONS -->
<section id="released-versions">
<h2>Released versions</h2>
<ul>
{items}
</ul>
</section>
<!-- END VERSIONS -->"""
_VERSION_ITEM_TMPL: str = (
' <li><a href="https://pythainlp.github.io/docs/{v}/">{v}</a></li>'
)
_DOCS_INDEX_TMPL: str = """\
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PyThaiNLP Documentation</title>
<style>
body {{
font-family: sans-serif;
max-width: 800px;
margin: 2rem auto;
padding: 0 1rem;
}}
h1 {{ color: #333; }}
ul {{ list-style: none; padding: 0; }}
li {{ margin: 0.5rem 0; }}
a {{ color: #0066cc; text-decoration: none; }}
a:hover {{ text-decoration: underline; }}
</style>
</head>
<body>
<h1>PyThaiNLP Documentation</h1>
<p>
Latest development docs:
<a href="https://pythainlp.github.io/dev-docs/">dev-docs</a>
</p>
<h2>Released versions</h2>
<ul>
{items}
</ul>
</body>
</html>
"""
def _is_version_dir(name: str) -> bool:
"""Return True if *name* looks like a version directory (e.g. ``5.3.1``)."""
return bool(re.match(r"^\d+\.\d+", name))
def _version_key(version: str) -> tuple:
"""Return a sort key for a version string."""
parts = re.split(r"[.\-]", version)
key: list[int] = []
for part in parts:
try:
key.append(int(part))
except ValueError:
break
return tuple(key)
def _get_version_dirs(docs_dir: Path) -> list[str]:
"""Return version directories in *docs_dir*, sorted newest-first."""
versions = [
p.name
for p in docs_dir.iterdir()
if p.is_dir() and _is_version_dir(p.name)
]
versions.sort(key=_version_key, reverse=True)
return versions
def update_docs_index(docs_dir: Path) -> None:
"""Generate or overwrite ``docs_dir/index.html`` listing all versions.
:param docs_dir: Path to the versioned documentation repository root.
"""
versions = _get_version_dirs(docs_dir)
items = "\n".join(
f' <li><a href="{v}/">{v}</a></li>' for v in versions
)
content = _DOCS_INDEX_TMPL.format(items=items)
index_path = docs_dir / "index.html"
index_path.write_text(content, encoding="utf-8")
print(f"Updated {index_path}")
def update_dev_docs_index(index_path: Path, version: str) -> None:
"""Inject or update a released-versions section in *index_path*.
Looks for ``<!-- BEGIN VERSIONS -->`` / ``<!-- END VERSIONS -->`` markers.
If found, prepends *version* to the existing list (skips if already there).
If not found, appends a new section before ``</body>`` (or at end of file).
:param index_path: Path to ``dev-docs/index.html``.
:param version: Version string without leading ``v`` (e.g. ``5.3.1``).
"""
content = index_path.read_text(encoding="utf-8")
new_item = _VERSION_ITEM_TMPL.format(v=version)
if _VERSIONS_BEGIN in content and _VERSIONS_END in content:
begin = content.index(_VERSIONS_BEGIN)
end = content.index(_VERSIONS_END) + len(_VERSIONS_END)
section = content[begin:end]
# Skip if this version is already listed.
if f"/{version}/" in section:
print(f"{index_path}: version {version} already present, skipping.")
return
# Prepend the new item inside the existing <ul> or <ol>.
section = re.sub(
r"(<[uo]l[^>]*>)",
rf"\1\n{new_item}",
section,
count=1,
)
content = content[:begin] + section + content[end:]
else:
# No markers yet — append a new section.
new_section = _VERSIONS_SECTION_TMPL.format(items=new_item)
if "</body>" in content:
content = content.replace("</body>", f"\n{new_section}\n</body>")
else:
content = content + f"\n{new_section}\n"
index_path.write_text(content, encoding="utf-8")
print(f"Updated {index_path}")
def main() -> None:
"""Entry point."""
parser = argparse.ArgumentParser(
description="Update PyThaiNLP documentation index pages on release.",
)
parser.add_argument(
"--docs-dir",
type=Path,
help=(
"Path to the versioned docs repository. "
"Generates or overwrites index.html listing all version directories."
),
)
parser.add_argument(
"--dev-docs-index",
type=Path,
help=(
"Path to dev-docs/index.html. "
"Injects or updates a released-versions section."
),
)
parser.add_argument(
"--version",
required=True,
help="Version string without leading 'v' (e.g. 5.3.1).",
)
args = parser.parse_args()
if not args.docs_dir and not args.dev_docs_index:
parser.error("At least one of --docs-dir or --dev-docs-index is required.")
if args.docs_dir:
if not args.docs_dir.is_dir():
print(
f"Error: --docs-dir '{args.docs_dir}' is not a directory.",
file=sys.stderr,
)
sys.exit(1)
update_docs_index(args.docs_dir)
if args.dev_docs_index:
if not args.dev_docs_index.exists():
print(
f"Warning: --dev-docs-index '{args.dev_docs_index}' does not exist, "
"skipping.",
file=sys.stderr,
)
else:
update_dev_docs_index(args.dev_docs_index, args.version)
if __name__ == "__main__":
main()