pandoc-cl/include-code.lua

59 lines
1.3 KiB
Lua
Raw Normal View History

2020-09-22 20:44:06 +00:00
local system = require 'pandoc.system'
local function get_file(filename)
local sysdir = system.get_working_directory()
for k,v in pairs(PANDOC_STATE.resource_path) do
local path = sysdir.."/"..v.."/"..filename
local f = io.open(path, "r")
if f ~= nil then
return f
else
io.stderr:write(string.format("file not found, content of code block not replaced: %s", filename))
return nil
end
end
end
local function get_content(file, ranges)
if ranges == nil then
return file:read "*a"
end
print(ranges)
local intervals = {}
for interval in ranges:gmatch('[^,%s]+') do
table.insert(intervals, interval)
end
local current_line = 0
for k, interval in pairs(intervals) do
print("handling interval", interval)
local first = 0
local last = 0
-- inspect interval
if string.find(interval, '-') then
-- we have an interval
first, last = interval:match("([0-9]+)-([0-9]+)")
print(first, last)
end
end
return ""
end
function CodeBlock(elem)
if (elem.attributes['include'] ~= nil) then
local filename = elem.attributes['include']
local f = get_file(filename)
if (f == nil) then
return elem
end
elem.text = get_content(f, elem.attributes['ranges'])
io.close(f)
end
return elem
end