Git Product home page Git Product logo

cmark-lua's Introduction

cmark-lua

Lua wrapper for libcmark, CommonMark parsing and rendering library

To install: luarocks install cmark.

cmark

cmark exposes the entire API of libcmark, as documented in the cmark(3) man page. Basic usage:

local cmark = require("cmark")

local doc = cmark.parse_document(input, string.len(input), cmark.OPT_DEFAULT)
local html = cmark.render_html(doc, cmark.OPT_DEFAULT)

For convenience, constants and functions are renamed so that an initial cmark_ or CMARK_ is omitted: for example, CMARK_NODE_PARAGRAPH is exposed as cmark.NODE_PARAGRAPH and cmark_parse_document as cmark.parse_document.

Two additional functions are provided:

cmark.parse_string(s, opts) is like parse_document, but does not require you to specify the length of the input string.

cmark.walk(node) wraps cmark's iterator interface in a format that is more lua-esque. Usage example:

local links = 0
for cur, entering, node_type in cmark.walk(doc) do
   if node_type == cmark.NODE_LINK and not entering then
       links = links + 1
       -- insert " (link #n)" after the link:
       local t = cmark.node_new(NODE_TEXT)
       cmark.node_set_literal(t, string.format(" (link #%d)", links))
       cmark.node_insert_after(cur, t)
   end
end

The memory allocated by libcmark for node objects must be freed by the calling program by calling cmark.node_free on the document node. (This will automatically free all children as well.)

In addition, a C function

void push_cmark_node(lua_State *L, cmark_node *node)

is exported to make it easier to use these functions from the C API.

For a higher-level interface, see lcmark.

cmark.builder

A special module, cmark.builder, is provided to make it easier to construct cmark nodes.

Usage examples:

local b = require 'cmark.builder'
local mydoc = b.document{
                b.paragraph{
                  b.text "Hello ",
                  b.emph{
                    b.text "world" },
                  b.link{
                    url = "http://example.com",
                    b.text "!" } } }

The arguments to builder functions are generally tables. Key-value pairs are used to set attributes, and the other values are used as children or literal string content, as appropriate.

The library will interpret values as the appropriate types, when possible. So, you can supply a single value instead of an array. And you can supply a string instead of an inline node, or a node instead of a list item. The following is equivalent to the example above:

local mydoc = b.document{
                b.paragraph{
                  "Hello ", b.emph "world",
                  b.link{ url="http://example.com", "!"} }}

The builder functions are

builder.document{block1, block2, ...}
builder.block_quote{block1, block2, ...}
builder.ordered_list{delim = cmark.PAREN_DELIM, item1, item2, ...}
-- attributes: delim, start, tight
builder.bullet_list  -- attributes: tight
builder.item
builder.code_block  -- attributes: info
builder.html_block
builder.custom_block --  attributes: on_enter, on_exit
builder.thematic_break
builder.heading  -- attributes: level
builder.paragraph
builder.text
builder.emph
builder.strong
builder.link   -- attributes: title, url
builder.image  -- attributes: title, url
builder.linebreak
builder.softbreak
builder.code
builder.html_inline
builder.custom_inline  -- attributes: on_enter, on_exit
builder.get_children(node) -- returns children of a node as a table

For developers

make builds the rock and installs it locally.

make test runs some tests. These are in test.t. You'll need the prove executable and the lua-TestMore rock.

make update will update the C sources and spec test from the ../cmark directory.

cmark-lua's People

Contributors

deepakjois avatar jgm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

cmark-lua's Issues

Add documentation

It would be good to make a list of all available methods:

  • node_get_type_string
  • node_get_fence_info
  • node_get_url
  • node_set_literal
  • node_insert_after
  • โ€ฆ

Does not compile on Windows 10

luarocks install cmark yields the following error on Windows 10 (with the VS 2019 Build Tools and Windows 10 SDK):

C:\Program Files (x86)\LuaRocks>luarocks.bat install cmark
Installing https://luarocks.org/cmark-0.29.0-1.src.rock

