MCPcopy Create free account
hub / github.com/RASAAS/docmcp-knowledge / process_doc

Function process_doc

scripts/append_missing_images.py:27–66  ·  view source on GitHub ↗
(doc_id)

Source from the content-addressed store, hash-verified

25]
26
27def process_doc(doc_id):
28 # Get all extracted images for this doc_id
29 img_dir = REPO_ROOT / "docs" / "public" / "images" / "mdcg"
30 all_imgs = sorted([f.name for f in img_dir.glob(f"{doc_id}-fig*.png")])
31 if not all_imgs:
32 return
33
34 def append_to_file(filepath):
35 if not filepath.exists():
36 return
37 content = filepath.read_text(encoding="utf-8")
38
39 # Check which images are NOT in the file
40 missing_imgs = []
41 for img_name in all_imgs:
42 if img_name not in content:
43 missing_imgs.append(img_name)
44
45 if missing_imgs:
46 print(f"[{doc_id}] {filepath.name}: {len(missing_imgs)} missing images appended.")
47 # Append missing
48 append_text = "\n\n## Additional Figures (Extracted)\n\n"
49 for m in missing_imgs:
50 append_text += f"\n![](/images/mdcg/{m})\n"
51
52 # If <!-- fulltext-end --> exists, insert before it
53 if "<!-- fulltext-end -->" in content:
54 parts = content.split("<!-- fulltext-end -->", 1)
55 new_content = parts[0] + append_text + "\n<!-- fulltext-end -->" + parts[1]
56 else:
57 new_content = content + append_text
58
59 filepath.write_text(new_content, encoding="utf-8")
60
61 # target files
62 append_to_file(FULLTEXT_DIR / f"{doc_id}.md")
63 append_to_file(ZH_DIR / f"{doc_id}.md")
64 append_to_file(EN_DIR / f"{doc_id}.md")
65 append_to_file(REPO_ROOT / "eu_mdr" / "mdcg" / f"{doc_id}.zh.md")
66 append_to_file(REPO_ROOT / "eu_mdr" / "mdcg" / f"{doc_id}.en.md")
67
68for d in docs_with_images:
69 process_doc(d)

Callers 1

Calls 1

append_to_fileFunction · 0.85

Tested by

no test coverage detected