-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(import): Add Lua filter for reading unformatted text
Sourced from GH issue comment: jgm/pandoc#6393 (comment)
- Loading branch information
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |