local P, S, R, Cf, Cc, Ct, V, Cs, Cg, Cb, B, C, Cmt =
lpeg.P, lpeg.S, lpeg.R, lpeg.Cf, lpeg.Cc, lpeg.Ct, lpeg.V,
lpeg.Cs, lpeg.Cg, lpeg.Cb, lpeg.B, lpeg.C, lpeg.Cmt
local whitespacechar = S(" \t\r\n")
local specialchar = S("/*~[]\\{}|")
local wordchar = (1 - (whitespacechar + specialchar ))
local spacechar = S(" \t")
local newline = P"\r"^-1 * P"\n"
local blankline = spacechar^0 * newline
local blanklines = newline * (spacechar^0 * newline)^1
local endline = newline - blanklines
local function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
-- Grammar
G = P{ "Pandoc",
Pandoc = Ct(V"Block"^0) / pandoc.Pandoc;
Block = blanklines^0 * V"Para" ;
Para = Ct(V"Inline"^1) / pandoc.Para;
Meta = V"MetaList" / pandoc.MetaBlocks;
MetaList = Ct(V"Abstract"^0) / pandoc.MetaList;
Inline = V"Str" + V"Space" + V"SoftBreak" ;
Abstract = P"\\abstract{"
* C((1-S"}")^1)
* P"}"
/ pandoc.Str;
Str = wordchar^1 / pandoc.Str;
Space = spacechar^1 / pandoc.Space;
SoftBreak = endline / pandoc.SoftBreak;
}
function Reader(input)
return lpeg.match(G, tostring(input))
end
```
Also I want to know how to add custom labels to the metaInlines.
It might sound like reinventing the wheel a bit but it is my curiosity that wants to explore implementing custom readers.