markdown_convert 1.2.18__tar.gz → 1.2.20__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: markdown_convert
3
- Version: 1.2.18
3
+ Version: 1.2.20
4
4
  Summary: Convert Markdown files to PDF from your command line.
5
5
  Project-URL: homepage, https://github.com/Julynx/markdown_convert
6
6
  Author-email: Julio Cabria <juliocabria@tutanota.com>
@@ -9,7 +9,7 @@ License-File: LICENSE
9
9
  Classifier: License :: OSI Approved :: GNU General Public License v2 (GPLv2)
10
10
  Classifier: Operating System :: OS Independent
11
11
  Classifier: Programming Language :: Python :: 3
12
- Requires-Python: <3.14,>=3.9
12
+ Requires-Python: <3.15,>=3.9
13
13
  Requires-Dist: argsdict==1.0.0
14
14
  Requires-Dist: latex2mathml>=3.78.1
15
15
  Requires-Dist: markdown2<3,>=2.4.13
@@ -17,38 +17,55 @@ from .resources import get_code_css_path, get_css_path, get_output_path
17
17
  from .utils import drop_duplicates
18
18
 
19
19
 
20
- def _generate_pdf_with_playwright(html_content, output_path, *, css_content=None):
20
+ def _generate_pdf_with_playwright(
21
+ html_content,
22
+ output_path,
23
+ *,
24
+ css_content=None,
25
+ base_dir=None,
26
+ dump_html=False,
27
+ ):
21
28
  """
22
29
  Generate a PDF from HTML content using Playwright.
30
+
31
+ Args:
32
+ html_content (str): HTML content to convert.
33
+ output_path (str): Path to save the PDF file.
34
+ css_content (str, optional): CSS content to inject.
35
+ base_dir (Path, optional): Base directory for resolving relative paths in HTML.
36
+ dump_html (bool, optional): Whether to dump the HTML content to a file.
23
37
  """
24
38
  with sync_playwright() as p:
25
39
  browser = p.chromium.launch(headless=True)
26
40
  page = browser.new_page()
27
- page.set_content(html_content)
28
- if css_content:
29
- page.add_style_tag(content=css_content)
30
- # Wait for any potential resources to load
31
- page.wait_for_load_state("networkidle")
32
-
33
- pdf_params = {
34
- "format": "A4",
35
- "print_background": True,
36
- "margin": {
37
- "top": "20mm",
38
- "bottom": "20mm",
39
- "left": "20mm",
40
- "right": "20mm",
41
- },
42
- }
43
-
44
- if output_path:
45
- page.pdf(path=output_path, **pdf_params)
46
- browser.close()
47
- return None
48
41
 
49
- pdf_bytes = page.pdf(**pdf_params)
50
- browser.close()
51
- return pdf_bytes
42
+ # Handle loading based on presence of base_dir
43
+ temp_html = None
44
+ try:
45
+ if base_dir:
46
+ temp_html = base_dir / f".temp_{os.getpid()}.html"
47
+ temp_html.write_text(html_content, encoding="utf-8")
48
+ page.goto(temp_html.as_uri(), wait_until="networkidle")
49
+ else:
50
+ page.set_content(html_content, wait_until="networkidle")
51
+
52
+ if css_content:
53
+ page.add_style_tag(content=css_content)
54
+
55
+ pdf_params = {
56
+ "format": "A4",
57
+ "print_background": True,
58
+ "margin": {"top": "20mm", "bottom": "20mm", "left": "20mm", "right": "20mm"},
59
+ "path": output_path,
60
+ } # Playwright ignores None paths
61
+
62
+ pdf_bytes = page.pdf(**pdf_params)
63
+ return None if output_path else pdf_bytes
64
+
65
+ finally:
66
+ browser.close()
67
+ if temp_html and temp_html.exists() and not dump_html:
68
+ temp_html.unlink()
52
69
 
53
70
 
54
71
  def _get_css_content(css_sources):
@@ -75,7 +92,6 @@ def _create_sections(html):
75
92
  Returns:
76
93
  HTML content with sections wrapped in <section> tags.
77
94
  """
78
-
79
95
  pattern = re.compile(r"(<h2.*?>.*?</h2>)(.*?)(?=(<h2.*?>|$))", re.DOTALL)
80
96
 
81
97
  def wrap_section(match):
@@ -119,14 +135,12 @@ def convert(
119
135
  html = markdown2.markdown_path(md_path, extras=MD_EXTENSIONS)
120
136
  html = _create_sections(html)
121
137
 
122
- if dump_html:
123
- html_dump_path = Path(output_path).with_suffix(".html")
124
- html_dump_path.write_text(html, encoding="utf-8")
125
-
126
138
  _generate_pdf_with_playwright(
127
139
  html,
128
140
  output_path,
129
141
  css_content=_get_css_content(css_sources),
142
+ base_dir=Path(md_path).resolve().parent,
143
+ dump_html=dump_html,
130
144
  )
131
145
 
132
146
  except Exception as exc:
@@ -4,14 +4,14 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "markdown_convert"
7
- version = "1.2.18"
7
+ version = "1.2.20"
8
8
  description = "Convert Markdown files to PDF from your command line."
9
9
  authors = [
10
10
  { name = "Julio Cabria", email = "juliocabria@tutanota.com" },
11
11
  ]
12
12
  license = { text = "GPL-2.0-only" }
13
13
  readme = "README.md"
14
- requires-python = ">=3.9,<3.14"
14
+ requires-python = ">=3.9,<3.15"
15
15
  classifiers = [
16
16
  "Programming Language :: Python :: 3",
17
17
  "License :: OSI Approved :: GNU General Public License v2 (GPLv2)",