22from __future__ import absolute_import , print_function , with_statement
33from typing import Iterable
44from typing import Union
5+ from typing import Generator
56
67import logging
78import os
1112import tempfile
1213import textwrap
1314import glob
15+ from pathlib import Path
1416
1517from .handler import _check_log_handler
1618from .pandoc_download import DEFAULT_TARGET_FOLDER , download_pandoc
5254logger = logging .getLogger (__name__ )
5355
5456def convert_text (source :str , to :str , format :str , extra_args :Iterable = (), encoding :str = 'utf-8' ,
55- outputfile :Union [None , str ]= None , filters :Union [Iterable , None ]= None , verify_format :bool = True ,
57+ outputfile :Union [None , str , Path ]= None , filters :Union [Iterable , None ]= None , verify_format :bool = True ,
5658 sandbox :bool = True , cworkdir :Union [str , None ]= None ) -> str :
5759 """Converts given `source` from `format` to `to`.
5860
@@ -68,8 +70,9 @@ def convert_text(source:str, to:str, format:str, extra_args:Iterable=(), encodin
6870
6971 :param str encoding: the encoding of the input bytes (Default value = 'utf-8')
7072
71- :param str outputfile: output will be written to outfilename or the converted content
72- returned if None (Default value = None)
73+ :param str outputfile: output will be written to outputfile or the converted content
74+ returned if None. The output filename can be specified as a string
75+ or pathlib.Path object. (Default value = None)
7376
7477 :param list filters: pandoc filters e.g. filters=['pandoc-citeproc']
7578
@@ -93,12 +96,17 @@ def convert_text(source:str, to:str, format:str, extra_args:Iterable=(), encodin
9396 cworkdir = cworkdir )
9497
9598
96- def convert_file (source_file :Union [list , str ], to :str , format :Union [str , None ]= None , extra_args :Iterable = (), encoding :str = 'utf-8' ,
97- outputfile :Union [None , str ]= None , filters :Union [Iterable , None ]= None , verify_format :bool = True ,
98- sandbox :bool = True , cworkdir :Union [str , None ]= None ) -> str :
99+ def convert_file (source_file :Union [list , str , Path , Generator ], to :str , format :Union [str , None ]= None ,
100+ extra_args :Iterable = (), encoding :str = 'utf-8' , outputfile :Union [None , str , Path ]= None ,
101+ filters :Union [Iterable , None ]= None , verify_format :bool = True , sandbox :bool = True ,
102+ cworkdir :Union [str , None ]= None ) -> str :
99103 """Converts given `source` from `format` to `to`.
100104
101- :param (str, list) source_file: Either a full file path, relative file path, a file patterh (like dir/*.md), or a list if file or file patterns.
105+ :param (str, list, pathlib.Path) source_file: If a string, should be either
106+ an absolute file path, relative file path, or a file pattern (like dir/*.md).
107+ If a list, should be a list of file paths, file patterns, or pathlib.Path
108+ objects. In addition, pathlib.Path objects as well as the generators produced by
109+ pathlib.Path.glob may be specified.
102110
103111 :param str to: format into which the input should be converted; can be one of
104112 `pypandoc.get_pandoc_formats()[1]`
@@ -112,8 +120,9 @@ def convert_file(source_file:Union[list, str], to:str, format:Union[str, None]=N
112120
113121 :param str encoding: the encoding of the file or the input bytes (Default value = 'utf-8')
114122
115- :param str outputfile: output will be written to outfilename or the converted content
116- returned if None (Default value = None)
123+ :param str outputfile: output will be written to outputfile or the converted content
124+ returned if None. The output filename can be specified as a string
125+ or pathlib.Path object. (Default value = None)
117126
118127 :param list filters: pandoc filters e.g. filters=['pandoc-citeproc']
119128
@@ -130,6 +139,14 @@ def convert_file(source_file:Union[list, str], to:str, format:Union[str, None]=N
130139 :raises OSError: if pandoc is not found; make sure it has been installed and is available at
131140 path.
132141 """
142+ # This if block effectively adds support for pathlib.Path objects
143+ # and generators produced by pathlib.Path().glob().
144+ if not isinstance (source_file , str ):
145+ try :
146+ source_file = list (map (str , source_file ))
147+ except TypeError :
148+ source_file = str (source_file )
149+
133150 if not _identify_path (source_file ):
134151 raise RuntimeError ("source_file is not a valid path" )
135152 if _is_network_path (source_file ): # if the source_file is an url
@@ -145,18 +162,15 @@ def convert_file(source_file:Union[list, str], to:str, format:Union[str, None]=N
145162 if isinstance (source_file , list ): # a list of possibly file or file patterns. Expand all with glob
146163 for filepath in source_file :
147164 discovered_source_files .extend (glob .glob (filepath ))
148- if len (discovered_source_files ) == 1 : # behavior for a single file or a pattern
149- format = _identify_format_from_path (discovered_source_files [0 ], format )
150- return _convert_input (discovered_source_files [0 ], format , 'path' , to , extra_args = extra_args ,
151- outputfile = outputfile , filters = filters ,
152- verify_format = verify_format , sandbox = sandbox ,
153- cworkdir = cworkdir )
154- else : # behavior for multiple files or file patterns
155- format = _identify_format_from_path (discovered_source_files [0 ], format )
156- return _convert_input (discovered_source_files , format , 'path' , to , extra_args = extra_args ,
157- outputfile = outputfile , filters = filters ,
158- verify_format = verify_format , sandbox = sandbox ,
159- cworkdir = cworkdir )
165+
166+ format = _identify_format_from_path (discovered_source_files [0 ], format )
167+ if len (discovered_source_files ) == 1 :
168+ discovered_source_files = discovered_source_files [0 ]
169+
170+ return _convert_input (discovered_source_files , format , 'path' , to , extra_args = extra_args ,
171+ outputfile = outputfile , filters = filters ,
172+ verify_format = verify_format , sandbox = sandbox ,
173+ cworkdir = cworkdir )
160174
161175
162176def _identify_path (source ) -> bool :
@@ -330,7 +344,7 @@ def _convert_input(source, format, input_type, to, extra_args=(),
330344 args += input_file
331345
332346 if outputfile :
333- args .append ("--output=" + outputfile )
347+ args .append ("--output=" + str ( outputfile ) )
334348
335349 if sandbox :
336350 if ensure_pandoc_minimal_version (2 ,15 ): # sandbox was introduced in pandoc 2.15, so only add if we are using 2.15 or above.
0 commit comments