From 6eab8e6ceacab18da5cb119d2867e52d9aee37be Mon Sep 17 00:00:00 2001 From: kainctl Date: Sun, 4 Jan 2026 13:37:38 +0100 Subject: [PATCH 1/2] make functions local instead of global --- include-files.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include-files.lua b/include-files.lua index 8abcca7..1499c14 100644 --- a/include-files.lua +++ b/include-files.lua @@ -6,13 +6,14 @@ -- Module pandoc.path is required and was added in version 2.12 PANDOC_VERSION:must_be_at_least '2.12' +local pandoc = require 'pandoc' local List = require 'pandoc.List' local path = require 'pandoc.path' local system = require 'pandoc.system' --- Get include auto mode local include_auto = false -function get_vars (meta) +local function get_vars (meta) if meta['include-auto'] then include_auto = true end @@ -20,7 +21,7 @@ end --- Keep last heading level found local last_heading_level = 0 -function update_last_level(header) +local function update_last_level(header) last_heading_level = header.level end @@ -62,8 +63,7 @@ local function update_contents(blocks, shift_by, include_path) end --- Filter function for code blocks -local transclude -function transclude (cb) +local function transclude (cb) -- ignore code blocks which are not of class "include". if not cb.classes:includes 'include' then return From b8cc18752c7cb6173ad5ac3fe94841d9648d08cb Mon Sep 17 00:00:00 2001 From: kainctl Date: Sun, 4 Jan 2026 14:48:02 +0100 Subject: [PATCH 2/2] feat: add `include-as-code-block` --- expected-auto.native | 7 +++++++ expected.native | 7 +++++++ include-files.lua | 29 ++++++++++++++++++++++++++++- sample.md | 7 +++++++ 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/expected-auto.native b/expected-auto.native index 604cb86..0a0e93c 100644 --- a/expected-auto.native +++ b/expected-auto.native @@ -164,6 +164,13 @@ , CodeBlock ( "" , [ "c" ] , [ ( "include" , "subdir/somecode.c" ) ] ) "" +, Header + 1 + ( "include-code" , [] , [] ) + [ Str "Include" , Space , Str "Code" ] +, CodeBlock + ( "" , [ "org" ] , [] ) + "* Org header\n\nThis is /emphasized/.\n" , Header 1 ( "appendix" , [] , [] ) [ Str "Appendix" ] , Para [ Str "More" diff --git a/expected.native b/expected.native index 4977f8d..1dde6e8 100644 --- a/expected.native +++ b/expected.native @@ -164,6 +164,13 @@ , CodeBlock ( "" , [ "c" ] , [ ( "include" , "subdir/somecode.c" ) ] ) "" +, Header + 1 + ( "include-code" , [] , [] ) + [ Str "Include" , Space , Str "Code" ] +, CodeBlock + ( "" , [ "org" ] , [] ) + "* Org header\n\nThis is /emphasized/.\n" , Header 1 ( "appendix" , [] , [] ) [ Str "Appendix" ] , Para [ Str "More" diff --git a/include-files.lua b/include-files.lua index 1499c14..8f6815c 100644 --- a/include-files.lua +++ b/include-files.lua @@ -62,6 +62,32 @@ local function update_contents(blocks, shift_by, include_path) return pandoc.walk_block(pandoc.Div(blocks), update_contents_filter).content end +--- Include given file paths as code-blocks +--- while keeping the remaining attributes. +local function include_as_code_block(cb) + local _, match_pos = cb.classes:find('include-as-code-block') + if match_pos == nil then + return + end + + cb.classes:remove(match_pos) + local paths = cb.text + + cb.text = "" + for line in paths:gmatch('[^\n]+') do + if line:sub(1,2) ~= '//' then + local fh = io.open(line) + if not fh then + io.stderr:write("Cannot open file " .. line .. " | Skipping include-as-code-block\n") + else + cb.text = cb.text .. fh:read "*a" + fh:close() + end + end + end + return cb +end + --- Filter function for code blocks local function transclude (cb) -- ignore code blocks which are not of class "include". @@ -123,5 +149,6 @@ end return { { Meta = get_vars }, - { Header = update_last_level, CodeBlock = transclude } + { Header = update_last_level, CodeBlock = transclude }, + { CodeBlock = include_as_code_block } } diff --git a/sample.md b/sample.md index c283170..17a3ce9 100644 --- a/sample.md +++ b/sample.md @@ -37,6 +37,13 @@ file-f.md subdir/file-g.md ``` +# Include Code + +``` {.include-as-code-block .org} +// include org-mode file as code-block +file-d.org +``` + # Appendix More info goes here.