markdown_convert 1.2.15__tar.gz → 1.2.16__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.15
3
+ Version: 1.2.16
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>
@@ -13,8 +13,11 @@ from .modules.constants import RED, OPTIONS, OPTIONS_MODES
13
13
  from .modules.convert import convert, live_convert
14
14
  from .modules.resources import get_css_path, get_output_path, get_usage
15
15
  from .modules.utils import color
16
- from .modules.validate import (validate_css_path, validate_markdown_path,
17
- validate_output_path)
16
+ from .modules.validate import (
17
+ validate_css_path,
18
+ validate_markdown_path,
19
+ validate_output_path,
20
+ )
18
21
 
19
22
 
20
23
  def main():
@@ -32,11 +35,9 @@ def main():
32
35
  md_path = arg["markdown_file_path"]
33
36
  validate_markdown_path(md_path)
34
37
  except KeyError as key_err:
35
- raise IndexError("Missing 'markdown_file_path' argument.") \
36
- from key_err
38
+ raise IndexError("Missing 'markdown_file_path' argument.") from key_err
37
39
  except Exception as exc:
38
- raise IndexError(f"Invalid 'markdown_file_path' argument: {exc}") \
39
- from exc
40
+ raise IndexError(f"Invalid 'markdown_file_path' argument: {exc}") from exc
40
41
 
41
42
  # Get the mode
42
43
  try:
@@ -53,8 +54,7 @@ def main():
53
54
  except KeyError:
54
55
  css_path = get_css_path()
55
56
  except Exception as exc:
56
- raise IndexError(f"Invalid 'css_file_path' argument: {exc}") \
57
- from exc
57
+ raise IndexError(f"Invalid 'css_file_path' argument: {exc}") from exc
58
58
 
59
59
  # Get the output path
60
60
  output_path = None
@@ -65,11 +65,10 @@ def main():
65
65
  except KeyError:
66
66
  output_path = get_output_path(md_path, None)
67
67
  except Exception as exc:
68
- raise IndexError(f"Invalid 'output_path' argument: {exc}") \
69
- from exc
68
+ raise IndexError(f"Invalid 'output_path' argument: {exc}") from exc
70
69
 
71
70
  # Compile the markdown file
72
- print(f'\nGenerating PDF file from \'{md_path}\'...\n')
71
+ print(f"\nGenerating PDF file from '{md_path}'...\n")
73
72
  if mode == "once":
74
73
  convert(md_path, css_path, output_path)
75
74
  else:
@@ -77,11 +76,11 @@ def main():
77
76
 
78
77
  sys_exit(0)
79
78
 
79
+ # pylint: disable=W0718
80
80
  except Exception as err:
81
81
 
82
82
  asked_for_help = "--help" in arg or "-h" in arg
83
- show_usage = (isinstance(err, (IndexError, ValueError))
84
- or asked_for_help)
83
+ show_usage = isinstance(err, (IndexError, ValueError)) or asked_for_help
85
84
 
86
85
  if show_usage:
87
86
  print(get_usage())
@@ -92,5 +91,5 @@ def main():
92
91
  sys_exit(1)
93
92
 
94
93
 
95
- if __name__ == '__main__':
94
+ if __name__ == "__main__":
96
95
  main()
@@ -0,0 +1,22 @@
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")
16
+
17
+ MD_EXTENSIONS = {
18
+ "fenced-code-blocks": None,
19
+ "header-ids": None,
20
+ "breaks": {"on_newline": True},
21
+ "tables": None,
22
+ }
@@ -26,12 +26,12 @@ def _suppress_warnings():
26
26
  Only errors and exceptions will be shown.
27
27
  """
28
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)
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
35
 
36
36
 
37
37
  def _silent_pdf_generation(func, *args, **kwargs):
@@ -40,31 +40,32 @@ def _silent_pdf_generation(func, *args, **kwargs):
40
40
  Preserves exceptions and critical errors.
41
41
  """
42
42
  _suppress_warnings()
43
-
43
+
44
44
  # Capture stdout and stderr to filter out warnings
45
45
  stdout_capture = StringIO()
46
46
  stderr_capture = StringIO()
47
-
47
+
48
48
  try:
49
49
  with redirect_stdout(stdout_capture), redirect_stderr(stderr_capture):
50
50
  result = func(*args, **kwargs)
51
-
51
+
52
52
  # Check if there were any critical errors in stderr
53
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']):
54
+ if stderr_content and any(
55
+ keyword in stderr_content.lower()
56
+ for keyword in ["error", "exception", "traceback", "failed"]
57
+ ):
56
58
  # Print only critical errors, not warnings
