generate_pdfs.py 4.3 KB

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