Crate rcmark [] [src]

libcmark bindings for Rust

This library contains bindings to the libcmark C library, which is the C reference implementation of the CommonMark standard. This binding does no additional parsing work beyond that of the underlying library, so it ought to be as accurate.

Nodes

The Node is the core abstraction in rcmark. Nodes can be built up programmatically or by parsing CommonMark source. Nodes all have a type and may have parent, child, and sibling nodes. Depending on the node type, a variety of properties are available. If a property is not applicable to a given node type, then attempting to access it will panic.

use rcmark::{Node, NodeType, ListType};

 let mut root = Node::new(NodeType::Document);

 let mut heading = Node::new(NodeType::Header);
 heading.set_header_level(1);

 let mut heading_text = Node::new(NodeType::Text);
 heading_text.set_literal("Hello, World!");

 heading.prepend_child(&mut heading_text);
 root.prepend_child(&mut heading);

Parsing a Document

Parsing can be done through either a Parser instance or the all-in-one parse_document function.

use rcmark::{Parser, parse_document, DEFAULT, NORMALIZE};

 let doc = parse_document("**Hello**, `World!`", DEFAULT);
 assert_eq!(doc.start_line(), 1);

 let mut parser = Parser::new(NORMALIZE);
 parser.feed("# Hello, World!");
 let doc2 = parser.finish();

Rendering a Document

Rendering could be done manually, but libcmark also provides functions to render to XML, HTML, man pages, and CommonMark.

let doc = rcmark::parse_document("# Hello", rcmark::DEFAULT);

 assert_eq!(rcmark::render_xml(&doc, rcmark::DEFAULT),
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
             <!DOCTYPE CommonMark SYSTEM \"CommonMark.dtd\">\n\
             <document>\n  \
               <header level=\"1\">\n    \
                 <text>Hello</text>\n  \
               </header>\n\
             </document>\n");
 assert_eq!(rcmark::render_html(&doc, rcmark::DEFAULT),
            "<h1>Hello</h1>\n");
 assert_eq!(rcmark::render_man(&doc, rcmark::DEFAULT),
            ".SH\nHello\n");
 assert_eq!(rcmark::render_commonmark(&doc, rcmark::DEFAULT, 2),
            "# Hello\n");

Structs

CmarkOptions
Node

An element of a CommonMark document. This includes headers, paragraphs, links, and various other types. Nodes have a variety of properties depending on their type, all of which are exposed here. However, attempting to access a property that does not apply to the node's type will result in a panic. Setters return true or false to indicate whether the node was updated successfully.

NodeIterator
Parser

Enums

DelimType
EventType

The event types that may be produced by a node iterator.

ListType

The type of CommonMark list.

NodeType

The types of nodes that make up a CommonMark document.

Constants

DEFAULT

Default writer options

HARDBREAKS

Render softbreak elements as hard line breaks

NORMALIZE

Normalize the tree by consolidating adjacent text nodes

SMART

Convert straight quotes to curly quotes, --- to , and -- to

SOURCEPOS

Include a data-sourcepos attribute on block elements

Functions

cmark_version
parse_document
render_commonmark
render_html
render_man
render_xml
version