Code Monkey home page Code Monkey logo

djot.lua's Introduction

Djot.lua

GitHub CI

This repository contains a Lua parser for djot, a light markup syntax.

Despite being written in an interpreted language, this implementation is very fast (converting a 260K test document in 141 ms on an M1 mac using the standard lua interpreter). It can produce an AST, rendered HTML, or a stream of match tokens that identify elements by source position, which could be used for syntax highlighting or a linting tool.

We also provide a custom pandoc writer for djot (djot-writer.lua), so that documents in other formats can be converted to djot format, and a custom pandoc reader (djot-reader.lua), so that djot documents can be converted to any format pandoc supports. To use these, just put them in your working directory and use pandoc -f djot-reader.lua to convert from djot, and pandoc -t djot-writer.lua to convert to djot. You'll need pandoc version 2.18 or higher, and you'll need the djot library to be installed in your LUA_PATH; see Installing, below. If you're using the dev version of djot or don't want to worry about the djot library being installed, you can create self-contained versions of the custom reader and writer using the amalg tool:

luarocks install amalg
make djot-reader.amalg.lua
make djot-writer.amalg.lua

These can be moved anywhere and do not require any Lua libraries to be installed.

Installing

To install djot using luarocks, just

luarocks install djot

This will install both the library and the executable djot.

Using the Lua library

Quick start

If you just want to parse some input and produce HTML:

local djot = require("djot")
local input = "This is *djot*"
local doc = djot.parse(input)
local html = djot.render_html(doc)

The AST is available as a Lua table, doc.ast.

To render the AST:

local rendered = djot.render_ast_pretty(doc)

Or as JSON:

local rendered = djot.render_ast_json(doc)

To alter the AST with a filter:

local src = "return { str = function(e) e.text = e.text:upper() end }"
local filter = djot.filter.load_filter(src)
djot.filter.apply_filter(doc, filter)

For a streaming parser:

for startpos, endpos, annotation in djot.parse_events("*hello there*") do
  print(startpos, endpos, annotation)
end

(This will print start and end byte offsets into the input for annotated tokens.)

The code

The code for djot (excluding the test suite) is standard Lua, compatible with lua 5.1--5.4 and luajit. Djot has no external dependencies. You can run it without installing it using ./run.sh.

make install will build the rockspec and install the library and executable using luarocks. Once installed, the library can be used by Lua programs, and the executable can be run using djot. djot -h will give help output.

If you can't assume that lua or luajit will be installed on the target machine, you can use make djot in the clib directory to create a portable binary that bakes in a lua interpreter and the necessary scripts.

make test will run the tests, and make testall will also run some tests of pathological cases.

License

The code and documentation are released under the MIT license.

djot.lua's People

Contributors

hellux avatar jgm 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.