import os import re vault_path = "vault" report_file = os.path.join(vault_path, "linking_audit_report.md") def apply_recommendations(): if not os.path.exists(report_file): print(f"Report file {report_file} not found. Run audit_links.py first.") return with open(report_file, "r", encoding="utf-8") as f: report_text = f.read() sec3_match = re.search(r"## 3\. Khuyến nghị Bổ sung Liên kết Nhân quả \(Linking Recommendations\)\n(.*)", report_text, re.DOTALL) if not sec3_match: print("Could not find Section 3 in the report.") return rec_lines = sec3_match.group(1).strip().split("\n") applied_count = 0 for line in rec_lines: if not line.strip().startswith("-"): continue match = re.search(r"-\s+\*\*`?\[\[([^\]]+)\]\]`?\*\*\s+(?:trong phần `?([^`]+)`?\s+)?.*?[kK]huyến nghị.*?\s+`?\[\[([^\]]+)\]\]`?", line) if match: source_file = match.group(1).strip() section_group = match.group(2).strip() if match.group(2) else None target_link = match.group(3).strip() file_path = os.path.join(vault_path, f"{source_file}.md") if not os.path.exists(file_path): print(f"Source file {file_path} does not exist. Skipping.") continue with open(file_path, "r", encoding="utf-8") as sf: content = sf.read() target_name = target_link.split("|")[0].strip() # Check if link already exists in file if re.search(r"\[\[" + re.escape(target_name) + r"(?:\|[^\]]+)?\]\]", content, re.IGNORECASE): print(f"Link to [[{target_name}]] already exists in {source_file}. Skipping.") continue if source_file == "open_research_questions" and section_group: # Handle special placement for open_research_questions Nhóm sections # We want to find the header for Nhóm X and append the link inside that section # Sections are separated by "---" # Let us locate the header: "## ... section_group ..." header_regex = r"(## [^\n]*" + re.escape(section_group) + r"[^\n]*)" header_match = re.search(header_regex, content) if header_match: header_text = header_match.group(1) pos = content.find(header_text) # find the next divider "---" or "## " to locate end of section next_div = content.find("\n---", pos) if next_div != -1: # insert right before the "---" divider new_content = content[:next_div].rstrip() + f"\n* **Tài liệu liên đới**: [[{target_link}]]\n" + content[next_div:] else: new_content = content.rstrip() + f"\n* **Tài liệu liên đới**: [[{target_link}]]\n" with open(file_path, "w", encoding="utf-8") as sf: sf.write(new_content) print(f"Successfully added [[{target_link}]] to open_research_questions.md under {section_group}") applied_count += 1 else: print(f"Could not find section header for {section_group} in open_research_questions.md. Skipping.") else: # General file links section append links_section_header = "## 🔗 Liên kết Tài liệu liên quan (Obsidian Links)" alternative_header = "## 🔗 Liên kết Tài liệu liên quan" if links_section_header in content: pos = content.find(links_section_header) next_section = content.find("\n## ", pos + len(links_section_header)) if next_section != -1: new_content = content[:next_section].rstrip() + f"\n* [[{target_link}]]\n\n" + content[next_section:] else: new_content = content.rstrip() + f"\n* [[{target_link}]]\n" elif alternative_header in content: pos = content.find(alternative_header) next_section = content.find("\n## ", pos + len(alternative_header)) if next_section != -1: new_content = content[:next_section].rstrip() + f"\n* [[{target_link}]]\n\n" + content[next_section:] else: new_content = content.rstrip() + f"\n* [[{target_link}]]\n" else: new_content = content.rstrip() + f"\n\n---\n\n{links_section_header}\n\n* [[{target_link}]]\n" with open(file_path, "w", encoding="utf-8") as sf: sf.write(new_content) print(f"Successfully added [[{target_link}]] to {source_file}.md") applied_count += 1 print(f"Applied {applied_count} linking recommendations.") if __name__ == "__main__": apply_recommendations()