adityarathod/pyandoc

error with python 3 str

Opened this issue · 1 comments

GNHua commented

I am using python 3.5 to run the example code

>>> import pandoc
>>> doc = pandoc.Document()
>>> doc.markdown = '''
... # I am an H1 Tag
... 
... * bullet point
... * more points
... * point with [link](http://google.com)
... '''
>>> print(doc.rst)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/haocan/Developer/Python/projectforum/venv/lib/python3.5/site-packages/pandoc/core.py", line 78, in <lambda>
    (lambda x, fmt=fmt: cls._output(x, fmt)), # fget
  File "/Users/haocan/Developer/Python/projectforum/venv/lib/python3.5/site-packages/pandoc/core.py", line 97, in _output
    return p.communicate(self._content)[0]
  File "/Users/haocan/anaconda/lib/python3.5/subprocess.py", line 801, in communicate
    stdout, stderr = self._communicate(input, endtime, timeout)
  File "/Users/haocan/anaconda/lib/python3.5/subprocess.py", line 1431, in _communicate
    input_view = memoryview(self._input)
TypeError: memoryview: a bytes-like object is required, not 'str'

There is an error because it's expecting a bytes encoded string, most likely since this library has not been updated for years. To make it work you have to set doc.markdown to the string and then append .encode('utf-8') and finally print the result by appending .decode() to doc.rst, or whatever format you want to convert to.

Here's how the example code would look with those modifications in place:

doc = pandoc.Document()

doc.markdown = '''
# I am an H1 Tag

* bullet point
* more points
* point with [link](http://kennethreitz.com)!
'''.encode('utf-8')

print(doc.rst.decode())

However, I would recommend using a more recent pandoc library if you can.