#common-mark

nightly bin+lib rcmark

Bindings to cmark for parsing and rendering CommonMark

1 unstable release

Uses old Rust 2015

0.1.0 Jun 2, 2015

#36 in #common-mark

Apache-2.0/MIT

27KB
602 lines

rcmark

cmark bindings for Rust.

Building

You'll need CMake and pkg-config installed first, unless you already have libcmark installed somewhere. Then, just run:

$ cargo build

lib.rs:

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");

Dependencies

~185KB