(source: str, target_path: Path, article_map: dict[str, dict[str, str]])
| 37 | |
| 38 | |
| 39 | def translate_doc(source: str, target_path: Path, article_map: dict[str, dict[str, str]]) -> str: |
| 40 | lines = source.splitlines() |
| 41 | out: list[str] = [] |
| 42 | in_fence = False |
| 43 | fence_lang = "" |
| 44 | fence_lines: list[str] = [] |
| 45 | |
| 46 | def flush_fence() -> None: |
| 47 | nonlocal fence_lines |
| 48 | if fence_lang in {"", "prompt", "text", "txt", "markdown", "md"} and any(re.search(r"[\u4e00-\u9fff]", line) for line in fence_lines): |
| 49 | translated = article_gen.translate_text("\n".join(fence_lines)) |
| 50 | out.extend(translated.splitlines()) |
| 51 | else: |
| 52 | out.extend(fence_lines) |
| 53 | fence_lines = [] |
| 54 | |
| 55 | for line in lines: |
| 56 | fence_match = re.match(r"^(```|~~~)\s*([A-Za-z0-9_-]*)", line) |
| 57 | if fence_match: |
| 58 | if in_fence: |
| 59 | flush_fence() |
| 60 | out.append(line) |
| 61 | in_fence = False |
| 62 | fence_lang = "" |
| 63 | else: |
| 64 | out.append(line) |
| 65 | in_fence = True |
| 66 | fence_lang = fence_match.group(2).lower() |
| 67 | continue |
| 68 | |
| 69 | if in_fence: |
| 70 | fence_lines.append(line) |
| 71 | continue |
| 72 | |
| 73 | if line in TITLE_OVERRIDES: |
| 74 | out.append(TITLE_OVERRIDES[line]) |
| 75 | else: |
| 76 | out.append(article_gen.translate_line(line)) |
| 77 | |
| 78 | if in_fence: |
| 79 | flush_fence() |
| 80 | |
| 81 | text = "\n".join(out).rstrip() + "\n" |
| 82 | return fix_links(text, target_path, article_map) |
| 83 | |
| 84 | |
| 85 | def fix_links(text: str, target_path: Path, article_map: dict[str, dict[str, str]]) -> str: |
no test coverage detected