rolandshoemaker / CommonMark-py

Depreciated in favor of rtfd/CommonMark-py
Other
125 stars 12 forks source link

Can't print same AST to JSON twice in a row #14

Open abought opened 9 years ago

abought commented 9 years ago

Running CommonMark.ASTtoJSON(ast) on the same tree twice returns different results: the first time it works, but the second time throws an exception. See an example, below.

import CommonMark
parser = CommonMark.DocParser()
ast = parser.parse("Some random text\n\n### A heading\nSome more text")

The first time, it serializes correctly:

CommonMark.ASTtoJSON(ast)
'{"children": [{"strings": ["Some random text"], "last_line_blank": true, "tight": false, "inline_content": [{"c": "Some random text", "last_line_blank": false, "tight": false, "t": "Str", "open": true}], "end_line": 1, "t": "Paragraph", "start_line": 1, "open": false, "start_column": 1}, {"strings": ["A heading"], "last_line_blank": false, "tight": false, "inline_content": [{"c": "A heading", "last_line_blank": false, "tight": false, "t": "Str", "open": true}], "end_line": 3, "t": "ATXHeader", "start_line": 3, "open": false, "start_column": 1, "level": 3}, {"strings": ["Some more text"], "last_line_blank": false, "tight": false, "inline_content": [{"c": "Some more text", "last_line_blank": false, "tight": false, "t": "Str", "open": true}], "end_line": 3, "t": "Paragraph", "start_line": 4, "open": false, "start_column": 1}], "last_line_blank": false, "tight": false, "end_line": 2, "t": "Document", "start_line": 1, "open": false, "start_column": 1}'

But if the same command is immediately issued again, it fails:

CommonMark.ASTtoJSON(ast)
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/ipython-0.14.dev-py2.7.egg/IPython/core/interactiveshell.py", line 2759, in run_code
    exec code_obj in self.user_global_ns, self.user_ns
  File "<ipython-input-9-e31549bab177>", line 1, in <module>
    CommonMark.ASTtoJSON(ast)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/CommonMark/CommonMark.py", line 113, in ASTtoJSON
    return json.dumps(prepare(block), default=lambda o: o.__dict__) # sort_keys=True) # indent=4)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/CommonMark/CommonMark.py", line 90, in prepare
    if block.parent:
AttributeError: 'Block' object has no attribute 'parent'

This occurs using CommonMark 0.5.4 as installed from pypi. The same error message occurs regardless of whether I use Python 2.7.3 or Python 3.4.2, Mac OS.