57
59
  print(stderr_content, file=sys.stderr)
58
-
60
+
59
61
  return result
60
-
62
+
61
63
  except Exception as exc:
62
64
  # Always re-raise actual exceptions
63
65
  raise exc
64
66
 
65
67
 
66
- def convert(md_path, css_path=None, output_path=None,
67
- *, extend_default_css=True):
68
+ def convert(md_path, css_path=None, output_path=None, *, extend_default_css=True):
68
69
  """
69
70
  Convert a markdown file to a pdf file.
70
71
 
@@ -88,23 +89,20 @@ def convert(md_path, css_path=None, output_path=None,
88
89
  css_sources = drop_duplicates(css_sources)
89
90
 
90
91
  try:
91
- html = markdown2.markdown_path(md_path,
92
- extras=MD_EXTENSIONS)
92
+ html = markdown2.markdown_path(md_path, extras=MD_EXTENSIONS)
93
93
 
94
94
  # Use silent PDF generation to suppress warnings
95
95
  _silent_pdf_generation(
96
- lambda: weasyprint
97
- .HTML(string=html, base_url='.')
98
- .write_pdf(target=output_path,
99
- stylesheets=list(css_sources))
96
+ lambda: weasyprint.HTML(string=html, base_url=".").write_pdf(
97
+ target=output_path, stylesheets=list(css_sources)
98
+ )
100
99
  )
101
100
 
102
101
  except Exception as exc:
103
102
  raise RuntimeError(exc) from exc
104
103
 
105
104
 
106
- def live_convert(md_path, css_path=None, output_path=None,
107
- *, extend_default_css=True):
105
+ def live_convert(md_path, css_path=None, output_path=None, *, extend_default_css=True):
108
106
  """
109
107
  Convert a markdown file to a pdf file and watch for changes.
110
108
 
