49 lines
1.4 KiB
Lua
49 lines
1.4 KiB
Lua
|
|
-- math-omit.lua
|
||
|
|
-- Replace any math with a placeholder and ensure a space before it when appropriate.
|
||
|
|
local PH = "[math omitted]"
|
||
|
|
|
||
|
|
function Math(el)
|
||
|
|
-- Emit the placeholder as a Str; spacing is fixed in Para/Plain below.
|
||
|
|
return pandoc.Str(PH)
|
||
|
|
end
|
||
|
|
|
||
|
|
local function ensure_space_before_ph(inlines)
|
||
|
|
local out = {}
|
||
|
|
for i = 1, #inlines do
|
||
|
|
local cur = inlines[i]
|
||
|
|
if cur.t == "Str" and cur.text == PH then
|
||
|
|
local prev = out[#out]
|
||
|
|
local need_space = true
|
||
|
|
|
||
|
|
-- No space if it's the first token in the block
|
||
|
|
if not prev then
|
||
|
|
need_space = false
|
||
|
|
elseif prev.t == "Space" then
|
||
|
|
need_space = false
|
||
|
|
elseif prev.t == "Str" then
|
||
|
|
-- If previous char is an opening bracket/paren/slash/hyphen or whitespace, skip
|
||
|
|
local last = prev.text:sub(-1)
|
||
|
|
if last:match("[%(%[%{%/%-]") or last:match("%s") then
|
||
|
|
need_space = false
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
if need_space then table.insert(out, pandoc.Space()) end
|
||
|
|
table.insert(out, cur)
|
||
|
|
else
|
||
|
|
table.insert(out, cur)
|
||
|
|
end
|
||
|
|
end
|
||
|
|
return out
|
||
|
|
end
|
||
|
|
|
||
|
|
function Para(el)
|
||
|
|
el.content = ensure_space_before_ph(el.content)
|
||
|
|
return el
|
||
|
|
end
|
||
|
|
|
||
|
|
function Plain(el)
|
||
|
|
el.content = ensure_space_before_ph(el.content)
|
||
|
|
return el
|
||
|
|
end
|