Skip to content

Commit

Permalink
feat(pandocast): Support the lunajson parsing library as alternative
Browse files Browse the repository at this point in the history
Alternative to luajson for JSON parsing.
  • Loading branch information
Omikhleia authored and Didier Willis committed Sep 18, 2024
1 parent 17e80d8 commit fef750e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,19 @@ Some *Djot* content

### Pandoc AST alternative package

_Prerequisites:_ The [LuaJSON](https://github.com/harningt/luajson) module must be installed and available to your SILE environment.
This topic is not covered here.
_Prerequisites:_ Either [lunajson](https://github.com/grafi-tt/lunajson) or [LuaJSON](https://github.com/harningt/luajson) must be installed and available to your SILE environment. The former is recommended has no additional dependencies, and is straightforward to install:

```shell
luarocks install lunajson
```

The latter depends on the LPeg library, and this may require some additional tooling to be installed on your system (such as a C compiler, etc.). Moreover, as of 2024, the stable version has some issues, and you will neeed to install the development version instead:

```shell
luarocks install --dev luajson
```

Ready to go?
First, using the appropriate version of Pandoc, convert your file to a JSON AST:

```shell
Expand Down
13 changes: 11 additions & 2 deletions examples/sile-and-pandoc.dj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,17 @@ The following solution is still an experimental proof-of-concept, but you may gi
## Prerequisites

Obviously, you need to have the Pandoc software installed on your system.
You also need to have the LuaJSON module installed and available to your SILE environment.
As of 2024, we recommend using the development version of the module, due to some issues with the current release version.
You also need to have either the *lunajson* or *luajson* module installed and available to your SILE environment:

{custom-style=CodeBlock}
:::
```bash
luarocks install lunajson
```
:::

The former is a Lua-only module, with no external dependencies.
The other is an LPeg-based parser, and on some systems, it may require the LPeg library to be compiled and installed. Luarocks should take care of that for you, but it assumes you have the necessary development tools on your system. Moreover, as of 2024, the current release version of the module has some issues, so we recommend using the development version instead:

{custom-style=CodeBlock}
:::
Expand Down
7 changes: 5 additions & 2 deletions inputters/pandocast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,12 @@ function inputter.appropriate (round, filename, doc)
end

function inputter:parse (doc)
local has_json, json = pcall(require, "json.decode")
local has_json, json = pcall(require, "lunajson")
if not has_json then
SU.error("The pandocast inputter requires LuaJSON's json.decode() to be available.")
has_json, json = pcall(require, "json.decode")
if not has_json then
SU.error("The pandocast inputter requires either luajson or lunajson to be available.")
end
end

local jsast = json.decode(doc)
Expand Down

0 comments on commit fef750e

Please sign in to comment.