@@ -120,14 +118,13 @@ def live_convert(md_path, css_path=None, output_path=None,
120
118
  if output_path is None:
121
119
  output_path = get_output_path(md_path, None)
122
120
 
123
- live_converter = LiveConverter(md_path, css_path, output_path,
124
- extend_default_css=extend_default_css,
125
- loud=True)
121
+ live_converter = LiveConverter(
122
+ md_path, css_path, output_path, extend_default_css=extend_default_css, loud=True
123
+ )
126
124
  live_converter.observe()
127
125
 
128
126
 
129
- def convert_text(md_text, css_text=None,
130
- *, extend_default_css=True):
127
+ def convert_text(md_text, css_text=None, *, extend_default_css=True):
131
128
  """
132
129
  Convert markdown text to a pdf file.
133
130
 
@@ -139,8 +136,8 @@ def convert_text(md_text, css_text=None,
139
136
  Returns:
140
137
  PDF file as bytes.
141
138
  """
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')
139
+ default_css = Path(get_css_path()).read_text(encoding="utf-8")
140
+ code_css = Path(get_code_css_path()).read_text(encoding="utf-8")
144
141
 
145
142
  if css_text is None:
146
143
  css_text = default_css
@@ -150,32 +147,30 @@ def convert_text(md_text, css_text=None,
150
147
  else:
151
148
  css_sources = [code_css, css_text]
152
149
 
153
- css_sources = [weasyprint.CSS(string=css)
154
- for css in drop_duplicates(css_sources)]
150
+ css_sources = [weasyprint.CSS(string=css) for css in drop_duplicates(css_sources)]
155
151
 
156
152
  try:
157
- html = markdown2.markdown(md_text,
158
- extras=MD_EXTENSIONS)
153
+ html = markdown2.markdown(md_text, extras=MD_EXTENSIONS)
159
154
 
160
155
  # Use silent PDF generation to suppress warnings
161
156
  return _silent_pdf_generation(
162
- lambda: weasyprint
163
- .HTML(string=html, base_url='.')
164
- .write_pdf(stylesheets=css_sources)
157
+ lambda: weasyprint.HTML(string=html, base_url=".").write_pdf(
158
+ stylesheets=css_sources
159
+ )
165
160
  )
166
161
 
167
162
  except Exception as exc:
168
163
  raise RuntimeError(exc) from exc
169
164
 
170
165
 
171
- class LiveConverter():
166
+ class LiveConverter:
172
167
  """
173
168
  Class to convert a markdown file to a pdf file and watch for changes.
174
169
  """
175
170
 
176
- def __init__(self, md_path, css_path, output_path,
177
- *, extend_default_css=True,
178
- loud=False):
171
+ def __init__(
172
+ self, md_path, css_path, output_path, *, extend_default_css=True, loud=False
173
+ ):
179
174
  """
180
175
  Initialize the LiveConverter class.
181
176
 
@@ -210,8 +205,12 @@ class LiveConverter():
210
205
  """
211
206
  Write the pdf file.
212
207
  """
213
- convert(self.md_path, self.css_path, self.output_path,
214
- extend_default_css=self.extend_default_css)
208
+ convert(
209
+ self.md_path,
210
+ self.css_path,
211
+ self.output_path,
212
+ extend_default_css=self.extend_default_css,
213
+ )
215
214
  if self.loud:
216
215
  print(f"- PDF file updated: {datetime.now()}", flush=True)
217
216
 
@@ -231,8 +230,10 @@ class LiveConverter():
231
230
  md_modified = self.get_last_modified_date(self.md_path)
232
231
  css_modified = self.get_last_modified_date(self.css_path)
233
232
 
234
- if md_modified != self.md_last_modified or \
235
- css_modified != self.css_last_modified:
233
+ if (
234
+ md_modified != self.md_last_modified
235
+ or css_modified != self.css_last_modified
236
+ ):
236
237
 
237
238
  self.write_pdf()
238
239
 
@@ -244,4 +245,3 @@ class LiveConverter():
244
245
  except KeyboardInterrupt:
245
246
  if self.loud:
246
247
  print("\nInterrupted by user.\n", flush=True)
247
- return
@@ -48,8 +48,8 @@ def get_css_path():
48
48
  Returns:
49
49
  str: The path to the default CSS file.
50
50
  """
51
- package_files = files('markdown_convert')
52
- css_file = package_files / 'default.css'
51
+ package_files = files("markdown_convert")
52
+ css_file = package_files / "default.css"
53
53
  return str(css_file)
54
54
 
55
55
 
@@ -60,8 +60,8 @@ def get_code_css_path():
60
60
  Returns:
61
61
  str: The path to the code CSS file.
62
62
  """
63
- package_files = files('markdown_convert')
64
- css_file = package_files / 'code.css'
63
+ package_files = files("markdown_convert")
64
+ css_file = package_files / "code.css"
65
65
  return str(css_file)
66
66
 
67
67
 
@@ -72,21 +72,27 @@ def get_usage():
72
72
  Returns:
73
73
  str: The usage message.
74
74
  """
75
- commd = (f"{color(GREEN, 'markdown-convert')} "
76
- f"[{color(YELLOW, OPTIONS[0])}] [{color(BLUE, 'options')}]")
75
+ commd = (
76
+ f"{color(GREEN, 'markdown-convert')} "
77
+ f"[{color(YELLOW, OPTIONS[0])}] [{color(BLUE, 'options')}]"
78
+ )
77
79
  opt_1 = f"{color(BLUE, OPTIONS[1])}{color(CYAN, '=')}{color(CYAN, '|'.join(OPTIONS_MODES))}"
78
- opt_2 = f"{color(BLUE, OPTIONS[2])}{color(CYAN, '=')}[{color(CYAN, 'css_file_path')}]"
80
+ opt_2 = (
81
+ f"{color(BLUE, OPTIONS[2])}{color(CYAN, '=')}[{color(CYAN, 'css_file_path')}]"
82
+ )
79
83
  opt_3 = f"{color(BLUE, OPTIONS[3])}{color(CYAN, '=')}[{color(CYAN, 'output_file_path')}]"
80
84
 
81
- usage = ("\n"
82
- "Usage:\n"
83
- f" {commd}\n"
84
- "\n"
85
- "Options:\n"
86
- f" {opt_1}\n"
87
- " Convert the markdown file once (default) or live.\n"
88
- f" {opt_2}\n"
89
- " Use a custom CSS file.\n"
90
- f" {opt_3}\n"
91
- " Specify the output file path.\n")
85
+ usage = (
86
+ "\n"
87
+ "Usage:\n"
88
+ f" {commd}\n"
89
+ "\n"
90
+ "Options:\n"
91
+ f" {opt_1}\n"
92
+ " Convert the markdown file once (default) or live.\n"
93
+ f" {opt_2}\n"
94
+ " Use a custom CSS file.\n"
95
+ f" {opt_3}\n"
96
+ " Specify the output file path.\n"
97
+ )
92
98
  return usage
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "markdown_convert"
7
- version = "1.2.15"
7
+ version = "1.2.16"
8
8
  description = "Convert Markdown files to PDF from your command line."
9
9
  authors = [
10
10
  { name = "Julio Cabria", email = "juliocabria@tutanota.com" },
@@ -1,26 +0,0 @@
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
- }