markdown_convert 1.2.15__py3-none-any.whl → 1.2.17__py3-none-any.whl

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,4 +1,4 @@
1
- """
2
- Empty file to make the folder a package.
3
- Author: @julynx
4
- """
1
+ """
2
+ Empty file to make the folder a package.
3
+ Author: @julynx
4
+ """
@@ -1,26 +1,23 @@
1
- """
2
- This module contains the constants used in the markdown_convert package.
3
- Author: @julynx
4
- """
5
-
6
- RED = '31'
7
- GREEN = '32'
8
- YELLOW = '33'
9
- BLUE = '34'
10
- MAGENTA = '35'
11
- CYAN = '36'
12
-
13
- OPTIONS = ('markdown_file_path',
14
- "--mode",
15
- '--css',
16
- "--out",
17
- "-h", "--help")
18
-
19
- OPTIONS_MODES = ('once', 'live')
20
-
21
- MD_EXTENSIONS = {
22
- "fenced-code-blocks": None,
23
- "header-ids": None,
24
- "breaks": {"on_newline": True},
25
- "tables": None
26
- }
1
+ """
2
+ This module contains the constants used in the markdown_convert package.
3
+ Author: @julynx
4
+ """
5
+
6
+ RED = "31"
7
+ GREEN = "32"
8
+ YELLOW = "33"
9
+ BLUE = "34"
10
+ MAGENTA = "35"
11
+ CYAN = "36"
12
+
13
+ OPTIONS = ("markdown_file_path", "--mode", "--css", "--out", "-h", "--help")
14
+
15
+ OPTIONS_MODES = ("once", "live", "debug")
16
+
17
+ MD_EXTENSIONS = {
18
+ "fenced-code-blocks": None,
19
+ "header-ids": None,
20
+ "breaks": {"on_newline": True},
21
+ "tables": None,
22
+ "latex": True,
23
+ }
@@ -1,247 +1,251 @@
1
- """
2
- Module to convert a markdown file to a pdf file.
3
- Author: @julynx
4
- """
5
-
6
- import os
7
- import sys
8
- import time
9
- import warnings
10
- from contextlib import redirect_stderr, redirect_stdout
11
- from datetime import datetime
12
- from io import StringIO
13
- from pathlib import Path
14
-
15
- import markdown2
16
- import weasyprint
17
-
18
- from .resources import get_css_path, get_code_css_path, get_output_path
19
- from .utils import drop_duplicates
20
- from .constants import MD_EXTENSIONS
21
-
22
-
23
- def _suppress_warnings():
24
- """
25
- Suppress all warnings in production while preserving critical error handling.
26
- Only errors and exceptions will be shown.
27
- """
28
- # Suppress all warnings but keep errors
29
- warnings.filterwarnings('ignore', category=UserWarning)
30
- warnings.filterwarnings('ignore', category=DeprecationWarning)
31
- warnings.filterwarnings('ignore', category=FutureWarning)
32
- warnings.filterwarnings('ignore', category=PendingDeprecationWarning)
33
- warnings.filterwarnings('ignore', category=ImportWarning)
34
- warnings.filterwarnings('ignore', category=ResourceWarning)
35
-
36
-
37
- def _silent_pdf_generation(func, *args, **kwargs):
38
- """
39
- Execute PDF generation function while suppressing all non-critical output.
40
- Preserves exceptions and critical errors.
41
- """
42
- _suppress_warnings()
43
-
44
- # Capture stdout and stderr to filter out warnings
45
- stdout_capture = StringIO()
46
- stderr_capture = StringIO()
47
-
48
- try:
49
- with redirect_stdout(stdout_capture), redirect_stderr(stderr_capture):
50
- result = func(*args, **kwargs)
51
-
52
- # Check if there were any critical errors in stderr
53
- stderr_content = stderr_capture.getvalue()
54
- if stderr_content and any(keyword in stderr_content.lower()
55
- for keyword in ['error', 'exception', 'traceback', 'failed']):
56
- # Print only critical errors, not warnings
57
- print(stderr_content, file=sys.stderr)
58
-
59
- return result
60
-
61
- except Exception as exc:
62
- # Always re-raise actual exceptions
63
- raise exc
64
-
65
-
66
- def convert(md_path, css_path=None, output_path=None,
67
- *, extend_default_css=True):
68
- """
69
- Convert a markdown file to a pdf file.
70
-
71
- Args:
72
- md_path (str): Path to the markdown file.
73
- css_path (str=None): Path to the CSS file.
74
- output_path (str=None): Path to the output file.
75
- extend_default_css (bool=True): Extend the default CSS file.
76
- """
77
- if css_path is None:
78
- css_path = get_css_path()
79
-
80
- if output_path is None:
81
- output_path = get_output_path(md_path, None)
82
-
83
- if extend_default_css:
84
- css_sources = [get_code_css_path(), get_css_path(), css_path]
85
- else:
86
- css_sources = [get_code_css_path(), css_path]
87
-
88
- css_sources = drop_duplicates(css_sources)
89
-
90
- try:
91
- html = markdown2.markdown_path(md_path,
92
- extras=MD_EXTENSIONS)
93
-
94
- # Use silent PDF generation to suppress warnings
95
- _silent_pdf_generation(
96
- lambda: weasyprint
97
- .HTML(string=html, base_url='.')
98
- .write_pdf(target=output_path,
99
- stylesheets=list(css_sources))
100
- )
101
-
102
- except Exception as exc:
103
- raise RuntimeError(exc) from exc
104
-
105
-
106
- def live_convert(md_path, css_path=None, output_path=None,
107
- *, extend_default_css=True):
108
- """
109
- Convert a markdown file to a pdf file and watch for changes.
110
-
111
- Args:
112
- md_path (str): Path to the markdown file.
113
- css_path (str=None): Path to the CSS file.
114
- output_path (str=None): Path to the output file.
115
- extend_default_css (bool=True): Extend the default CSS file.
116
- """
117
- if css_path is None:
118
- css_path = get_css_path()
119
-
120
- if output_path is None:
121
- output_path = get_output_path(md_path, None)
122
-
123
- live_converter = LiveConverter(md_path, css_path, output_path,
124
- extend_default_css=extend_default_css,
125
- loud=True)
126
- live_converter.observe()
127
-
128
-
129
- def convert_text(md_text, css_text=None,
130
- *, extend_default_css=True):
131
- """
132
- Convert markdown text to a pdf file.
133
-
134
- Args:
135
- md_text (str): Markdown text.
136
- css_text (str=None): CSS text.
137
- extend_default_css (bool=True): Extend the default CSS file.
138
-
139
- Returns:
140
- PDF file as bytes.
141
- """
142
- default_css = Path(get_css_path()).read_text(encoding='utf-8')
143
- code_css = Path(get_code_css_path()).read_text(encoding='utf-8')
144
-
145
- if css_text is None:
146
- css_text = default_css
147
-
148
- if extend_default_css:
149
- css_sources = [code_css, default_css, css_text]
150
- else:
151
- css_sources = [code_css, css_text]
152
-
153
- css_sources = [weasyprint.CSS(string=css)
154
- for css in drop_duplicates(css_sources)]
155
-
156
- try:
157
- html = markdown2.markdown(md_text,
158
- extras=MD_EXTENSIONS)
159
-
160
- # Use silent PDF generation to suppress warnings
161
- return _silent_pdf_generation(
162
- lambda: weasyprint
163
- .HTML(string=html, base_url='.')
164
- .write_pdf(stylesheets=css_sources)
165
- )
166
-
167
- except Exception as exc:
168
- raise RuntimeError(exc) from exc
169
-
170
-
171
- class LiveConverter():
172
- """
173
- Class to convert a markdown file to a pdf file and watch for changes.
174
- """
175
-
176
- def __init__(self, md_path, css_path, output_path,
177
- *, extend_default_css=True,
178
- loud=False):
179
- """
180
- Initialize the LiveConverter class.
181
-
182
- Args:
183
- md_path (str): Path to the markdown file.
184
- css_path (str): Path to the CSS file.
185
- output_path (str): Path to the output file.
186
- extend_default_css (bool): Extend the default CSS file.
187
- """
188
- self.md_path = Path(md_path).absolute()
189
- self.css_path = Path(css_path).absolute()
190
- self.output_path = output_path
191
- self.extend_default_css = extend_default_css
192
- self.loud = loud
193
-
194
- self.md_last_modified = None
195
- self.css_last_modified = None
196
-
197
- def get_last_modified_date(self, file_path):
198
- """
199
- Get the last modified date of a file.
200
-
201
- Args:
202
- file_path (str): Path to the file.
203
-
204
- Returns:
205
- Last modified date of the file.
206
- """
207
- return os.path.getmtime(file_path)
208
-
209
- def write_pdf(self):
210
- """
211
- Write the pdf file.
212
- """
213
- convert(self.md_path, self.css_path, self.output_path,
214
- extend_default_css=self.extend_default_css)
215
- if self.loud:
216
- print(f"- PDF file updated: {datetime.now()}", flush=True)
217
-
218
- def observe(self, poll_interval=1):
219
- """
220
- Observe the markdown and CSS files. Calls write_pdf() when a file is
221
- modified.
222
- """
223
- self.write_pdf()
224
-
225
- self.md_last_modified = self.get_last_modified_date(self.md_path)
226
- self.css_last_modified = self.get_last_modified_date(self.css_path)
227
-
228
- try:
229
- while True:
230
-
231
- md_modified = self.get_last_modified_date(self.md_path)
232
- css_modified = self.get_last_modified_date(self.css_path)
233
-
234
- if md_modified != self.md_last_modified or \
235
- css_modified != self.css_last_modified:
236
-
237
- self.write_pdf()
238
-
239
- self.md_last_modified = md_modified
240
- self.css_last_modified = css_modified
241
-
242
- time.sleep(poll_interval)
243
-
244
- except KeyboardInterrupt:
245
- if self.loud:
246
- print("\nInterrupted by user.\n", flush=True)
247
- return
1
+ """
2
+ Module to convert a markdown file to a pdf file.
3
+ Author: @julynx
4
+ """
5
+
6
+ import os
7
+ import time
8
+ from datetime import datetime
9
+ from pathlib import Path
10
+
11
+ import markdown2
12
+ from playwright.sync_api import sync_playwright
13
+
14
+ from .resources import get_css_path, get_code_css_path, get_output_path
15
+ from .utils import drop_duplicates
16
+ from .constants import MD_EXTENSIONS
17
+
18
+
19
+ def _generate_pdf_with_playwright(html_content, output_path):
20
+ """
21
+ Generate a PDF from HTML content using Playwright.
22
+ """
23
+ with sync_playwright() as p:
24
+ browser = p.chromium.launch(headless=True)
25
+ page = browser.new_page()
26
+ page.set_content(html_content)
27
+ # Wait for any potential resources to load
28
+ page.wait_for_load_state("networkidle")
29
+
30
+ pdf_params = {
31
+ "format": "A4",
32
+ "print_background": True,
33
+ "margin": {
34
+ "top": "20mm",
35
+ "bottom": "20mm",
36
+ "left": "20mm",
37
+ "right": "20mm",
38
+ },
39
+ }
40
+
41
+ if output_path:
42
+ page.pdf(path=output_path, **pdf_params)
43
+ browser.close()
44
+ return None
45
+
46
+ pdf_bytes = page.pdf(**pdf_params)
47
+ browser.close()
48
+ return pdf_bytes
49
+
50
+
51
+ def _embed_css_in_html(html, css_sources):
52
+ """
53
+ Embed CSS styles into HTML content.
54
+
55
+ Args:
56
+ html (str): HTML content.
57
+ css_sources (list): List of CSS file paths.
58
+
59
+ Returns:
60
+ HTML content with embedded CSS styles.
61
+ """
62
+ css_buffer = ""
63
+ for css_file in css_sources:
64
+ css_buffer += Path(css_file).read_text(encoding="utf-8") + "\n"
65
+
66
+ style_tag = f"<style>\n{css_buffer}\n</style>\n"
67
+ return f"<!DOCTYPE html>\n<html>\n<head>\n{style_tag}</head>\n<body>\n{html}\n</body>\n</html>"
68
+
69
+
70
+ def convert(
71
+ md_path,
72
+ css_path=None,
73
+ output_path=None,
74
+ *,
75
+ extend_default_css=True,
76
+ dump_html=False,
77
+ ):
78
+ """
79
+ Convert a markdown file to a pdf file.
80
+
81
+ Args:
82
+ md_path (str): Path to the markdown file.
83
+ css_path (str=None): Path to the CSS file.
84
+ output_path (str=None): Path to the output file.
85
+ extend_default_css (bool=True): Extend the default CSS file.
86
+ dump_html (bool=False): Dump the intermediate HTML to a file.
87
+ """
88
+ if css_path is None:
89
+ css_path = get_css_path()
90
+
91
+ if output_path is None:
92
+ output_path = get_output_path(md_path, None)
93
+
94
+ if extend_default_css:
95
+ css_sources = [get_code_css_path(), get_css_path(), css_path]
96
+ else:
97
+ css_sources = [get_code_css_path(), css_path]
98
+
99
+ css_sources = drop_duplicates(css_sources)
100
+
101
+ try:
102
+ html = markdown2.markdown_path(md_path, extras=MD_EXTENSIONS)
103
+ html = _embed_css_in_html(html, css_sources)
104
+
105
+ if dump_html:
106
+ html_dump_path = Path(output_path).with_suffix(".html")
107
+ html_dump_path.write_text(html, encoding="utf-8")
108
+
109
+ _generate_pdf_with_playwright(html, output_path)
110
+
111
+ except Exception as exc:
112
+ raise RuntimeError(exc) from exc
113
+
114
+
115
+ def live_convert(md_path, css_path=None, output_path=None, *, extend_default_css=True):
116
+ """
117
+ Convert a markdown file to a pdf file and watch for changes.
118
+
119
+ Args:
120
+ md_path (str): Path to the markdown file.
121
+ css_path (str=None): Path to the CSS file.
122
+ output_path (str=None): Path to the output file.
123
+ extend_default_css (bool=True): Extend the default CSS file.
124
+ """
125
+ if css_path is None:
126
+ css_path = get_css_path()
127
+
128
+ if output_path is None:
129
+ output_path = get_output_path(md_path, None)
130
+
131
+ live_converter = LiveConverter(
132
+ md_path, css_path, output_path, extend_default_css=extend_default_css, loud=True
133
+ )
134
+ live_converter.observe()
135
+
136
+
137
+ def convert_text(md_text, css_text=None, *, extend_default_css=True):
138
+ """
139
+ Convert markdown text to a pdf file.
140
+
141
+ Args:
142
+ md_text (str): Markdown text.
143
+ css_text (str=None): CSS text.
144
+ extend_default_css (bool=True): Extend the default CSS file.
145
+
146
+ Returns:
147
+ PDF file as bytes.
148
+ """
149
+ default_css = Path(get_css_path()).read_text(encoding="utf-8")
150
+ code_css = Path(get_code_css_path()).read_text(encoding="utf-8")
151
+
152
+ if css_text is None:
153
+ css_text = default_css
154
+
155
+ if extend_default_css:
156
+ css_sources = [code_css, default_css, css_text]
157
+ else:
158
+ css_sources = [code_css, css_text]
159
+
160
+ try:
161
+ html = markdown2.markdown(md_text, extras=MD_EXTENSIONS)
162
+ html = _embed_css_in_html(html, css_sources)
163
+
164
+ return _generate_pdf_with_playwright(html, None)
165
+
166
+ except Exception as exc:
167
+ raise RuntimeError(exc) from exc
168
+
169
+
170
+ class LiveConverter:
171
+ """
172
+ Class to convert a markdown file to a pdf file and watch for changes.
173
+ """
174
+
175
+ def __init__(
176
+ self, md_path, css_path, output_path, *, extend_default_css=True, loud=False
177
+ ):
178
+ """
179
+ Initialize the LiveConverter class.
180
+
181
+ Args:
182
+ md_path (str): Path to the markdown file.
183
+ css_path (str): Path to the CSS file.
184
+ output_path (str): Path to the output file.
185
+ extend_default_css (bool): Extend the default CSS file.
186
+ """
187
+ self.md_path = Path(md_path).absolute()
188
+ self.css_path = Path(css_path).absolute()
189
+ self.output_path = output_path
190
+ self.extend_default_css = extend_default_css
191
+ self.loud = loud
192
+
193
+ self.md_last_modified = None
194
+ self.css_last_modified = None
195
+
196
+ def get_last_modified_date(self, file_path):
197
+ """
198
+ Get the last modified date of a file.
199
+
200
+ Args:
201
+ file_path (str): Path to the file.
202
+
203
+ Returns:
204
+ Last modified date of the file.
205
+ """
206
+ return os.path.getmtime(file_path)
207
+
208
+ def write_pdf(self):
209
+ """
210
+ Write the pdf file.
211
+ """
212
+ convert(
213
+ self.md_path,
214
+ self.css_path,
215
+ self.output_path,
216
+ extend_default_css=self.extend_default_css,
217
+ )
218
+ if self.loud:
219
+ print(f"- PDF file updated: {datetime.now()}", flush=True)
220
+
221
+ def observe(self, poll_interval=1):
222
+ """
223
+ Observe the markdown and CSS files. Calls write_pdf() when a file is
224
+ modified.
225
+ """
226
+ self.write_pdf()
227
+
228
+ self.md_last_modified = self.get_last_modified_date(self.md_path)
229
+ self.css_last_modified = self.get_last_modified_date(self.css_path)
230
+
231
+ try:
232
+ while True:
233
+
234
+ md_modified = self.get_last_modified_date(self.md_path)
235
+ css_modified = self.get_last_modified_date(self.css_path)
236
+
237
+ if (
238
+ md_modified != self.md_last_modified
239
+ or css_modified != self.css_last_modified
240
+ ):
241
+
242
+ self.write_pdf()
243
+
244
+ self.md_last_modified = md_modified
245
+ self.css_last_modified = css_modified
246
+
247
+ time.sleep(poll_interval)
248
+
249
+ except KeyboardInterrupt:
250
+ if self.loud:
251
+ print("\nInterrupted by user.\n", flush=True)