226 lines
6.8 KiB
Lua
226 lines
6.8 KiB
Lua
-- fenced-divs.lua — rebuild ::: divs that pandoc left as plain text
|
|
--
|
|
-- Pandoc only recognises a ::: fence nested in a list item when it starts
|
|
-- exactly at the item's text column: 3 spaces after "1. ", 2 after "- ".
|
|
-- Indented one step further (the usual 4 spaces), the fence is read as
|
|
-- paragraph text and the div is lost:
|
|
--
|
|
-- 1. First part.
|
|
--
|
|
-- :::{.hint} <- 4 spaces: printed as ":::{.hint}"
|
|
-- A hint.
|
|
-- :::
|
|
--
|
|
-- This filter finds such literal fence lines and rebuilds the divs from what
|
|
-- lies between them, so a div works at any indentation. The content keeps its
|
|
-- formatting and maths. A list inside such a div needs a blank line before it
|
|
-- (as anywhere in markdown). Unbalanced fences are left as they are, with a
|
|
-- warning.
|
|
--
|
|
-- Runs at pre-ast (see _extension.yml), before every filter that reads divs.
|
|
|
|
local warned = false
|
|
|
|
local function warn(msg)
|
|
if warned then return end
|
|
warned = true
|
|
io.stderr:write("[notes] " .. msg .. "\n")
|
|
end
|
|
|
|
------------------------------------------------------------------ fence lines
|
|
|
|
-- attributes of an opening fence: "{.hint}", "{.solution part=b}", "hint"
|
|
local function parse_attr(rest)
|
|
local src
|
|
if rest:match("^%b{}$") then
|
|
src = rest
|
|
elseif rest:match("^[%w_%-]+$") then
|
|
src = "{." .. rest .. "}"
|
|
else
|
|
return nil
|
|
end
|
|
local ok, doc = pcall(pandoc.read, "[x]" .. src, "markdown")
|
|
if not ok or #doc.blocks ~= 1 then return nil end
|
|
local ins = doc.blocks[1].content
|
|
if #ins ~= 1 or ins[1].t ~= "Span" then return nil end
|
|
return ins[1].attr
|
|
end
|
|
|
|
-- nil, or { close = true }, or { open = true, attr = Attr }
|
|
local function fence_of(line)
|
|
local buf = {}
|
|
for _, it in ipairs(line) do
|
|
if it.t == "Str" then
|
|
buf[#buf + 1] = it.text
|
|
elseif it.t == "Space" then
|
|
buf[#buf + 1] = " "
|
|
elseif it.t == "Quoted" then -- smart quotes ate the quotes of title="..."
|
|
local q = it.quotetype == "DoubleQuote" and '"' or "'"
|
|
buf[#buf + 1] = q .. pandoc.utils.stringify(it.content) .. q
|
|
else
|
|
return nil
|
|
end
|
|
end
|
|
local rest = table.concat(buf):match("^:::+%s*(.-)%s*$")
|
|
if rest == nil then return nil end
|
|
rest = rest:gsub("%s*:+$", "") -- "::: {.hint} :::" is allowed
|
|
if rest == "" then return { close = true } end
|
|
local attr = parse_attr(rest)
|
|
if attr == nil then return nil end
|
|
return { open = true, attr = attr }
|
|
end
|
|
|
|
local function split_lines(inlines)
|
|
local lines, cur = {}, pandoc.Inlines({})
|
|
for _, it in ipairs(inlines) do
|
|
if it.t == "SoftBreak" or it.t == "LineBreak" then
|
|
lines[#lines + 1] = cur
|
|
cur = pandoc.Inlines({})
|
|
else
|
|
cur:insert(it)
|
|
end
|
|
end
|
|
lines[#lines + 1] = cur
|
|
return lines
|
|
end
|
|
|
|
------------------------------------------------------------------ rebuild
|
|
|
|
local repair_blocks
|
|
|
|
-- tokens of a Para/Plain: its text lines, split around fence lines
|
|
local function para_tokens(b, toks)
|
|
local lines = split_lines(b.content)
|
|
local fences = {}
|
|
for i, l in ipairs(lines) do fences[i] = fence_of(l) end
|
|
if next(fences) == nil then
|
|
toks[#toks + 1] = { block = b }
|
|
return false
|
|
end
|
|
local chunk = pandoc.Inlines({})
|
|
local ctor = b.t == "Plain" and pandoc.Plain or pandoc.Para
|
|
local function flush()
|
|
if #chunk > 0 then toks[#toks + 1] = { block = ctor(chunk) } end
|
|
chunk = pandoc.Inlines({})
|
|
end
|
|
for i, l in ipairs(lines) do
|
|
if fences[i] then
|
|
flush()
|
|
toks[#toks + 1] = fences[i]
|
|
else
|
|
if #chunk > 0 then chunk:insert(pandoc.SoftBreak()) end
|
|
chunk:extend(l)
|
|
end
|
|
end
|
|
flush()
|
|
return true
|
|
end
|
|
|
|
-- A fence line right after a list item or quote is swallowed by it (lazy
|
|
-- continuation): its unmatched closing fences are handed back ("hoisted")
|
|
-- to the enclosing level, where their opening fence is.
|
|
local function container_tokens(b, toks)
|
|
local changed = false
|
|
if b.t == "BulletList" or b.t == "OrderedList" then
|
|
local items = {}
|
|
for i, item in ipairs(b.content) do
|
|
local nb, hoist, ch = repair_blocks(item)
|
|
if hoist > 0 and i < #b.content then return nil end
|
|
items[i] = nb
|
|
changed = changed or ch
|
|
if i == #b.content and hoist > 0 then
|
|
b.content = items
|
|
toks[#toks + 1] = { block = b }
|
|
for _ = 1, hoist do toks[#toks + 1] = { close = true } end
|
|
return true
|
|
end
|
|
end
|
|
if changed then b.content = items end
|
|
elseif b.t == "BlockQuote" or b.t == "Div" then
|
|
local nb, hoist, ch = repair_blocks(b.content)
|
|
if hoist > 0 and b.t == "Div" then return nil end
|
|
if ch then b.content = nb; changed = true end
|
|
toks[#toks + 1] = { block = b }
|
|
for _ = 1, hoist do toks[#toks + 1] = { close = true } end
|
|
return changed or hoist > 0
|
|
end
|
|
toks[#toks + 1] = { block = b }
|
|
return changed
|
|
end
|
|
|
|
-- returns new blocks, number of unmatched trailing closing fences, changed?
|
|
function repair_blocks(blocks)
|
|
local toks, changed = {}, false
|
|
for _, b in ipairs(blocks) do
|
|
local ch
|
|
if b.t == "Para" or b.t == "Plain" then
|
|
ch = para_tokens(b, toks)
|
|
else
|
|
ch = container_tokens(b, toks)
|
|
if ch == nil then
|
|
warn("unbalanced ::: fences inside a list; start them at the item's text column")
|
|
return blocks, 0, false
|
|
end
|
|
end
|
|
changed = changed or ch
|
|
end
|
|
if not changed then return blocks, 0, false end
|
|
|
|
local last_content = 0 -- closing fences after this token can be hoisted
|
|
for i, t in ipairs(toks) do
|
|
if not t.close then last_content = i end
|
|
end
|
|
local root = { blocks = pandoc.Blocks({}) }
|
|
local stack = { root }
|
|
local hoist = 0
|
|
for i, t in ipairs(toks) do
|
|
local top = stack[#stack]
|
|
if t.block then
|
|
top.blocks:insert(t.block)
|
|
elseif t.open then
|
|
stack[#stack + 1] = { attr = t.attr, blocks = pandoc.Blocks({}) }
|
|
elseif #stack > 1 then
|
|
stack[#stack] = nil
|
|
stack[#stack].blocks:insert(pandoc.Div(top.blocks, top.attr))
|
|
elseif i > last_content then
|
|
hoist = hoist + 1
|
|
else
|
|
warn("a ::: closing fence has no opening fence; left as text")
|
|
return blocks, 0, false
|
|
end
|
|
end
|
|
if #stack > 1 then
|
|
warn("a ::: fence is never closed; left as text")
|
|
return blocks, 0, false
|
|
end
|
|
return root.blocks, hoist, true
|
|
end
|
|
|
|
------------------------------------------------------------------ filter
|
|
|
|
local function has_literal_fence(doc)
|
|
local found = false
|
|
doc:walk({
|
|
Str = function(s)
|
|
if not found and s.text:sub(1, 3) == ":::" then found = true end
|
|
end,
|
|
})
|
|
return found
|
|
end
|
|
|
|
return {
|
|
{
|
|
Pandoc = function(doc)
|
|
if not has_literal_fence(doc) then return nil end
|
|
local blocks, hoist, changed = repair_blocks(doc.blocks)
|
|
if hoist > 0 then
|
|
warn("a ::: closing fence has no opening fence; left as text")
|
|
return nil
|
|
end
|
|
if not changed then return nil end
|
|
doc.blocks = blocks
|
|
return doc
|
|
end,
|
|
},
|
|
}
|