class commonmark.blocks.Parser(options={})[source]
add_child(tag, offset)[source]

Add block of type tag as a child of the tip. If the tip can’t accept children, close and finalize it and try its parent, and so on til we find a block that can accept children.


Add a line to the block at the tip. We assume the tip can accept lines – that check should be done before calling this.


Finalize and close any unmatched blocks.

finalize(block, line_number)[source]

Finalize a block. Close it and do any necessary postprocessing, e.g. creating string_content from strings, setting the ‘tight’ or ‘loose’ status of a list, and parsing the beginnings of paragraphs for reference definitions. Reset the tip to the parent of the closed block.


Analyze a line of text and update the document appropriately.

We parse markdown text by calling this on each line of input, then finalizing the document.


The main parsing function. Returns a parsed document AST.


Walk through a block & children recursively, parsing string content into inline content where appropriate.