32 lines
1.1 KiB
Lua
32 lines
1.1 KiB
Lua
-- Copyright (c) 2026 Marc Wäckerlin. SPDX-License-Identifier: MIT
|
|||
|
|
-- A folded block does not belong in a document nobody can click.
|
||
|
|
--
|
||
|
|
-- `<details>` with its `<summary>` is a fold of the browser: the reader opens
|
||
|
|
-- it when he wants it and sees a single line meanwhile. On paper there is
|
||
|
|
-- nothing to open, so the fold arrives unfolded — measured in one report, the
|
||
|
|
-- diagram sources behind it filled whole pages under the pictures they draw.
|
||
|
|
-- The block goes out with everything in it, the summary line included.
|
||
|
|
|
||
|
|
local OPEN = '<details'
|
||
|
|
local CLOSE = '</details>'
|
||
|
|
|
||
|
|
local function marker(block)
|
||
|
|
return block.t == 'RawBlock' and block.format == 'html' and block.text or ''
|
||
|
|
end
|
||
|
|
|
||
|
|
function Blocks(blocks)
|
||
|
|
local kept, depth = {}, 0
|
||
|
|
for _, block in ipairs(blocks) do
|
||
|
|
local raw = marker(block)
|
||
|
|
local opens = raw:find(OPEN, 1, true)
|
||
|
|
local closes = raw:find(CLOSE, 1, true)
|
||
|
|
if opens then
|
||
|
|
depth = depth + (closes and 0 or 1)
|
||
|
|
elseif closes then
|
||
|
|
depth = math.max(0, depth - 1)
|
||
|
|
elseif depth == 0 then
|
||
|
|
kept[#kept + 1] = block
|
||
|
|
end
|
||
|
|
end
|
||
|
|
return kept
|
||
|
|
end
|