37 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
			
		
		
	
	
			37 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
 | 
						|
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
 | 
						|
    return { pandoc.Plain { pandoc.Str(contents) } }
 | 
						|
end
 | 
						|
 | 
						|
function Div(e)
 | 
						|
    local include = e.attributes["include"]
 | 
						|
    if include then
 | 
						|
        local blocks = label2Block(include)
 | 
						|
        -- Replace the Div contents
 | 
						|
        return pandoc.Div(blocks, e.attr)
 | 
						|
    end
 | 
						|
    return nil -- no change
 | 
						|
end
 |