Save a single WordPress doc as Markdown files. Returns (zh_file, fm).
(self, doc: dict)
| 223 | return fm |
| 224 | |
| 225 | def save_document(self, doc: dict) -> Tuple[Optional[Path], Optional[dict]]: |
| 226 | """Save a single WordPress doc as Markdown files. Returns (zh_file, fm).""" |
| 227 | link = doc.get("link", "") |
| 228 | output_path, regulation = self.detect_output_path(link) |
| 229 | |
| 230 | title_clean = self.clean_title(doc.get("title", {}).get("rendered", "")) |
| 231 | slug = doc.get("slug") or self.slugify(title_clean) |
| 232 | |
| 233 | content_html = doc.get("content", {}).get("rendered", "") |
| 234 | content_md = self.html_to_markdown(content_html) |
| 235 | |
| 236 | # Skip near-empty docs (likely category index pages) |
| 237 | if len(content_md) < MIN_CONTENT_LENGTH: |
| 238 | return None, None |
| 239 | |
| 240 | fm = self.build_front_matter(doc, output_path, regulation) |
| 241 | fm_str = yaml.dump(fm, allow_unicode=True, default_flow_style=False, sort_keys=False) |
| 242 | |
| 243 | out_dir = self.output_dir / output_path |
| 244 | zh_file = out_dir / f"{slug}.zh.md" |
| 245 | en_file = out_dir / f"{slug}.en.md" |
| 246 | |
| 247 | if not self.dry_run: |
| 248 | out_dir.mkdir(parents=True, exist_ok=True) |
| 249 | with open(zh_file, "w", encoding="utf-8") as f: |
| 250 | f.write(f"---\n{fm_str}---\n\n") |
| 251 | f.write(f"# {title_clean}\n\n") |
| 252 | f.write(content_md) |
| 253 | f.write("\n") |
| 254 | |
| 255 | if not en_file.exists(): |
| 256 | fm_en = fm.copy() |
| 257 | fm_en["translation"] = "ai-assisted" |
| 258 | fm_en["title"] = {"zh": title_clean, "en": f"[Translation needed] {title_clean}"} |
| 259 | fm_en_str = yaml.dump( |
| 260 | fm_en, allow_unicode=True, default_flow_style=False, sort_keys=False |
| 261 | ) |
| 262 | with open(en_file, "w", encoding="utf-8") as f: |
| 263 | f.write(f"---\n{fm_en_str}---\n\n") |
| 264 | f.write(f"# [Translation needed] {title_clean}\n\n") |
| 265 | f.write("> This document requires English translation. " |
| 266 | "See CONTRIBUTING.md for translation guidelines.\n") |
| 267 | |
| 268 | return zh_file, fm |
| 269 | |
| 270 | def update_index(self, output_path: str, entries: List[dict]) -> None: |
| 271 | """Update or create _index.json for a directory.""" |
no test coverage detected