Markdown 文档生成器
| 5 | |
| 6 | |
| 7 | class MarkdownFormatter: |
| 8 | """Markdown 文档生成器""" |
| 9 | |
| 10 | def __init__(self, category: str = "Development"): |
| 11 | self.category = category |
| 12 | |
| 13 | def format(self, jobs: list[dict]) -> str: |
| 14 | """生成 Markdown 文档""" |
| 15 | lines = [ |
| 16 | self._generate_header(jobs), |
| 17 | self._generate_stats(jobs), |
| 18 | self._generate_job_list(jobs), |
| 19 | ] |
| 20 | return "\n".join(lines) |
| 21 | |
| 22 | def _generate_header(self, jobs: list[dict]) -> str: |
| 23 | """生成文档头部""" |
| 24 | return f"""# Working Nomads {self.category} 职位列表 |
| 25 | |
| 26 | > 更新日期: {datetime.now().strftime("%Y-%m-%d %H:%M")} |
| 27 | > 职位数量: {len(jobs)} |
| 28 | > 数据来源: Working Nomads (https://www.workingnomads.com/jobs) |
| 29 | |
| 30 | --- |
| 31 | |
| 32 | """ |
| 33 | |
| 34 | def _generate_stats(self, jobs: list[dict]) -> str: |
| 35 | """生成统计信息""" |
| 36 | full_time = sum(1 for j in jobs if j["position_type"] == "Full-time") |
| 37 | part_time = sum(1 for j in jobs if j["position_type"] == "Part-time") |
| 38 | freelance = sum(1 for j in jobs if j["position_type"] == "Freelance") |
| 39 | with_salary = sum(1 for j in jobs if j["salary"] != "Not specified") |
| 40 | |
| 41 | return f"""## 📊 统计概览 |
| 42 | |
| 43 | | 指标 | 数量 | |
| 44 | |------|------| |
| 45 | | 全职职位 | {full_time} | |
| 46 | | 兼职职位 | {part_time} | |
| 47 | | 自由职业 | {freelance} | |
| 48 | | 有薪资信息 | {with_salary} | |
| 49 | |
| 50 | --- |
| 51 | |
| 52 | """ |
| 53 | |
| 54 | def _generate_job_list(self, jobs: list[dict]) -> str: |
| 55 | """生成职位列表""" |
| 56 | lines = ["## 💼 职位列表\n"] |
| 57 | |
| 58 | for idx, job in enumerate(jobs, 1): |
| 59 | lines.extend( |
| 60 | [ |
| 61 | f"### {idx}. {job['title']}", |
| 62 | "", |
| 63 | f"**🏢 公司:** {job['company']}", |
| 64 | f"**📍 地点:** {job['location']}", |