Skip to content

Commit 2003f98

Browse files
author
Janusz Skonieczny
committed
Support for files encoded with utf-8
1 parent 8480648 commit 2003f98

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

‎pypandoc.py‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111

1212

1313

14-
def convert(source, to, format=None, extra_args=()):
14+
def convert(source, to, format=None, extra_args=(), encoding='utf-8'):
1515
'''Converts given `source` from `format` `to` another. `source` may be either a file path or a string to be converted. It's possible to pass `extra_args` if needed. In case `format` is not provided, it will try to invert the format based on given `source`.
1616
1717
Raises OSError if pandoc is not found! Make sure it has been installed and is available at path.
1818
'''
19-
return _convert(_read_file, _process_file, source, to, format, extra_args)
19+
return _convert(_read_file, _process_file, source, to, format, extra_args, encoding=encoding)
2020

21-
def _convert(reader, processor, source, to, format=None, extra_args=()):
22-
source, format = reader(source, format)
21+
def _convert(reader, processor, source, to, format=None, extra_args=(), encoding=None):
22+
source, format = reader(source, format, encoding=encoding)
2323

2424
formats = {
2525
'dbk': 'docbook',
@@ -44,9 +44,10 @@ def _convert(reader, processor, source, to, format=None, extra_args=()):
4444

4545
return processor(source, to, format, extra_args)
4646

47-
def _read_file(source, format):
47+
def _read_file(source, format, encoding='utf-8'):
4848
if os.path.exists(source):
49-
with open(source) as f:
49+
import codecs
50+
with codecs.open(source, encoding=encoding) as f:
5051
format = format or os.path.splitext(source)[1].strip('.')
5152
source = f.read()
5253

‎tests.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
def test_converter(to, format=None, extra_args=()):
99

10-
def reader(*args):
10+
def reader(*args, **kwargs):
1111
return source, format
1212

1313
def processor(*args):

0 commit comments

Comments
 (0)