cl /nologo /MD /O2 -c -Focmark_wrap.obj -IC:\Program Files (x86)\Lua\5.1\include cmark_wrap.c -I. -Iext
cmark_wrap.c
ext\cmark.h(27): error C2143: syntax error: missing ')' before '('
ext\cmark.h(27): error C2091: function returns function
ext\cmark.h(27): error C2143: syntax error: missing ')' before 'string'
ext\cmark.h(27): error C2143: syntax error: missing '{' before 'string'
ext\cmark.h(27): error C2059: syntax error: 'string'
ext\cmark.h(27): error C2059: syntax error: ')'
ext\cmark.h(111): error C2143: syntax error: missing ')' before '('
ext\cmark.h(111): error C2091: function returns function
ext\cmark.h(111): error C2143: syntax error: missing ')' before 'string'
ext\cmark.h(111): error C2143: syntax error: missing '{' before 'string'
ext\cmark.h(111): error C2059: syntax error: 'string'
ext\cmark.h(111): error C2059: syntax error: ')'
ext\cmark.h(117): error C2143: syntax error: missing ')' before '('
ext\cmark.h(117): error C2091: function returns function
ext\cmark.h(117): error C2143: syntax error: missing ')' before 'string'
ext\cmark.h(117): error C2143: syntax error: missing '{' before 'string'
ext\cmark.h(117): error C2059: syntax error: 'string'
ext\cmark.h(117): error C2059: syntax error: ')'
ext\cmark.h(122): error C2143: syntax error: missing ')' before '('
ext\cmark.h(122): error C2091: function returns function
ext\cmark.h(122): error C2143: syntax error: missing ')' before 'string'
ext\cmark.h(122): error C2143: syntax error: missing '{' before 'string'
ext\cmark.h(122): error C2059: syntax error: 'string'
ext\cmark.h(122): error C2059: syntax error: ')'
ext\cmark.h(131): error C2143: syntax error: missing ')' before '('
ext\cmark.h(131): error C2091: function returns function
ext\cmark.h(131): error C2143: syntax error: missing ')' before 'string'
ext\cmark.h(131): error C2143: syntax error: missing '{' before 'string'
ext\cmark.h(131): error C2059: syntax error: 'string'
ext\cmark.h(131): error C2059: syntax error: ')'
ext\cmark.h(136): error C2143: syntax error: missing ')' before '('
ext\cmark.h(136): error C2091: function returns function
ext\cmark.h(136): error C2143: syntax error: missing ')' before 'string'
ext\cmark.h(136): error C2143: syntax error: missing '{' before 'string'
ext\cmark.h(136): error C2059: syntax error: 'string'
ext\cmark.h(136): error C2059: syntax error: ')'
ext\cmark.h(140): error C2143: syntax error: missing ')' before '('
ext\cmark.h(140): error C2091: function returns function
ext\cmark.h(140): error C2143: syntax error: missing ')' before 'string'
ext\cmark.h(140): error C2143: syntax error: missing '{' before 'string'
ext\cmark.h(140): error C2059: syntax error: 'string'
ext\cmark.h(140): error C2059: syntax error: ')'
ext\cmark.h(144): error C2143: syntax error: missing ')' before '('
ext\cmark.h(144): error C2091: function returns function
ext\cmark.h(144): error C2143: syntax error: missing ')' before 'string'
ext\cmark.h(144): error C2143: syntax error: missing '{' before 'string'
ext\cmark.h(144): error C2059: syntax error: 'string'
ext\cmark.h(144): error C2059: syntax error: ')'
ext\cmark.h(148): error C2143: syntax error: missing ')' before '('
ext\cmark.h(148): error C2091: function returns function
ext\cmark.h(148): error C2143: syntax error: missing ')' before 'string'
ext\cmark.h(148): error C2143: syntax error: missing '{' before 'string'
ext\cmark.h(148): error C2059: syntax error: 'string'
ext\cmark.h(148): error C2059: syntax error: ')'
ext\cmark.h(207): error C2143: syntax error: missing ')' before '('
ext\cmark.h(207): error C2091: function returns function
ext\cmark.h(207): error C2143: syntax error: missing ')' before 'string'
ext\cmark.h(207): error C2143: syntax error: missing '{' before 'string'
ext\cmark.h(207): error C2059: syntax error: 'string'
ext\cmark.h(207): error C2059: syntax error: ')'
ext\cmark.h(212): error C2143: syntax error: missing ')' before '('
ext\cmark.h(212): error C2091: function returns function
ext\cmark.h(212): error C2143: syntax error: missing ')' before 'string'
ext\cmark.h(212): error C2143: syntax error: missing '{' before 'string'
ext\cmark.h(212): error C2059: syntax error: 'string'
ext\cmark.h(212): error C2059: syntax error: ')'
ext\cmark.h(218): error C2143: syntax error: missing ')' before '('
ext\cmark.h(218): error C2091: function returns function
ext\cmark.h(218): error C2143: syntax error: missing ')' before 'string'
ext\cmark.h(218): error C2143: syntax error: missing '{' before 'string'
ext\cmark.h(218): error C2059: syntax error: 'string'
ext\cmark.h(218): error C2059: syntax error: ')'
ext\cmark.h(223): error C2143: syntax error: missing ')' before '('
ext\cmark.h(223): error C2091: function returns function
ext\cmark.h(223): error C2143: syntax error: missing ')' before 'string'
ext\cmark.h(223): error C2143: syntax error: missing '{' before 'string'
ext\cmark.h(223): error C2059: syntax error: 'string'
ext\cmark.h(223): error C2059: syntax error: ')'
ext\cmark.h(228): error C2143: syntax error: missing ')' before '('
ext\cmark.h(228): error C2091: function returns function
ext\cmark.h(228): error C2143: syntax error: missing ')' before 'string'
ext\cmark.h(228): error C2143: syntax error: missing '{' before 'string'
ext\cmark.h(228): error C2059: syntax error: 'string'
ext\cmark.h(228): error C2059: syntax error: ')'
ext\cmark.h(233): error C2143: syntax error: missing ')' before '('
ext\cmark.h(233): error C2091: function returns function
ext\cmark.h(233): error C2143: syntax error: missing ')' before 'string'
ext\cmark.h(233): error C2143: syntax error: missing '{' before 'string'
ext\cmark.h(233): error C2059: syntax error: 'string'
ext\cmark.h(233): error C2059: syntax error: ')'
ext\cmark.h(240): error C2143: syntax error: missing ')' before '('
ext\cmark.h(240): error C2091: function returns function
ext\cmark.h(240): error C2143: syntax error: missing ')' before 'string'
ext\cmark.h(240): error C2143: syntax error: missing '{' before 'string'
ext\cmark.h(240): error C2059: syntax error: 'string'
ext\cmark.h(240): error C2059: syntax error: ')'
ext\cmark.h(250): error C2143: syntax error: missing ')' before '('
ext\cmark.h(250): error C2091: function returns function
ext\cmark.h(250): error C2143: syntax error: missing ')' before 'string'
ext\cmark.h(250): error C2143: syntax error: missing '{' before 'string'
ext\cmark.h(250): error C2059: syntax error: 'string'
ext\cmark.h(250): fatal error C1003: error count exceeds 100; stopping compilation

Error: Build error: Failed compiling object cmark_wrap.obj

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.