Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions expected-auto.native
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions expected.native
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
37 changes: 32 additions & 5 deletions include-files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@
-- 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
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

Expand Down Expand Up @@ -61,9 +62,34 @@ 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 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
Expand Down Expand Up @@ -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 }
}
7 changes: 7 additions & 0 deletions sample.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down