includes multiple files

This commit is contained in:
2025-10-10 11:06:34 +08:00
parent a0aa9e611b
commit 5a20323b59
4 changed files with 93 additions and 19 deletions

View File

@@ -1,18 +1,34 @@
function File2Block(filename)
local fh = io.open(filename, "r")
if not fh then
io.stderr:write("Cannot open file: " .. filename .. "\n")
return { pandoc.Plain { pandoc.Str("[Error: cannot read file " .. filename .. "]") } }
local function words(s)
local res = {}
for part in s:gmatch("[^,]+") do -- split by commas
local trimmed = part:match("^%s*(.-)%s*$") -- remove leading/trailing spaces
if trimmed ~= "" then
table.insert(res, trimmed)
end
end
return res
end
local function label2Block(labels)
local contents = ""
for _,l in ipairs(words(labels)) do
-- use file for now.
local fh = io.open(l, "r")
if not fh then
io.stderr:write("Cannot open file: " .. l .. "\n")
return { pandoc.Plain { pandoc.Str("[Error: cannot read file " .. l .. "]") } }
end
contents = contents .. fh:read("*a")
fh:close()
end
local contents = fh:read("*a")
fh:close()
return { pandoc.Plain { pandoc.Str(contents) } }
end
function Div(e)
local include = e.attributes["include"]
if include then
local blocks = File2Block(include)
local blocks = label2Block(include)
-- Replace the Div contents
return pandoc.Div(blocks, e.attr)
end