Skip to content

Commit 8bc3c51

Browse files
Added parameter to control if files are sorted before being passed to pandoc (#378)
* Added parameter to control if files are sorted before being passed to pandoc * fix: addressing params ordering comment in PR review --------- Co-authored-by: Jessica Tegner <jessica@jessicategner.com>
1 parent 50f3b08 commit 8bc3c51

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ Note that for citeproc tests to pass you'll need to have [pandoc-citeproc](https
312312
* [Alex Kneisel](https://github.com/hey-thanks/) - Added pathlib.Path support to convert_file.
313313
* [Juho Vepsäläinen](https://github.com/bebraw/) - Creator and former maintainer of pypandoc
314314
* [Connor](https://github.com/DisSupEng/) - Updated Dockerfile to Python 3.9 image and added docker compose file
315+
* [Colin Bull](https://github.com/colinbull) - Added ability to control whether files are sorted before being passed to pandoc process.
315316

316317
## License
317318

pypandoc/__init__.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ def convert_text(source:str, to:str, format:str, extra_args:Iterable=(), encodin
8181
:param bool sandbox: Run pandoc in pandocs own sandbox mode, limiting IO operations in readers and writers to reading the files specified on the command line. Anyone using pandoc on untrusted user input should use this option. Note: This only does something, on pandoc >= 2.15
8282
(Default value = False)
8383
84+
:param str cworkdir: set the current working directory (Default value = None)
85+
8486
:returns: converted string (unicode) or an empty string if an outputfile was given
8587
:rtype: unicode
8688
@@ -98,7 +100,7 @@ def convert_text(source:str, to:str, format:str, extra_args:Iterable=(), encodin
98100
def convert_file(source_file:Union[list, str, Path, Generator], to:str, format:Union[str, None]=None,
99101
extra_args:Iterable=(), encoding:str='utf-8', outputfile:Union[None, str, Path]=None,
100102
filters:Union[Iterable, None]=None, verify_format:bool=True, sandbox:bool=False,
101-
cworkdir:Union[str, None]=None) -> str:
103+
cworkdir:Union[str, None]=None, sort_files=True) -> str:
102104
"""Converts given `source` from `format` to `to`.
103105
104106
:param (str, list, pathlib.Path) source_file: If a string, should be either
@@ -131,6 +133,10 @@ def convert_file(source_file:Union[list, str, Path, Generator], to:str, format:U
131133
:param bool sandbox: Run pandoc in pandocs own sandbox mode, limiting IO operations in readers and writers to reading the files specified on the command line. Anyone using pandoc on untrusted user input should use this option. Note: This only does something, on pandoc >= 2.15
132134
(Default value = False)
133135
136+
:param str cworkdir: set the current working directory (Default value = None)
137+
138+
:param bool sort_files: causes the files to be sorted before being passed to pandoc (Default value = True)
139+
134140
:returns: converted string (unicode) or an empty string if an outputfile was given
135141
:rtype: unicode
136142
@@ -200,7 +206,7 @@ def convert_file(source_file:Union[list, str, Path, Generator], to:str, format:U
200206
return _convert_input(discovered_source_files, format, 'path', to, extra_args=extra_args,
201207
outputfile=outputfile, filters=filters,
202208
verify_format=verify_format, sandbox=sandbox,
203-
cworkdir=cworkdir)
209+
cworkdir=cworkdir, sort_files=sort_files)
204210

205211

206212
def _identify_path(source) -> bool:
@@ -356,7 +362,7 @@ def _validate_formats(format, to, outputfile):
356362

357363
def _convert_input(source, format, input_type, to, extra_args=(),
358364
outputfile=None, filters=None, verify_format=True,
359-
sandbox=False, cworkdir=None):
365+
sandbox=False, cworkdir=None, sort_files=True):
360366

361367
_check_log_handler()
362368

@@ -379,8 +385,10 @@ def _convert_input(source, format, input_type, to, extra_args=(),
379385
input_file = source
380386
else:
381387
input_file = []
382-
383-
input_file = sorted(input_file)
388+
389+
if sort_files:
390+
input_file = sorted(input_file)
391+
384392
args = [__pandoc_path, '--from=' + format]
385393

386394
args.append('--to=' + to)

tests.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ def capture(command, *args, **kwargs):
3131

3232

3333
@contextlib.contextmanager
34-
def closed_tempfile(suffix, text=None, dir_name=None):
34+
def closed_tempfile(suffix, text=None, dir_name=None, prefix=None):
3535
file_name = None
3636
try:
3737
if dir_name:
3838
dir_name = tempfile.mkdtemp(suffix=dir_name)
3939

40-
with tempfile.NamedTemporaryFile('w+t', suffix=suffix, delete=False, dir=dir_name) as test_file:
40+
with tempfile.NamedTemporaryFile('w+t', suffix=suffix, prefix=prefix, delete=False, dir=dir_name) as test_file:
4141
file_name = test_file.name
4242
if text:
4343
test_file.write(text)
@@ -201,6 +201,13 @@ def test_basic_conversion_from_multiple_files(self):
201201
received = pypandoc.convert_file([file_name1,file_name2], 'html')
202202
self.assertEqualExceptForNewlineEnd(expected, received)
203203

204+
def test_sorting_rules_applied_for_multiple_files(self):
205+
with closed_tempfile('.md', prefix='1_', text='some title 1') as file_name1:
206+
with closed_tempfile('.md', prefix='2_', text='some title 2') as file_name2:
207+
expected = '<p>some title 2</p>\n<p>some title 1</p>'
208+
received = pypandoc.convert_file([file_name2,file_name1], 'html', sort_files=False)
209+
self.assertEqualExceptForNewlineEnd(expected, received)
210+
204211
def test_basic_conversion_from_file_pattern(self):
205212
received = pypandoc.convert_file("./*.md", 'html')
206213
received = received.lower()

0 commit comments

Comments
 (0)