Skip to content

Commit

Permalink
Fix validate_formats to accept Path object for pdf (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
kencx authored Aug 24, 2023
1 parent b9860a0 commit b99a74b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 10 additions & 2 deletions pypandoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,16 @@ def _validate_formats(format, to, outputfile):
if base_to_format == "pdf":
# pdf formats needs to actually have a to format of latex and a
# filename with an ending pf .pdf
if outputfile[-4:] != ".pdf":
raise RuntimeError('PDF output needs an outputfile with ".pdf" as a fileending.')
if isinstance(outputfile, str):
if outputfile[-4:] != ".pdf":
raise RuntimeError(
'PDF output needs an outputfile with ".pdf" as a fileending.'
)
elif isinstance(outputfile, Path):
if outputfile.suffix != ".pdf":
raise RuntimeError(
'PDF output needs an outputfile with ".pdf" as a fileending.'
)
# to is not allowed to contain pdf, but must point to latex
# it's also not allowed to contain extensions according to the docs
if to != base_to_format:
Expand Down
8 changes: 6 additions & 2 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,11 +565,15 @@ def f():

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

def path_filename():
pypandoc.convert_text('# some title\n', to='pdf', format='md', outputfile=Path(file_name))

with self.assertRaisesRegex(RuntimeError, 'PDF output needs an outputfile with ".pdf" as a fileending'):
f()
str_filename()
path_filename()

# no extensions allowed
with closed_tempfile('.pdf') as file_name:
Expand Down

0 comments on commit b99a74b

Please sign in to comment.