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 27, 2024
1 parent 17e80d8 commit 20669c5
Show file tree
Hide file tree
Showing 5 changed files with 93 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 need 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
11 changes: 9 additions & 2 deletions examples/sile-and-pandoc.dj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ 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 a JSON parser, and this collection comes by default with the *lunajson* module as pre-installed dependency.
It is a Lua-only module, with no external dependencies, so it works out of the box.

Another option is to use the *luajson* module, which is an LPeg-based parser, and might (or not) be more efficient in some cases.
On some systems, it may however require the LPeg library to be compiled and installed.
Luarocks{.nobreak} should take care of that for you, but it then 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 All @@ -23,6 +28,8 @@ luarocks install --dev luajson
```
:::

If you have *luajson* installed, it is used instead of the default *lunajson* dependency.

## Using Pandoc's AST with the pandocast package

The experimental *pandocast* package allows you to use Pandoc’s JSON AST as an input format for documents.
Expand Down
11 changes: 9 additions & 2 deletions inputters/pandocast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
-- AST conversion relies on the Pandoc types specification:
-- https://hackage.haskell.org/package/pandoc-types
--
-- Using the LuaJSON library for parsing.
-- Using the luajson (LPEG-based) or lunajson (pure Lua) library for parsing.
-- Reusing the common commands initially made for the "markdown" inputter/package.
--
-- @copyright License: MIT (c) 2022-2024 Omikhleia, Didier Willis
Expand Down Expand Up @@ -582,9 +582,16 @@ function inputter.appropriate (round, filename, doc)
end

function inputter:parse (doc)
-- Load JSON parser:
-- Luajson is LPEG-based, and lunajson is pure Lua without other
-- dependencies. Prioritize the former for if available, otherwise
-- fallback to lunajson, which we'll have in our dependencies.
local has_json, json = pcall(require, "json.decode")
if not has_json then
SU.error("The pandocast inputter requires LuaJSON's json.decode() to be available.")
has_json, json = pcall(require, "lunajson")
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
62 changes: 62 additions & 0 deletions rockspecs/markdown.sile-2.2.0-1.rockspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
rockspec_format = "3.0"
package = "markdown.sile"
version = "2.2.0-1"
source = {
url = "git+https://github.com/Omikhleia/markdown.sile.git",
tag = "v2.2.0",
}
description = {
summary = "Native Markdown support for the SILE typesetting system.",
detailed = [[
This package set for the SILE typesetting system provides a complete redesign
of the native Markdown support for SILE, with a great set of Pandoc-like
extensions and plenty of extra goodies.
]],
homepage = "https://github.com/Omikhleia/markdown.sile",
license = "MIT",
}
dependencies = {
"lua >= 5.1",
"embedders.sile >= 0.2.0",
"labelrefs.sile >= 0.1.0",
"ptable.sile >= 3.1.0",
"smartquotes.sile >= 1.0.0",
"textsubsuper.sile >= 1.1.1",
"silex.sile >= 0.6.0, < 1.0",
"lunajson",
}

build = {
type = "builtin",
modules = {
["sile.classes.markdown"] = "classes/markdown.lua",
["sile.inputters.markdown"] = "inputters/markdown.lua",
["sile.inputters.pandocast"] = "inputters/pandocast.lua",
["sile.inputters.djot"] = "inputters/djot.lua",
["sile.packages.markdown"] = "packages/markdown/init.lua",
["sile.packages.markdown.cmbase"] = "packages/markdown/cmbase.lua",
["sile.packages.markdown.commands"] = "packages/markdown/commands.lua",
["sile.packages.markdown.utils"] = "packages/markdown/utils.lua",
["sile.packages.pandocast"] = "packages/pandocast/init.lua",
["sile.packages.djot"] = "packages/djot/init.lua",

["sile.lunamark"] = "lua-libraries/lunamark.lua",
["sile.lunamark.entities"] = "lua-libraries/lunamark/entities.lua",
["sile.lunamark.reader"] = "lua-libraries/lunamark/reader.lua",
["sile.lunamark.reader.markdown"] = "lua-libraries/lunamark/reader/markdown.lua",
["sile.lunamark.util"] = "lua-libraries/lunamark/util.lua",
["sile.lunamark.writer"] = "lua-libraries/lunamark/writer.lua",
["sile.lunamark.writer.generic"] = "lua-libraries/lunamark/writer/generic.lua",
["sile.lunamark.writer.html"] = "lua-libraries/lunamark/writer/html.lua",
["sile.lunamark.writer.html5"] = "lua-libraries/lunamark/writer/html5.lua",

["sile.djot"] = "lua-libraries/djot.lua",
["sile.djot.attributes"] = "lua-libraries/djot/attributes.lua",
["sile.djot.json"] = "lua-libraries/djot/json.lua",
["sile.djot.block"] = "lua-libraries/djot/block.lua",
["sile.djot.inline"] = "lua-libraries/djot/inline.lua",
["sile.djot.html"] = "lua-libraries/djot/html.lua",
["sile.djot.ast"] = "lua-libraries/djot/ast.lua",
["sile.djot.filter"] = "lua-libraries/djot/filter.lua",
}
}
1 change: 1 addition & 0 deletions rockspecs/markdown.sile-dev-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies = {
"smartquotes.sile",
"textsubsuper.sile",
"silex.sile",
"lunajson",
}

build = {
Expand Down

0 comments on commit 20669c5

Please sign in to comment.