将 graphviz 集成到 pandoc
Integrate graphviz into pandoc
我正在尝试使用 dot 命令将 graphviz 集成到 pandoc markdown 中,我想知道最简单的方法是什么。目前我正在创建单独的 .dot
文件,在命令行上使用 dot
命令手动从它们创建图像到一个单独的文件中,最后使用 markdown 的图像语法添加该文件:
![](./path/to/image.png)
但由于点语法比 markdown 更易于阅读,我更愿意将点集成到 markdown 中。感觉应该是可以的,因为在pandoc filters page中这是作为例子给出的。只是我不想自己编写代码,而是想使用一些现成的包。有这样的东西吗?
tarleb 在评论中发布了一个 link 到 https://github.com/pandoc/lua-filters/tree/master/diagram-generator#graphviz,这是一个很大的帮助。使用 lua-filter 确实如此。稍微调整了 linked 脚本以满足我的需要。
脚本现在如下所示:
-- insprired from https://github.com/pandoc/lua-filters/blob/5686d96/diagram-generator/diagram-generator.lua
local dotPath = os.getenv("DOT") or "dot"
local filetype = "svg"
local mimetype = "image/svg+xml"
local function graphviz(code, filetype)
return pandoc.pipe(dotPath, {"-T" .. filetype}, code)
end
function CodeBlock(block)
local converters = {
graphviz = graphviz,
}
local img_converter = converters[block.classes[1]]
if not img_converter then
return nil
end
local success, img = pcall(img_converter, block.text, filetype)
if not success then
io.stderr:write(tostring(img))
io.stderr:write('\n')
error 'Image conversion failed. Aborting.'
end
return pandoc.RawBlock('html', img)
end
return {
{CodeBlock = CodeBlock},
}
然后可以使用 pandoc 选项 --lua-filter
将此过滤器添加到转换中:
pandoc\
slides.md\
-o slides.html\
--lua-filter=codeblock-filter.lua
还在my blog中添加了更多解释。
我正在尝试使用 dot 命令将 graphviz 集成到 pandoc markdown 中,我想知道最简单的方法是什么。目前我正在创建单独的 .dot
文件,在命令行上使用 dot
命令手动从它们创建图像到一个单独的文件中,最后使用 markdown 的图像语法添加该文件:
![](./path/to/image.png)
但由于点语法比 markdown 更易于阅读,我更愿意将点集成到 markdown 中。感觉应该是可以的,因为在pandoc filters page中这是作为例子给出的。只是我不想自己编写代码,而是想使用一些现成的包。有这样的东西吗?
tarleb 在评论中发布了一个 link 到 https://github.com/pandoc/lua-filters/tree/master/diagram-generator#graphviz,这是一个很大的帮助。使用 lua-filter 确实如此。稍微调整了 linked 脚本以满足我的需要。
脚本现在如下所示:
-- insprired from https://github.com/pandoc/lua-filters/blob/5686d96/diagram-generator/diagram-generator.lua
local dotPath = os.getenv("DOT") or "dot"
local filetype = "svg"
local mimetype = "image/svg+xml"
local function graphviz(code, filetype)
return pandoc.pipe(dotPath, {"-T" .. filetype}, code)
end
function CodeBlock(block)
local converters = {
graphviz = graphviz,
}
local img_converter = converters[block.classes[1]]
if not img_converter then
return nil
end
local success, img = pcall(img_converter, block.text, filetype)
if not success then
io.stderr:write(tostring(img))
io.stderr:write('\n')
error 'Image conversion failed. Aborting.'
end
return pandoc.RawBlock('html', img)
end
return {
{CodeBlock = CodeBlock},
}
然后可以使用 pandoc 选项 --lua-filter
将此过滤器添加到转换中:
pandoc\
slides.md\
-o slides.html\
--lua-filter=codeblock-filter.lua
还在my blog中添加了更多解释。