depth 1 replace
This commit is contained in:
		
							
								
								
									
										53
									
								
								filter.lua
									
									
									
									
									
								
							
							
						
						
									
										53
									
								
								filter.lua
									
									
									
									
									
								
							@@ -1,3 +1,11 @@
 | 
			
		||||
local label_map = {}               -- label has to be unique!
 | 
			
		||||
 | 
			
		||||
local function collect_labels(div) -- only collect labels in div
 | 
			
		||||
    if div.identifier and div.identifier ~= "" then
 | 
			
		||||
        label_map[div.identifier] = div:clone()
 | 
			
		||||
    end
 | 
			
		||||
    return nil
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local function words(s)
 | 
			
		||||
    local res = {}
 | 
			
		||||
@@ -10,27 +18,36 @@ local function words(s)
 | 
			
		||||
    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 function replace(e)
 | 
			
		||||
    local include = e.attributes["include"]
 | 
			
		||||
    if include then
 | 
			
		||||
        local blocks = label2Block(include)
 | 
			
		||||
        local blocks = {}
 | 
			
		||||
        -- Replace the Div contents
 | 
			
		||||
        for _, l in ipairs(words(include)) do
 | 
			
		||||
            if label_map[l] then
 | 
			
		||||
                table.insert(blocks, label_map[l]:clone())
 | 
			
		||||
                io.stderr:write("insert [" .. label_map[l].identifier .. ']\n')
 | 
			
		||||
            else
 | 
			
		||||
                io.stderr:write("Cannot find AST node with label " .. l)
 | 
			
		||||
            end
 | 
			
		||||
        end
 | 
			
		||||
        return pandoc.Div(blocks, e.attr)
 | 
			
		||||
    end
 | 
			
		||||
    return nil -- no change
 | 
			
		||||
    return e -- no change
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
return {
 | 
			
		||||
    -- traverse = 'topdown',
 | 
			
		||||
    Pandoc = function(doc)
 | 
			
		||||
        -- collect labels
 | 
			
		||||
        for _, blk in ipairs(doc.blocks) do
 | 
			
		||||
            collect_labels(blk)
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        -- replace
 | 
			
		||||
        for i, blk in ipairs(doc.blocks) do
 | 
			
		||||
            doc.blocks[i] = replace(blk)
 | 
			
		||||
        end
 | 
			
		||||
        return doc
 | 
			
		||||
    end
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user