1
0

generate_pdfs.py 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import os
  2. import glob
  3. from markdown_pdf import MarkdownPdf
  4. from markdown_pdf import Section
  5. def main():
  6. docs_dir = os.path.join(os.path.dirname(__file__), '..', 'docs')
  7. md_files = glob.glob(os.path.join(docs_dir, '*.md'))
  8. if not md_files:
  9. print("No markdown files found in docs/")
  10. return
  11. for md_file in md_files:
  12. pdf_file = md_file.replace('.md', '.pdf')
  13. print(f"Converting {os.path.basename(md_file)} to PDF...")
  14. with open(md_file, 'r', encoding='utf-8') as f:
  15. md_content = f.read()
  16. import subprocess
  17. try:
  18. log_info = subprocess.check_output(['git', 'log', '-1', '--format=%H %an %ae %ad %cn %ce %cd %N %s', '--date=format:%Y/%m/%d %H:%M:%S'], encoding='utf-8').strip()
  19. try:
  20. tag_info = subprocess.check_output(['git', 'describe', '--tags', '--always'], stderr=subprocess.DEVNULL, encoding='utf-8').strip()
  21. except Exception:
  22. tag_info = ""
  23. if tag_info:
  24. git_id = f"$Id$"
  25. else:
  26. git_id = f"$Id$"
  27. except Exception:
  28. git_id = "$Id$"
  29. md_content = md_content.replace('$Id$', git_id)
  30. try:
  31. pdf = MarkdownPdf(toc_level=2)
  32. base_name = os.path.basename(md_file)
  33. if base_name == 'project_report.md':
  34. print("Splitting project_report.md into Portrait/Landscape/Portrait sections...")
  35. parts = md_content.split('## 2. Project File Catalog & Documentation')
  36. if len(parts) == 2:
  37. portrait_part1 = parts[0]
  38. remaining = parts[1]
  39. parts2 = remaining.split('## 3. Directory Structure Map')
  40. if len(parts2) == 2:
  41. landscape_part = '## 2. Project File Catalog & Documentation' + parts2[0]
  42. portrait_part2 = '## 3. Directory Structure Map' + parts2[1]
  43. pdf.add_section(Section(portrait_part1, paper_size="A4"))
  44. pdf.add_section(Section(landscape_part, paper_size="A4-L"))
  45. pdf.add_section(Section(portrait_part2, paper_size="A4"))
  46. else:
  47. print("WARNING: Could not find Directory Structure Map heading. Defaulting to full portrait.")
  48. pdf.add_section(Section(md_content, paper_size="A4"))
  49. else:
  50. print("WARNING: Could not find Project File Catalog heading. Defaulting to full portrait.")
  51. pdf.add_section(Section(md_content, paper_size="A4"))
  52. else:
  53. pdf.add_section(Section(md_content, paper_size="A4"))
  54. pdf.save(pdf_file)
  55. print(f"Saved {os.path.basename(pdf_file)}")
  56. # Copy to workspace root if applicable
  57. import shutil
  58. root_dir = os.path.join(os.path.dirname(__file__), '..')
  59. if base_name == 'project_report.md':
  60. dest = os.path.join(root_dir, 'Project.pdf')
  61. try:
  62. if os.path.exists(dest):
  63. os.remove(dest)
  64. shutil.copy2(pdf_file, dest)
  65. print("Successfully updated root Project.pdf")
  66. except Exception as copy_err:
  67. print(f"WARNING: Could not copy to root Project.pdf: {copy_err}")
  68. elif base_name == 'retro_planning.md':
  69. dest = os.path.join(root_dir, 'Retro Planning.pdf')
  70. try:
  71. if os.path.exists(dest):
  72. os.remove(dest)
  73. shutil.copy2(pdf_file, dest)
  74. print("Successfully updated root Retro Planning.pdf")
  75. except Exception as copy_err:
  76. print(f"WARNING: Could not copy to root Retro Planning.pdf: {copy_err}")
  77. except Exception as e:
  78. print(f"WARNING: Could not save {os.path.basename(pdf_file)}. File might be locked or open in a viewer. Error: {e}")
  79. if __name__ == "__main__":
  80. main()