miyuchina / mistletoe

A fast, extensible and spec-compliant Markdown parser in pure Python.
MIT License
811 stars 113 forks source link

TypeError: 'str' object is not an iterator, when using example in docs #137

Closed Lautron closed 2 years ago

Lautron commented 2 years ago

Error message

Traceback (most recent call last):
  File "/home/lautarob/Documents/code/Automation/md2anki/parser.py", line 19, in <module>
    parser()
  File "/home/lautarob/Documents/code/Automation/md2anki/parser.py", line 15, in parser
    doc = Document.read(md)
  File "/home/lautarob/Documents/code/Automation/md2anki/venv/lib/python3.10/site-packages/mistletoe/block_token.py", line 130, in read
    line_buffer = [next(lines)]
TypeError: 'str' object is not an iterator

Tried using Python3.6 and Python3.10, but got the same error in both versions.

Code that caused the error

from mistletoe import Document

def open_file(filename: str):
    with open(filename, 'r') as md_file:
        return md_file.read()

def parser():
    md = open_file('AYED2.md')
    doc = Document.read(md)
    print(doc)

if __name__ == "__main__":
    parser()

Example in Docs

From "Programatic Use" section on Getting Started

>> from mistletoe import Document
>> text = """
.. Here's some *text*
..
.. 1. a list
..
.. > a *quote*"""
>> doc = Document.read(text)
>> doc
Document(children=3, link_definitions=0, footnotes=0, footref_order=0, front_matter=None)

Final comment

I am willing to help with whatever I can and I hope I'm not wasting anyone's time.

pbodnar commented 2 years ago

Hi @Lautron, from your link Getting Started I can see you are probably using a fork of mistletoe: https://github.com/executablebooks/mistletoe-ebp? Or, maybe you have just opened a wrong documentation page for mistletoe?

Anyway, the error in your "Code that caused the error" seems to be hidden in the fact Document(), or Document.read() in your case, expects that you pass an iterator object like a file into it. But you pass a string instead - that's why you get the error. So try to replace this:

return md_file.read()

with this:

return md_file

I hope that helps. :)

Lautron commented 2 years ago

Hi @Lautron, from your link Getting Started I can see you are probably using a fork of mistletoe: https://github.com/executablebooks/mistletoe-ebp? Or, maybe you have just opened a wrong documentation page for mistletoe?

Anyway, the error in your "Code that caused the error" seems to be hidden in the fact Document(), or Document.read() in your case, expects that you pass an iterator object like a file into it. But you pass a string instead - that's why you get the error. So try to replace this:

return md_file.read()

with this:

return md_file

I hope that helps. :)

Ohh, I see, I made a silly mistake. It seems that I googled "mistletoe docs" and opened the first website that showed up without realising it was a fork.

Thanks for you help :)