| 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\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") |