Save a single post as a Markdown file.
(self, post: dict)
| 156 | return text.strip("-")[:80] |
| 157 | |
| 158 | def save_post(self, post: dict) -> Tuple[Optional[Path], Optional[dict]]: |
| 159 | """Save a single post as a Markdown file.""" |
| 160 | title_clean = self.clean_title(post.get("title", {}).get("rendered", "")) |
| 161 | slug = post.get("slug") or self.slugify(title_clean) |
| 162 | |
| 163 | content_html = post.get("content", {}).get("rendered", "") |
| 164 | content_md = self.html_to_markdown(content_html) |
| 165 | |
| 166 | if len(content_md) < MIN_CONTENT_LENGTH: |
| 167 | return None, None |
| 168 | |
| 169 | excerpt_html = post.get("excerpt", {}).get("rendered", "") |
| 170 | excerpt_md = self.html_to_markdown(excerpt_html) |
| 171 | excerpt_clean = re.sub(r"\n+", " ", excerpt_md).strip()[:200] |
| 172 | |
| 173 | date_raw = post.get("date", "")[:10] |
| 174 | year = date_raw[:4] if date_raw else "unknown" |
| 175 | |
| 176 | subcategory = self.detect_subcategory(post) |
| 177 | output_path = f"insights/{subcategory}" |
| 178 | |
| 179 | fm: dict = { |
| 180 | "id": f"insights-{slug}", |
| 181 | "title": {"zh": title_clean, "en": ""}, |
| 182 | "type": "insight", |
| 183 | "subcategory": subcategory, |
| 184 | "category": output_path, |
| 185 | "status": "active", |
| 186 | "published_date": date_raw, |
| 187 | "source_url": post.get("link", ""), |
| 188 | "source_format": "html", |
| 189 | "translation": "original", |
| 190 | "contributor": "RASAAS", |
| 191 | "migrated_from": "wordpress", |
| 192 | "wordpress_id": post.get("id"), |
| 193 | } |
| 194 | if excerpt_clean: |
| 195 | fm["excerpt"] = {"zh": excerpt_clean, "en": ""} |
| 196 | |
| 197 | fm_str = yaml.dump(fm, allow_unicode=True, default_flow_style=False, sort_keys=False) |
| 198 | |
| 199 | out_dir = self.output_dir / output_path |
| 200 | zh_file = out_dir / f"{slug}.zh.md" |
| 201 | |
| 202 | if not self.dry_run: |
| 203 | out_dir.mkdir(parents=True, exist_ok=True) |
| 204 | with open(zh_file, "w", encoding="utf-8") as f: |
| 205 | f.write(f"---\n{fm_str}---\n\n") |
| 206 | f.write(f"# {title_clean}\n\n") |
| 207 | if excerpt_clean: |
| 208 | f.write(f"> {excerpt_clean}\n\n") |
| 209 | f.write(content_md) |
| 210 | f.write("\n") |
| 211 | |
| 212 | return zh_file, fm |
| 213 | |
| 214 | def update_index(self, output_path: str, entries: List[dict]) -> None: |
| 215 | """Update or create _index.json for insights directory.""" |
no test coverage detected