Spaces:
Sleeping
Sleeping
| import hashlib | |
| import re | |
| from pathlib import Path | |
| from urllib.parse import urlparse | |
| def _slug_from_url(url: str) -> str: | |
| """ | |
| URLから安全なファイル名を生成。例: https://ja.empatheme.org/potion/108/ → potion_108.md | |
| """ | |
| p = urlparse(url) | |
| parts = [part for part in p.path.strip("/").split("/") if part] | |
| if len(parts) >= 2: | |
| base = f"{parts[-2]}_{parts[-1]}" | |
| elif parts: | |
| base = parts[-1] | |
| else: | |
| base = "index" | |
| # サニタイズ: ファイル名に使えない文字をアンダースコアに置換 | |
| base = re.sub(r'[^\w\-_.]', '_', base) | |
| return f"{base}.md" | |
| def ensure_dir(path: Path) -> None: | |
| path.mkdir(parents=True, exist_ok=True) | |
| def save_markdown(md_text: str, out_dir: Path, url: str) -> Path: | |
| ensure_dir(out_dir) | |
| filename = _slug_from_url(url) | |
| out_path = out_dir / filename | |
| out_path.write_text(md_text, encoding="utf-8") | |
| return out_path | |