-
-
Save tarleb/1a690d38508d99c88c331f63ce7f6a2c to your computer and use it in GitHub Desktop.
--- Pandoc Lua filter to include other Markdown files | |
--- | |
--- Usage: Use a special code block with class `include` to | |
--- include Markdown files. Each code line is treated as the | |
--- filename of a Markdown file, parsed as Markdown, and | |
--- included. Metadata from include files is discarded. | |
--- | |
--- Example: | |
--- | |
--- ``` {.include} | |
--- chapters/introduction.md | |
--- chapters/methods.md | |
--- chapters/results.md | |
--- chapters/discussion.md | |
--- ``` | |
local List = require 'pandoc.List' | |
function CodeBlock(cb) | |
if cb.classes:includes 'include' then | |
local blocks = List:new() | |
for line in cb.text:gmatch('[^\n]+') do | |
if line:sub(1,1) ~= '#' then | |
local fh = io.open(line) | |
blocks:extend(pandoc.read (fh:read '*a').blocks) | |
fh:close() | |
end | |
end | |
return blocks | |
end | |
end |
Does this work?
function CodeBlock(cb)
local blocks = List:new()
if cb.classes:includes'include' then
if not FORMAT:match 'html' then
return {}
end
for line in cb.text:gmatch('[^\n]+') do
if line:sub(1,1)~='#' then
local fh = io.open(line)
blocks:extend(pandoc.read (fh:read '*a').blocks)
fh:close()
end
end
end
return blocks
end
It includes files only if the target format is HTML and deletes the block otherwise.
I added this in front of the code:
local List = require 'pandoc.List'
I don't see any change in the output for HTML or man pages.
Could you send me a reproducible example as zip? My mail is in on my profile page.
Will do tonighttomorrow, I'm headed out the door now for the rest of the day. Thanks!
Concluding this dialog for posterity, @tarleb and I conversed offline and he came up with a modification to his lua plugin, and a couple more lua plugins.
include.lua
--- Pandoc Lua filter to include other Markdown files
---
--- Usage: Use a special code block with class `include` to
--- include Markdown files. Each code line is treated as the
--- filename of a Markdown file, parsed as Markdown, and
--- included. Metadata from include files is discarded.
---
--- Example:
---
--- ``` {.include}
--- chapters/introduction.md
--- chapters/methods.md
--- chapters/results.md
--- chapters/discussion.md
--- ```
local List = require 'pandoc.List'
function CodeBlock(cb)
if cb.classes:includes'include' then
local blocks = List:new()
for line in cb.text:gmatch('[^\n]+') do
if line:sub(1,1)~='#' then
local fh = io.open(line)
blocks:extend(pandoc.read (fh:read '*a').blocks)
fh:close()
end
end
return blocks
end
end
delink.lua
delink.lua
is shorter and faster than delink.hs
, and doesn't require Haskell to be installed.
function Link (link)
return link.content
end
no-includes.lua
This no-includes.lua
filter removes all include
blocks. The filter is used when targeting man output.
Using include.lua
would also work, but this filter is faster.
function CodeBlock (cb)
if cb.classes:includes 'include' then
return {}
end
end
I also updated my bash script to use the plugins:
#!/bin/bash
function pandocHtml {
# Reads Pandoc markdown version, writes man page
# $1 - filename, including file type, which should be .md
# $2 - uncompressed output filename, with file type
sudo bash -c "pandoc $1 -s \
--lua-filter=scripts/include.lua \
--lua-filter=scripts/links-to-html.lua \
--css=http://cadenza.micronauticsresearch.com/css/main.css \
--css=http://cadenza.micronauticsresearch.com/css/man.css \
--from markdown \
--to html > \"$2\""
}
function pandocMan {
# Reads Pandoc markdown version, writes man page
# $1 - filename, including file type, which should be .md
# $2 - uncompressed output filename, with file type
sudo bash -c "pandoc $1 -s \
--filter scripts/delink.hs \
--from markdown \
--to man > \"$2\""
}
# Make the script directory current
cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1
pandocHtml cad.md cad.html
pandocMan cad.md cad.1.man
This is a very nice little filter -- thanks!
The filter you show above includes JavaScript without any problem when rendering to HTML. I want the filter to do nothing when rendering to a man page. Currently it does not do anything for man pages, but its name appears instead, as I show above.
I edited my post to add a link to the JavaScript, in case you were curious.