RC RANDOM CHAOS

Pandoc 2.0 Bakes a Lua Interpreter In, Killing JSON Filter Overhead

· via Hacker News

Original source

Pandoc Lua Filters

Hacker News →

Pandoc has always let users hook into its document-conversion pipeline with filters that rewrite the abstract syntax tree between parsing and output. The traditional approach pipes the AST out as JSON, lets an external program in any language mutate it, and reads the modified JSON back—flexible, but slow and fragile. Every filter depends on the user having the right interpreter and AST library installed, so a filter that works on one machine may not run on another.

Since version 2.0, Pandoc ships with an embedded Lua 5.4 interpreter and a native filter library, so filters run with zero external dependencies and Pandoc data is passed directly to Lua instead of being serialized to JSON over a pipe. A filter is just a Lua table mapping element names (Strong, Str, Para, etc.) to functions that return the replacement node; missing handlers fall back to generic Inline/Block functions or leave elements untouched. Benchmarks converting the Pandoc manual to HTML show the Lua version comfortably beating both a compiled Haskell filter and an interpreted Python one, purely by avoiding the marshaling round-trip.

Later releases layered on more control: sequence-level Inlines and Blocks functions (2.9.2) for logic that needs surrounding context, and a selectable traversal order (2.17) that adds top-down, depth-first walking alongside the default typewise pass. Top-down traversals can be pruned mid-tree by returning false, useful for skipping subtrees like footnote contents. The result is a portable, self-contained way to script document transformations that anyone with the Pandoc binary can run.

Read the full article

Continue reading at Hacker News →

This is an AI-generated summary. Read the original for the full story.