Skip to content

Commit b99a74b

Browse files
authored
Fix validate_formats to accept Path object for pdf (JessicaTegner#338)
1 parent b9860a0 commit b99a74b

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

‎pypandoc/__init__.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,16 @@ def _validate_formats(format, to, outputfile):
303303
if base_to_format == "pdf":
304304
# pdf formats needs to actually have a to format of latex and a
305305
# filename with an ending pf .pdf
306-
if outputfile[-4:] != ".pdf":
307-
raise RuntimeError('PDF output needs an outputfile with ".pdf" as a fileending.')
306+
if isinstance(outputfile, str):
307+
if outputfile[-4:] != ".pdf":
308+
raise RuntimeError(
309+
'PDF output needs an outputfile with ".pdf" as a fileending.'
310+
)
311+
elif isinstance(outputfile, Path):
312+
if outputfile.suffix != ".pdf":
313+
raise RuntimeError(
314+
'PDF output needs an outputfile with ".pdf" as a fileending.'
315+
)
308316
# to is not allowed to contain pdf, but must point to latex
309317
# it's also not allowed to contain extensions according to the docs
310318
if to != base_to_format:

‎tests.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,11 +565,15 @@ def f():
565565

566566
# outputfile needs to end in pdf
567567
with closed_tempfile('.WRONG') as file_name:
568-
def f():
568+
def str_filename():
569569
pypandoc.convert_text('# some title\n', to='pdf', format='md', outputfile=file_name)
570570

571+
def path_filename():
572+
pypandoc.convert_text('# some title\n', to='pdf', format='md', outputfile=Path(file_name))
573+
571574
with self.assertRaisesRegex(RuntimeError, 'PDF output needs an outputfile with ".pdf" as a fileending'):
572-
f()
575+
str_filename()
576+
path_filename()
573577

574578
# no extensions allowed
575579
with closed_tempfile('.pdf') as file_name:

0 commit comments

Comments
 (0)