MCPcopy
hub / github.com/chinesehuazhou/python-weekly / write_summary_content

Function write_summary_content

resources/weekly_workflow.py:265–325  ·  view source on GitHub ↗

写入摘要文件的内容 :param f: 文件对象 :param content_meta: 元数据字典 :param md_body: 正文内容 :param weekly_no: 期号 :param source_file: 源文件路径,用于复制元数据 :param with_metadata: 是否写入元数据 :param is_english: 是否为英文版本

(f, content_meta, md_body, weekly_no, source_file=None, with_metadata=False, is_english=False)

Source from the content-addressed store, hash-verified

263 return yaml.safe_load(match.group(1))
264
265def write_summary_content(f, content_meta, md_body, weekly_no, source_file=None, with_metadata=False, is_english=False):
266 """
267 写入摘要文件的内容
268 :param f: 文件对象
269 :param content_meta: 元数据字典
270 :param md_body: 正文内容
271 :param weekly_no: 期号
272 :param source_file: 源文件路径,用于复制元数据
273 :param with_metadata: 是否写入元数据
274 :param is_english: 是否为英文版本
275 """
276 if with_metadata and source_file:
277 # 从源文件复制完整的元数据
278 with open(source_file, 'r', encoding='utf-8') as source:
279 content = source.read()
280 meta_match = re.search(r'---(.*?)---', content, re.DOTALL)
281 if meta_match:
282 f.write("---" + meta_match.group(1) + "---\n\n")
283 else:
284 # 只写入标题
285 f.write(f"# {content_meta['title']}\n\n")
286
287 # 根据语言版本选择对应的文本常量
288 if is_english:
289 intro = WEEKLY_INTRO_EN
290 subscription_info = SUBSCRIPTION_INFO_EN
291 season2_summary = SEASON2_SUMMARY_EN
292 free_collection = FREE_COLLECTION_EN
293 season1_summary = SEASON1_SUMMARY_EN
294 summary_text = "Here are the title summaries for this issue:"
295 full_text_info = f"After subscribing, you can view the full text of Issue {weekly_no} for free:"
296 else:
297 intro = WEEKLY_INTRO
298 subscription_info = SUBSCRIPTION_INFO
299 season2_summary = SEASON2_SUMMARY
300 free_collection = FREE_COLLECTION
301 season1_summary = SEASON1_SUMMARY
302 summary_text = "以下是本期标题摘要:"
303 full_text_info = f"订阅后,可免费查看 第 {weekly_no} 期周刊的全文:"
304
305 # 写入正文内容
306 f.write(intro + "\n\n")
307 f.write(f"{content_meta['description']}\n\n")
308 f.write(f"{summary_text} \n\n")
309
310 # 添加换行符,解决某些平台无法正确换行的问题
311 formatted_body = md_body
312 for i in range(1, 20):
313 formatted_body = formatted_body.replace(chr(9311 + i), "\n" + chr(9311 + i))
314 f.write(formatted_body + "\n\n")
315
316 # 写入订阅信息和其他固定内容
317 f.write(subscription_info + "\n\n")
318 f.write(f"{full_text_info} \n\n")
319 f.write(season2_summary + "\n\n")
320 f.write(free_collection + "\n\n")
321 f.write(season1_summary + "\n\n")
322

Callers 2

write_to_md_fileFunction · 0.85
write_to_md_file_enFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected