File size: 19,653 Bytes
8ede856 | 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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 | from __future__ import annotations
import argparse
import posixpath
import re
from dataclasses import dataclass
from pathlib import Path, PurePosixPath
TITLE_RE = re.compile(r"^#\s+(.+)$", re.MULTILINE)
FENCED_BLOCK_RE = re.compile(
r"(^```.*?$.*?^```$|^~~~.*?$.*?^~~~$)",
re.MULTILINE | re.DOTALL,
)
INLINE_CODE_RE = re.compile(r"(`[^`]*`)")
MANIFEST_NAME = ".astrbot-wiki-sync-manifest"
SOURCE_ALIASES = {
"zh/config/providers/start.md": "zh/providers/start.md",
"en/config/providers/start.md": "en/providers/start.md",
}
LANG_CONFIG = {
"zh": {
"index_title": "# AstrBot 中文文档",
"index_intro": "该页面由 `AstrBot-docs` 自动同步到 GitHub Wiki。",
"index_links": [
("关于 AstrBot", "zh-what-is-astrbot"),
("社区", "zh-community"),
("常见问题", "zh-faq"),
],
"home_intro": "该 Wiki 由 `AstrBot-docs` 自动同步生成。",
"home_links": [
("中文文档入口", "zh-index"),
("English Docs", "Home-en"),
],
"sidebar_language_label": "Chinese",
"sidebar_home_label": "首页",
"sidebar_home_target": "Home",
"sidebar_docs_entry_label": "文档入口",
},
"en": {
"index_title": "# AstrBot English Documentation",
"index_intro": "This page is synchronized automatically from `AstrBot-docs` to the GitHub wiki.",
"index_links": [
("What is AstrBot", "en-what-is-astrbot"),
("Community", "en-community"),
("FAQ", "en-faq"),
],
"home_intro": "This wiki is synchronized automatically from `AstrBot-docs`.",
"home_links": [
("English docs entry", "en-index"),
("中文文档入口", "Home"),
],
"sidebar_language_label": "English",
"sidebar_home_label": "Home",
"sidebar_home_target": "Home-en",
"sidebar_docs_entry_label": "Docs Entry",
},
}
@dataclass
class PageInfo:
source_path: str
page_name: str
title: str
content: str
language: str
group: str
is_index: bool
@dataclass
class ResolutionResult:
resolved_path: str | None
ambiguous_matches: tuple[str, ...] = ()
@dataclass
class MarkdownLink:
start: int
end: int
prefix: str
target: str
suffix: str
@dataclass
class Segment:
kind: str
text: str
def repo_root() -> Path:
return Path(__file__).resolve().parents[1]
def discover_source_pages(source_root: str) -> tuple[str, ...]:
root = Path(source_root)
pages = []
for language in ("zh", "en"):
language_root = root / language
if not language_root.exists():
continue
for path in language_root.rglob("*.md"):
pages.append(path.relative_to(root).as_posix())
return tuple(sorted(pages))
def find_label_end(content: str, label_start: int) -> int:
index = label_start + 1
while index < len(content):
close = content.find("]", index)
if close == -1:
return -1
if close > label_start and content[close - 1] == "\\":
index = close + 1
continue
lookahead = close + 1
while lookahead < len(content) and content[lookahead].isspace():
lookahead += 1
if lookahead < len(content) and content[lookahead] == "(":
return close
index = close + 1
return -1
def find_target_end(content: str, target_start: int) -> int:
depth = 0
index = target_start
while index < len(content):
character = content[index]
if character == "\\":
index += 2
continue
if character == "(":
depth += 1
elif character == ")":
if depth == 0:
return index
depth -= 1
index += 1
return -1
def iter_markdown_links(content: str):
"""Yield inline Markdown links only.
This scanner intentionally handles inline `[]()` links used in the docs tree.
It does not parse reference-style links or arbitrary HTML.
"""
index = 0
while index < len(content):
label_start = content.find("[", index)
if label_start == -1:
break
link_start = (
label_start - 1
if label_start > 0 and content[label_start - 1] == "!"
else label_start
)
label_end = find_label_end(content, label_start)
if label_end == -1:
index = label_start + 1
continue
target_start = label_end + 1
while target_start < len(content) and content[target_start].isspace():
target_start += 1
if target_start >= len(content) or content[target_start] != "(":
index = label_end + 1
continue
target_start += 1
target_end = find_target_end(content, target_start)
if target_end == -1:
index = label_end + 1
continue
yield MarkdownLink(
start=link_start,
end=target_end + 1,
prefix=content[link_start:target_start],
target=content[target_start:target_end],
suffix=")",
)
index = target_end + 1
def split_anchor(target: str) -> tuple[str, str]:
if "#" not in target:
return target, ""
base, anchor = target.split("#", 1)
return base, f"#{anchor}"
def prepare_candidate_path(path: PurePosixPath) -> PurePosixPath:
if not path.suffix:
path = path.with_suffix(".md")
normalized = PurePosixPath(posixpath.normpath(path.as_posix()))
normalized_text = normalized.as_posix()
aliased = SOURCE_ALIASES.get(normalized_text, normalized_text)
return PurePosixPath(aliased)
def language_for_source(source_path: str) -> str:
return PurePosixPath(source_path).parts[0]
def parse_doc_target(target: str) -> tuple[str, str] | None:
if target.startswith(("http://", "https://", "mailto:", "#")):
return None
base_target, anchor = split_anchor(target)
if not base_target:
return None
suffix = PurePosixPath(base_target).suffix.lower()
if suffix and suffix != ".md":
return None
return base_target, anchor
def find_existing_source_path(
candidate: PurePosixPath,
source_root: Path,
source_pages: tuple[str, ...],
) -> ResolutionResult:
candidate_text = candidate.as_posix()
if (source_root / candidate_text).exists():
return ResolutionResult(resolved_path=candidate_text)
language = candidate.parts[0] if candidate.parts else ""
suffix = (
PurePosixPath(*candidate.parts[1:]).as_posix()
if len(candidate.parts) > 1
else ""
)
if not suffix:
return ResolutionResult(resolved_path=None)
prefix = f"{language}/"
full_suffix = f"{language}/{suffix}"
matches = [
page
for page in source_pages
if page.startswith(prefix)
and (page == full_suffix or page.endswith(f"/{suffix}"))
]
if len(matches) == 1:
return ResolutionResult(resolved_path=matches[0])
if len(matches) > 1:
return ResolutionResult(
resolved_path=None,
ambiguous_matches=tuple(sorted(matches)),
)
return ResolutionResult(resolved_path=None)
def resolve_link_path(
base_target: str,
source_path: str,
source_root: Path,
source_pages: tuple[str, ...],
) -> ResolutionResult:
source_language = language_for_source(source_path)
if base_target.startswith("/"):
target = base_target.lstrip("/")
if not target:
candidate = PurePosixPath(source_language) / "index.md"
elif target in {"en", "en/"}:
candidate = PurePosixPath("en") / "index.md"
elif target in {"zh", "zh/"}:
candidate = PurePosixPath("zh") / "index.md"
elif target.startswith(("en/", "zh/")):
candidate = PurePosixPath(target)
else:
language_root = source_language if source_language == "en" else "zh"
candidate = PurePosixPath(language_root) / target
else:
candidate = PurePosixPath(source_path).parent / base_target
candidate = prepare_candidate_path(candidate)
return find_existing_source_path(candidate, source_root, source_pages)
class LinkResolver:
def __init__(self, source_root: Path):
self.source_root = Path(source_root)
self.source_pages = discover_source_pages(str(self.source_root))
def resolve_base_target(
self, base_target: str, source_path: str
) -> ResolutionResult:
return resolve_link_path(
base_target=base_target,
source_path=source_path,
source_root=self.source_root,
source_pages=self.source_pages,
)
def resolve_markdown_target(
self, target: str, source_path: str
) -> tuple[str | None, str]:
parsed_target = parse_doc_target(target)
if parsed_target is None:
return None, ""
base_target, anchor = parsed_target
result = self.resolve_base_target(base_target, source_path)
return result.resolved_path, anchor
def rewrite_link_target(target: str, source_path: str, resolver: LinkResolver) -> str:
resolved, anchor = resolver.resolve_markdown_target(target, source_path)
if resolved is None:
return target
return f"{page_name_for_source(resolved)}{anchor}"
def rewrite_links_in_segment(
segment: str,
source_path: str,
resolver: LinkResolver,
) -> str:
links = list(iter_markdown_links(segment))
if not links:
return segment
result: list[str] = []
previous_end = 0
for link in links:
result.append(segment[previous_end : link.start])
result.append(
f"{link.prefix}{rewrite_link_target(link.target, source_path, resolver)}{link.suffix}",
)
previous_end = link.end
result.append(segment[previous_end:])
return "".join(result)
def iter_segments(content: str):
last_end = 0
for fenced in FENCED_BLOCK_RE.finditer(content):
before = content[last_end : fenced.start()]
if before:
last_inline_end = 0
for inline in INLINE_CODE_RE.finditer(before):
if inline.start() > last_inline_end:
yield Segment("text", before[last_inline_end : inline.start()])
yield Segment("inline_code", inline.group(0))
last_inline_end = inline.end()
if last_inline_end < len(before):
yield Segment("text", before[last_inline_end:])
yield Segment("code_block", fenced.group(0))
last_end = fenced.end()
tail = content[last_end:]
if not tail:
return
last_inline_end = 0
for inline in INLINE_CODE_RE.finditer(tail):
if inline.start() > last_inline_end:
yield Segment("text", tail[last_inline_end : inline.start()])
yield Segment("inline_code", inline.group(0))
last_inline_end = inline.end()
if last_inline_end < len(tail):
yield Segment("text", tail[last_inline_end:])
def rewrite_links(
content: str,
source_path: str,
resolver: LinkResolver,
) -> str:
output: list[str] = []
for segment in iter_segments(content):
if segment.kind == "text":
output.append(
rewrite_links_in_segment(
segment.text,
source_path=source_path,
resolver=resolver,
)
)
continue
output.append(segment.text)
return "".join(output)
def find_unresolved_doc_links(source_root: Path) -> list[str]:
unresolved: list[str] = []
root = Path(source_root)
resolver = LinkResolver(root)
for source_path in resolver.source_pages:
content = (root / source_path).read_text(encoding="utf-8")
for link in iter_markdown_links(content):
resolved_path, _ = resolver.resolve_markdown_target(
link.target, source_path
)
if resolved_path is not None:
continue
parsed_target = parse_doc_target(link.target)
if parsed_target is None:
continue
base_target, _ = parsed_target
resolution = resolver.resolve_base_target(base_target, source_path)
if resolution.ambiguous_matches:
unresolved.append(
f"{source_path} -> {link.target} (ambiguous: {', '.join(resolution.ambiguous_matches)})",
)
continue
unresolved.append(f"{source_path} -> {link.target}")
return unresolved
def check_unresolved_doc_links(source_root: Path) -> None:
unresolved = find_unresolved_doc_links(source_root)
if not unresolved:
return
issues = "\n".join(f"- {item}" for item in unresolved)
raise ValueError(f"Unresolved internal doc links found:\n{issues}")
def page_name_for_source(source_path: str) -> str:
if not source_path.endswith(".md"):
raise ValueError(f"Unsupported source path: {source_path}")
return source_path[:-3].replace("/", "-")
def strip_frontmatter(content: str) -> str:
if not content.startswith("---\n"):
return content
closing = content.find("\n---\n", 4)
if closing == -1:
return content
return content[closing + 5 :].lstrip("\n")
def normalize_content(content: str) -> str:
stripped = content.rstrip()
if not stripped:
return ""
return f"{stripped}\n"
def default_title_for_source(source_path: str) -> str:
stem = PurePosixPath(source_path).stem
return stem.replace("-", " ")
def extract_title(content: str, source_path: str) -> str:
match = TITLE_RE.search(content)
if match:
return match.group(1).strip()
return default_title_for_source(source_path)
def build_language_index(language: str, page_names: set[str]) -> str:
config = LANG_CONFIG[language]
lines = [config["index_title"], "", config["index_intro"], ""]
for label, page_name in config["index_links"]:
if page_name in page_names:
lines.append(f"- [{label}]({page_name})")
return normalize_content("\n".join(lines))
def build_home_page(language: str) -> str:
config = LANG_CONFIG[language]
lines = ["# AstrBot Wiki", "", config["home_intro"], ""]
for label, target in config["home_links"]:
lines.append(f"- [{label}]({target})")
return normalize_content("\n".join(lines))
def build_sidebar(page_infos: list[PageInfo]) -> str:
lines: list[str] = []
for language in ("zh", "en"):
config = LANG_CONFIG[language]
infos = [
info
for info in page_infos
if info.language == language and not info.is_index
]
infos.sort(key=lambda info: info.source_path)
lines.append(f"### {config['sidebar_language_label']}")
lines.append("")
lines.append(
f"- [{config['sidebar_home_label']}]({config['sidebar_home_target']})",
)
lines.append(
f"- [{config['sidebar_docs_entry_label']}]({language}-index)",
)
grouped: dict[str, list[PageInfo]] = {}
for info in infos:
grouped.setdefault(info.group, []).append(info)
for group_name in sorted(grouped):
lines.append(f"- {group_name}")
for info in grouped[group_name]:
lines.append(f" - [{info.title}]({info.page_name})")
lines.append("")
return normalize_content("\n".join(lines))
def build_page_info(
source_root: Path, source_path: str, resolver: LinkResolver
) -> PageInfo:
source_file = source_root / source_path
content = source_file.read_text(encoding="utf-8")
content = strip_frontmatter(content)
content = rewrite_links(content, source_path=source_path, resolver=resolver)
content = normalize_content(content)
relative = PurePosixPath(source_path)
parts = relative.parts
group = "Top Level" if len(parts) <= 2 else parts[1].replace("-", " ")
return PageInfo(
source_path=source_path,
page_name=page_name_for_source(source_path),
title=extract_title(content, source_path),
content=content,
language=language_for_source(source_path),
group=group,
is_index=relative.name == "index.md",
)
def read_manifest(wiki_root: Path) -> set[str]:
manifest_path = wiki_root / MANIFEST_NAME
if not manifest_path.exists():
return set()
return {
line.strip()
for line in manifest_path.read_text(encoding="utf-8").splitlines()
if line.strip()
}
def write_manifest(wiki_root: Path, file_names: set[str]) -> None:
manifest_path = wiki_root / MANIFEST_NAME
content = "\n".join(sorted(file_names))
if content:
content = f"{content}\n"
manifest_path.write_text(content, encoding="utf-8")
def write_file(path: Path, content: str) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content, encoding="utf-8")
def sync_docs_to_wiki(source_root: Path, wiki_root: Path) -> None:
source_root = Path(source_root)
wiki_root = Path(wiki_root)
wiki_root.mkdir(parents=True, exist_ok=True)
resolver = LinkResolver(source_root)
page_infos = [
build_page_info(source_root, source_path, resolver)
for source_path in resolver.source_pages
]
page_names = {info.page_name for info in page_infos}
for info in page_infos:
if info.is_index and not info.content.strip():
generated = build_language_index(info.language, page_names)
info.content = generated
info.title = extract_title(generated, info.source_path)
desired_files = {f"{info.page_name}.md": info.content for info in page_infos}
desired_files["Home.md"] = build_home_page("zh")
desired_files["Home-en.md"] = build_home_page("en")
desired_files["_Sidebar.md"] = build_sidebar(page_infos)
previously_managed = read_manifest(wiki_root)
for existing_name in previously_managed - set(desired_files):
existing_path = wiki_root / existing_name
if existing_path.exists():
existing_path.unlink()
for file_name, content in desired_files.items():
write_file(wiki_root / file_name, content)
managed_files = set(desired_files)
write_manifest(wiki_root, managed_files)
def main() -> int:
parser = argparse.ArgumentParser(
description="Sync AstrBot docs content to GitHub wiki pages."
)
parser.add_argument(
"--source-root",
default=str(repo_root()),
help="Path to the AstrBot-docs repository root.",
)
parser.add_argument(
"--wiki-root",
help="Path to the checked out wiki repository.",
)
parser.add_argument(
"--check-links-only",
action="store_true",
help="Validate internal doc links without writing wiki files.",
)
args = parser.parse_args()
if not args.check_links_only and not args.wiki_root:
parser.error("--wiki-root is required unless --check-links-only is set")
check_unresolved_doc_links(Path(args.source_root))
if args.check_links_only:
return 0
sync_docs_to_wiki(
source_root=Path(args.source_root), wiki_root=Path(args.wiki_root)
)
return 0
if __name__ == "__main__":
raise SystemExit(main())
|