1
0

generate_pdfs.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #ident "@(#)$Format:LocalFoodAI_lanfr144:generate_pdfs.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. # Resolve dynamic absolute paths for downloaded TrueType fonts
  13. fonts_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'docs', 'fonts')).replace('\\', '/')
  14. regular_font = f"{fonts_dir}/Roboto-Regular.ttf"
  15. bold_font = f"{fonts_dir}/Roboto-Bold.ttf"
  16. mono_font = f"{fonts_dir}/RobotoMono-Regular.ttf"
  17. user_css = f"""
  18. @font-face {{
  19. font-family: 'Roboto';
  20. src: url('{regular_font}');
  21. }}
  22. @font-face {{
  23. font-family: 'Roboto';
  24. font-weight: bold;
  25. src: url('{bold_font}');
  26. }}
  27. @font-face {{
  28. font-family: 'RobotoMono';
  29. src: url('{mono_font}');
  30. }}
  31. body {{
  32. font-family: 'Roboto', sans-serif;
  33. }}
  34. code, pre {{
  35. font-family: 'RobotoMono', monospace;
  36. }}
  37. """
  38. for md_file in md_files:
  39. pdf_file = md_file.replace('.md', '.pdf')
  40. print(f"Converting {os.path.basename(md_file)} to PDF...")
  41. with open(md_file, 'r', encoding='utf-8') as f:
  42. md_content = f.read()
  43. import subprocess
  44. try:
  45. 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()
  46. try:
  47. tag_info = subprocess.check_output(['git', 'describe', '--tags', '--always'], stderr=subprocess.DEVNULL, encoding='utf-8').strip()
  48. except Exception:
  49. tag_info = ""
  50. if tag_info:
  51. git_id = f"$Id$"
  52. else:
  53. git_id = f"$Id$"
  54. except Exception:
  55. git_id = "$Id$"
  56. md_content = md_content.replace('$Id$', git_id)
  57. try:
  58. pdf = MarkdownPdf(toc_level=2)
  59. base_name = os.path.basename(md_file)
  60. if base_name == 'project_report.md':
  61. print("Splitting project_report.md into Portrait/Landscape/Portrait sections...")
  62. parts = md_content.split('## 2. Project File Catalog & Documentation')
  63. if len(parts) == 2:
  64. portrait_part1 = parts[0]
  65. remaining = parts[1]
  66. parts2 = remaining.split('## 3. Directory Structure Map')
  67. if len(parts2) == 2:
  68. landscape_part = '## 2. Project File Catalog & Documentation' + parts2[0]
  69. portrait_part2 = '## 3. Directory Structure Map' + parts2[1]
  70. pdf.add_section(Section(portrait_part1, paper_size="A4"), user_css=user_css)
  71. pdf.add_section(Section(landscape_part, paper_size="A4-L"), user_css=user_css)
  72. pdf.add_section(Section(portrait_part2, paper_size="A4"), user_css=user_css)
  73. else:
  74. print("WARNING: Could not find Directory Structure Map heading. Defaulting to full portrait.")
  75. pdf.add_section(Section(md_content, paper_size="A4"), user_css=user_css)
  76. else:
  77. print("WARNING: Could not find Project File Catalog heading. Defaulting to full portrait.")
  78. pdf.add_section(Section(md_content, paper_size="A4"), user_css=user_css)
  79. else:
  80. pdf.add_section(Section(md_content, paper_size="A4"), user_css=user_css)
  81. pdf.save(pdf_file)
  82. print(f"Saved {os.path.basename(pdf_file)}")
  83. # Copy to workspace root if applicable
  84. import shutil
  85. root_dir = os.path.join(os.path.dirname(__file__), '..')
  86. if base_name == 'project_report.md':
  87. dest = os.path.join(root_dir, 'Project.pdf')
  88. try:
  89. if os.path.exists(dest):
  90. os.remove(dest)
  91. shutil.copy2(pdf_file, dest)
  92. print("Successfully updated root Project.pdf")
  93. except Exception as copy_err:
  94. print(f"WARNING: Could not copy to root Project.pdf: {copy_err}")
  95. elif base_name == 'retro_planning.md':
  96. dest = os.path.join(root_dir, 'Retro Planning.pdf')
  97. try:
  98. if os.path.exists(dest):
  99. os.remove(dest)
  100. shutil.copy2(pdf_file, dest)
  101. print("Successfully updated root Retro Planning.pdf")
  102. except Exception as copy_err:
  103. print(f"WARNING: Could not copy to root Retro Planning.pdf: {copy_err}")
  104. except Exception as e:
  105. print(f"WARNING: Could not save {os.path.basename(pdf_file)}. File might be locked or open in a viewer. Error: {e}")
  106. if __name__ == "__main__":
  107. main()