From 54dcee4dc9120bbe5a60422ccd3bd037320d6b3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 22 Sep 2020 22:44:06 +0200 Subject: [PATCH] add basic include-code.lua script --- fixtures/inc.sql | 3 +++ fixtures/include-code.md | 9 +++++++ include-code.lua | 58 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 fixtures/inc.sql create mode 100644 fixtures/include-code.md create mode 100644 include-code.lua diff --git a/fixtures/inc.sql b/fixtures/inc.sql new file mode 100644 index 0000000..74783be --- /dev/null +++ b/fixtures/inc.sql @@ -0,0 +1,3 @@ +DROP SCHEMA IF EXISTS maryam CASCADE; + +CREATE SCHEMA maryam; diff --git a/fixtures/include-code.md b/fixtures/include-code.md new file mode 100644 index 0000000..6b872f4 --- /dev/null +++ b/fixtures/include-code.md @@ -0,0 +1,9 @@ + +```{.sql include=fixtures/inc.sql ranges=2-$ } +will be removed by include +``` + +```sql +SELECT * FROM source; +``` + diff --git a/include-code.lua b/include-code.lua new file mode 100644 index 0000000..5bb821f --- /dev/null +++ b/include-code.lua @@ -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