Git Product home page Git Product logo

commonmark-lua's Introduction

commonmark-lua

This is a lua binding to the libcmark commonmark parser (https://github.com/jgm/CommonMark). It requires that libcmark be installed, and it also requires luajit.

This is work in progress. The idea is to use libcmark to do the parsing, and provide pure lua writers and lua hooks for AST manipulation. This way authors can easily customize the way their text is rendered, and create renderers for new formats.

So far we have an xml writer (which prints an XML serialization of the AST) and an html writer (which passes the CommonMark spec tests).

Writers can easily be customized. For example, suppose you want to render all regular text content uppercase, leaving code and HTML tags alone:

#!/usr/bin/env luajit

local cmark = require("commonmark")
local W = require("commonmark.writer.html")

local inp = io.read("*all")
local doc = cmark.parse_document(inp, string.len(inp))

writer = W.new()
-- customize the writer:
writer.text = function(node)
   local s = cmark.node_get_string_content(node)
   writer.out(writer.escape(s:upper()))
end

io.write(writer.render(doc))

for warning in writer.warnings() do
   print('WARNING', warning)
end

Alternatively, you could walk the AST, changing text content to uppercase, and use the faster libcmark HTML renderer:

#!/usr/bin/env luajit

local cmark = require("commonmark")
local W = require("commonmark.writer.html")

local inp = io.read("*all")
local doc = cmark.parse_document(inp, string.len(inp))

for node in cmark.walk(doc) do
   if cmark.node_type(node) == 'text' then
      cmark.node_set_string_content(node, cmark.node_get_string_content(node):upper())
   end
end

io.write(cmark.render_html(doc))

No doubt the interface can be improved, but this is a start.

commonmark-lua's People

Contributors

grantmacken avatar jgm avatar

Watchers

 avatar  avatar

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.