Skip to content

Commit

Permalink
feat(import): Add Lua filter for reading unformatted text
Browse files Browse the repository at this point in the history
Sourced from GH issue comment:
jgm/pandoc#6393 (comment)
  • Loading branch information
jgm authored and alerque committed Aug 22, 2024
1 parent d0f7e6a commit f2dfc14
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/plain_reader.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- A sample custom reader that just parses text into blankline-separated
-- paragraphs with space-separated words.

-- For better performance we put these functions in local variables:
local P, S, V, Ct = lpeg.P, lpeg.S, lpeg.V, lpeg.Ct

local whitespacechar = S(" \t\r\n")
local wordchar = (1 - whitespacechar)
local spacechar = S(" \t")
local newline = P("\r") ^ -1 * P("\n")
local blanklines = newline * (spacechar ^ 0 * newline) ^ 1
local endline = newline - blanklines

-- Grammar
local G = P({
"Pandoc",
Pandoc = Ct(V("Block") ^ 0) / pandoc.Pandoc,
Block = blanklines ^ 0 * V("Para"),
Para = Ct(V("Inline") ^ 1) / pandoc.Para,
Inline = V("Str") + V("Space") + V("SoftBreak"),
Str = wordchar ^ 1 / pandoc.Str,
Space = spacechar ^ 1 / pandoc.Space,
SoftBreak = endline / pandoc.SoftBreak,
})

function Reader (input)
return lpeg.match(G, tostring(input))
end

0 comments on commit f2dfc14

Please sign in to comment.