generate_pdfs.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 %ad %an %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. pdf.add_section(Section(md_content))
  33. pdf.save(pdf_file)
  34. print(f"Saved {os.path.basename(pdf_file)}")
  35. except Exception as e:
  36. print(f"WARNING: Could not save {os.path.basename(pdf_file)}. File might be locked or open in a viewer. Error: {e}")
  37. if __name__ == "__main__":
  38. main()