test_pre.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import os
  2. import re
  3. import fitz
  4. from markdown_pdf import MarkdownPdf, Section
  5. docs_dir = r"c:/Users/lanfr144/Documents/DOPRO1/Antigravity/Food/docs"
  6. md_file = os.path.join(docs_dir, "Installation_Guide.md")
  7. with open(md_file, "r", encoding="utf-8") as f:
  8. md_content = f.read()
  9. # Clean paths
  10. root_dir = r"c:/Users/lanfr144/Documents/DOPRO1/Antigravity/Food"
  11. md_content = md_content.replace("file:///" + root_dir + "/docs/", "")
  12. md_content = md_content.replace("file:///" + root_dir + "/", "../")
  13. # Try WITH <br/> replacement
  14. md_content_br = re.sub(r'(```[a-zA-Z0-9_-]*\n[\s\S]*?\n```)', r'\1\n<br/>', md_content)
  15. user_css = """
  16. * {
  17. color: #1a1a1a !important;
  18. }
  19. body {
  20. font-family: 'Helvetica', 'Arial', sans-serif !important;
  21. color: #1a1a1a !important;
  22. background-color: #ffffff !important;
  23. }
  24. h1, h2, h3, h4, h5, h6, h1 *, h2 *, h3 *, h4 *, h5 *, h6 * {
  25. color: #000000 !important;
  26. margin-top: 18px !important;
  27. margin-bottom: 8px !important;
  28. }
  29. pre {
  30. background-color: #f8f9fa !important;
  31. border: 1px solid #e9ecef !important;
  32. padding: 2px !important;
  33. margin-top: 8px !important;
  34. margin-bottom: 16px !important;
  35. border-radius: 3px !important;
  36. font-family: 'Courier New', 'Courier', monospace !important;
  37. font-size: 10pt !important;
  38. white-space: pre-wrap !important;
  39. word-break: break-all !important;
  40. }
  41. pre code, pre * {
  42. font-family: 'Courier New', 'Courier', monospace !important;
  43. font-size: 10pt !important;
  44. color: #212529 !important;
  45. background-color: #f8f9fa !important;
  46. white-space: pre-wrap !important;
  47. word-break: break-all !important;
  48. }
  49. code {
  50. font-family: 'Courier New', 'Courier', monospace !important;
  51. font-size: 10pt !important;
  52. color: #b02a37 !important;
  53. background-color: #f8f9fa !important;
  54. padding: 2px 4px !important;
  55. border-radius: 3px !important;
  56. white-space: pre-wrap !important;
  57. word-break: break-all !important;
  58. }
  59. a, a * {
  60. color: #0d6efd !important;
  61. }
  62. blockquote, blockquote * {
  63. color: #555555 !important;
  64. border-left: 4px solid #ccc !important;
  65. padding-left: 10px !important;
  66. }
  67. table, tr, td, th, table * {
  68. color: #1a1a1a !important;
  69. border-color: #cccccc !important;
  70. }
  71. th {
  72. background-color: #f2f2f2 !important;
  73. }
  74. ul, li {
  75. list-style-type: disc !important;
  76. }
  77. """
  78. print("--- TESTING WITH <br/> ---")
  79. pdf_br = MarkdownPdf()
  80. pdf_br.add_section(Section(md_content_br, paper_size="A4", root=docs_dir), user_css=user_css)
  81. doc_br = pdf_br._make_doc()
  82. print(f"Total pages: {len(doc_br)}")
  83. print(f"Page 2 blocks count: {len(doc_br[1].get_text('blocks'))}")
  84. doc_br.close()
  85. print("\n--- TESTING WITH \\n\\n ---")
  86. # Try with \n\n instead
  87. md_content_nn = re.sub(r'(```[a-zA-Z0-9_-]*\n[\s\S]*?\n```)', r'\1\n\n', md_content)
  88. pdf_nn = MarkdownPdf()
  89. pdf_nn.add_section(Section(md_content_nn, paper_size="A4", root=docs_dir), user_css=user_css)
  90. doc_nn = pdf_nn._make_doc()
  91. print(f"Total pages: {len(doc_nn)}")
  92. print(f"Page 2 blocks count: {len(doc_nn[1].get_text('blocks'))}")
  93. doc_nn.close()