[][src]Type Definition cmark_gfm_sys::cmark_event_type

type cmark_event_type = u32;

Iterator

An iterator will walk through a tree of nodes, starting from a root node, returning one node at a time, together with information about whether the node is being entered or exited. The iterator will first descend to a child node, if there is one. When there is no child, the iterator will go to the next sibling. When there is no next sibling, the iterator will return to the parent (but with a 'cmark_event_type' of CMARK_EVENT_EXIT). The iterator will return CMARK_EVENT_DONE when it reaches the root node again. One natural application is an HTML renderer, where an ENTER event outputs an open tag and an EXIT event outputs a close tag. An iterator might also be used to transform an AST in some systematic way, for example, turning all level-3 headings into regular paragraphs.

void
usage_example(cmark_node *root) {
    cmark_event_type ev_type;
    cmark_iter *iter = cmark_iter_new(root);

    while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) {
        cmark_node *cur = cmark_iter_get_node(iter);
        // Do something with `cur` and `ev_type`
    }

    cmark_iter_free(iter);
}

Iterators will never return EXIT events for leaf nodes, which are nodes of type:

  • CMARK_NODE_HTML_BLOCK
  • CMARK_NODE_THEMATIC_BREAK
  • CMARK_NODE_CODE_BLOCK
  • CMARK_NODE_TEXT
  • CMARK_NODE_SOFTBREAK
  • CMARK_NODE_LINEBREAK
  • CMARK_NODE_CODE
  • CMARK_NODE_HTML_INLINE

Nodes must only be modified after an EXIT event, or an ENTER event for leaf nodes.