add basic include-code.lua script

This commit is contained in:
Julien Fastré 2020-09-22 22:44:06 +02:00
parent 0737a75e7e
commit 54dcee4dc9
3 changed files with 70 additions and 0 deletions

3
fixtures/inc.sql Normal file
View File

@ -0,0 +1,3 @@
DROP SCHEMA IF EXISTS maryam CASCADE;
CREATE SCHEMA maryam;

9
fixtures/include-code.md Normal file
View File

@ -0,0 +1,9 @@
```{.sql include=fixtures/inc.sql ranges=2-$ }
will be removed by include
```
```sql
SELECT * FROM source;
```

58
include-code.lua Normal file
View File

@ -0,0 +1,58 @@
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