我想在markdown文档中替换Table块,RawBlocks最终将包含latex。
我可以使用以下方法找到文档中的表格块:
function Table(table)
但是我需要将每个表块转换为json或本机文本,因此可以将其转换为:
local function table2latex(t)
local latex = pandoc.pipe('pandoc',{'-f','native','-t','latex'},t)
...
我找不到任何进行此类转换的示例,已经搜索了pandoc文档和其他网站,例如github和stackoverflow。
是否有可能或者我需要编写一个定制的嵌入式编写器?
修改
继续。下面的答案导致了以下脚本:
-- file: boxtables.lua
--
-- This filter extracts tables and converts them to latex
-- The latex is then altered by a sed script to "box" each cell
-- using "|" and '\hline' as column and row delimiters.
-- The original document containing the table(s) must have
-- 'header-includes' containing '\usepackage{longtable,booktabs)}'
-- The user should have the `jq`and 'sed' utilities installed and the shell
-- script 'table2latex.sh' executable in the current working directory
--
-- #!/bin/sh
-- latex="$(pandoc -f json -t latex)"
-- tmpl='{blocks:[{t:"RawBlock",c:["latex",$tab]}],"pandoc-api-version":[1,17,3],meta:{}}'
-- jq -n --arg tab "$latex" $tmpl
--
-- The lua filter can be invoked:
-- pandoc --lua-filter boxtables.lua -o documentContainingTable.pdf documentContainingTable.md
--
-- N.B. 'header-includes' and other details may be set on the command line or as a YAML header e.g.
--
-- ---
-- geometry: landscape, scale=0.9, centering
-- header-includes:
-- - \usepackage{longtable,booktabs}
-- ---
--
function table (tab)
-- create a new pandoc document containing only the table
local dummy_doc = pandoc.Pandoc(tab)
-- use table2latex.sh to convert the doc to latex
local tex_doc = pandoc.utils.run_json_filter(dummy_doc, 'table2latex.sh')
-- only a single block containing the table will be returned
return tex_doc.blocks
end
function rawblock (rb)
local oldlatex = rb.text
local sed = [[/^\\begin{longtable}/{s/@{}/&\n/;:a;ta;/\n@{}/!s/\n\(.\)/|\1\n/;ta;s/\n/|/};s/toprule/hline/;/mid\|bottom/d;/tabularnewline/a\\\hline]]
local newlatex = pandoc.pipe('sed',{sed},oldlatex)
return pandoc.RawBlock('latex', newlatex)
end
return {{Table = table},{RawBlock = rawblock}}
答案 0 :(得分:1)
没有内置功能,因此没有完全可移植的方式来执行此操作。我能想到的最好的方法是使用一个调用JSON过滤器的shell脚本将表转换为latex。以下版本基于pandoc.utils.run_json_filter
,自pandoc 2.1.1起可用。
-- file: table2latex.lua
local utils = require 'pandoc.utils'
function Table (tab)
-- create a new pandoc document containing only the table
local dummy_doc = pandoc.Pandoc(tab)
-- use table2latex.sh to convert the doc to latex
local tex_doc = utils.run_json_filter(dummy_doc, 'table2latex.sh')
-- only a single block containing the table will be returned
return tex_doc.blocks
end
table2latex.sh
辅助过滤器使用jq生成JSON。
#!/bin/sh
latex="$(pandoc -f json -t latex)"
tmpl='{blocks:[{t:"RawBlock",c:["latex",$tab]}],"pandoc-api-version":[1,17,3],meta:{}}'
jq -n --arg tab "$latex" $tmpl