generate_pdfs.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. color: #1a1a1a;
  34. background-color: #ffffff;
  35. }}
  36. h1, h2, h3, h4, h5, h6 {{
  37. color: #000000;
  38. }}
  39. code, pre {{
  40. font-family: 'RobotoMono', monospace;
  41. color: #b02a37;
  42. background-color: #f8f9fa;
  43. }}
  44. a {{
  45. color: #0d6efd;
  46. }}
  47. blockquote {{
  48. color: #555555;
  49. border-left: 4px solid #ccc;
  50. padding-left: 10px;
  51. }}
  52. """
  53. for md_file in md_files:
  54. pdf_file = md_file.replace('.md', '.pdf')
  55. print(f"Converting {os.path.basename(md_file)} to PDF...")
  56. with open(md_file, 'r', encoding='utf-8') as f:
  57. md_content = f.read()
  58. import subprocess
  59. try:
  60. 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()
  61. try:
  62. tag_info = subprocess.check_output(['git', 'describe', '--tags', '--always'], stderr=subprocess.DEVNULL, encoding='utf-8').strip()
  63. except Exception:
  64. tag_info = ""
  65. if tag_info:
  66. git_id = f"$Id$"
  67. else:
  68. git_id = f"$Id$"
  69. except Exception:
  70. git_id = "$Id$"
  71. md_content = md_content.replace('$Id$', git_id)
  72. try:
  73. pdf = MarkdownPdf(toc_level=2)
  74. base_name = os.path.basename(md_file)
  75. if base_name == 'project_report.md':
  76. print("Splitting project_report.md into Portrait/Landscape/Portrait sections...")
  77. parts = md_content.split('## 2. Project File Catalog & Documentation')
  78. if len(parts) == 2:
  79. portrait_part1 = parts[0]
  80. remaining = parts[1]
  81. parts2 = remaining.split('## 3. Directory Structure Map')
  82. if len(parts2) == 2:
  83. landscape_part = '## 2. Project File Catalog & Documentation' + parts2[0]
  84. portrait_part2 = '## 3. Directory Structure Map' + parts2[1]
  85. pdf.add_section(Section(portrait_part1, paper_size="A4"), user_css=user_css)
  86. pdf.add_section(Section(landscape_part, paper_size="A4-L"), user_css=user_css)
  87. pdf.add_section(Section(portrait_part2, paper_size="A4"), user_css=user_css)
  88. else:
  89. print("WARNING: Could not find Directory Structure Map heading. Defaulting to full portrait.")
  90. pdf.add_section(Section(md_content, paper_size="A4"), user_css=user_css)
  91. else:
  92. print("WARNING: Could not find Project File Catalog heading. Defaulting to full portrait.")
  93. pdf.add_section(Section(md_content, paper_size="A4"), user_css=user_css)
  94. else:
  95. pdf.add_section(Section(md_content, paper_size="A4"), user_css=user_css)
  96. pdf.save(pdf_file)
  97. print(f"Saved {os.path.basename(pdf_file)}")
  98. # Copy to workspace root if applicable
  99. import shutil
  100. root_dir = os.path.join(os.path.dirname(__file__), '..')
  101. if base_name == 'project_report.md':
  102. dest = os.path.join(root_dir, 'Project.pdf')
  103. try:
  104. if os.path.exists(dest):
  105. os.remove(dest)
  106. shutil.copy2(pdf_file, dest)
  107. print("Successfully updated root Project.pdf")
  108. except Exception as copy_err:
  109. print(f"WARNING: Could not copy to root Project.pdf: {copy_err}")
  110. elif base_name == 'retro_planning.md':
  111. dest = os.path.join(root_dir, 'Retro Planning.pdf')
  112. try:
  113. if os.path.exists(dest):
  114. os.remove(dest)
  115. shutil.copy2(pdf_file, dest)
  116. print("Successfully updated root Retro Planning.pdf")
  117. except Exception as copy_err:
  118. print(f"WARNING: Could not copy to root Retro Planning.pdf: {copy_err}")
  119. except Exception as e:
  120. print(f"WARNING: Could not save {os.path.basename(pdf_file)}. File might be locked or open in a viewer. Error: {e}")
  121. if __name__ == "__main__":
  122. main()