[英]How to apply two separate lua filters to Span elements, in R Markdown?
Using lua filters, I'm trying to allow people to do the following to PDF output from R Markdown:
[like this]{.correction}
[like this]{highlight = "red"}
[like this]{color = "blue"}
我可以使用最后显示的 lua 过滤器使所有这三个成为可能。 但是,我想允许用户关闭 (1)。
所以我需要 (1) 和 (2)+(3) 由单独的.lua文件提供。
但是,当我将代码拆分为两个文件时,我只能获得其中一个的功能。 我认为这是因为我一次只能使用一个针对 Span 元素的过滤器?
非常感谢任何有关解决方案的建议!
这是 lua 过滤器:
Span = function (el)
-- store the attributes for color and highlight
color = el.attributes['color']
highlight = el.attributes['highlight']
-- create a function to check for emptiness
local function isempty(s)
return s == nil or s == ''
end
-- highlight stuff that ends in {.correction}
if el.classes[1] == "correction" then
table.insert(
el.content, 1,
pandoc.RawInline('latex', '\\hl{')
)
table.insert(
el.content,
pandoc.RawInline('latex', '}')
)
end
-- highlight stuff with {highlight = "some-color"}
if not isempty(highlight) then
-- remove highlight attributes
el.attributes['highlight'] = nil
-- encapsulate in latex code
table.insert(
el.content, 1,
pandoc.RawInline('latex', '\\sethlcolor{' ..highlight..'}\\hl{')
)
table.insert(
el.content,
pandoc.RawInline('latex', '}\\sethlcolor{correctioncolor}')
)
end
-- color text with {color = "some-color"}
if not isempty(color) then
-- remove color attributes
el.attributes['color'] = nil
-- encapsulate in latex code
table.insert(
el.content, 1,
pandoc.RawInline('latex', '\\textcolor{'..color..'}{')
)
table.insert(
el.content,
pandoc.RawInline('latex', '}')
)
end
return el.content
end
这将不是在标题中而是在帖子中回答您的问题。
我可以使用最后显示的 lua 过滤器使所有这三个成为可能。 但是,我想允许用户关闭(1)。
您可以在同一个过滤器中使用元数据来执行此操作,该元数据可以允许将值传递给 Lua 过滤器可以使用的 Pandoc。 查看 Pandoc 的元数据
您可以从 R 中的用户那里获取一个值,使用-M
arg(或rmarkdown::pandoc_metadata_arg()
)将其传递给 Pandoc - 然后 Pandoc 就会知道它。 Lua 过滤器可以使用 Meta 进行访问。 您只需要处理执行顺序
例如,此示例将直接在 yaml header 中取值,并将在 Lua 中对其进行处理,以创建一个变量,作为激活或不激活过滤器的一部分。
下面设置为 false 将不会运行 Lua 中的部件。 设置为 true(或默认为不设置)将运行 Lua 中的部件并突出显示。
---
title: several Lua
output:
pdf_document:
keep_tex: true
pandoc_args: ["--lua-filter", "custom.lua"]
extra_dependencies: soul
correction: false
---
# write the lua files
## Correction
```{cat, engine.opts = list(file = "custom.lua")}
local correction_activated = true
Meta = function(m)
if m['correction'] ~= nil then
correction_activated = m['correction']
end
end
Span = function (el)
-- store the attributes for color and highlight
color = el.attributes['color']
highlight = el.attributes['highlight']
-- create a function to check for emptiness
local function isempty(s)
return s == nil or s == ''
end
-- highlight stuff that ends in {.correction}
if correction_activated and el.classes[1] == "correction" then
table.insert(
el.content, 1,
pandoc.RawInline('latex', '\\hl{')
)
table.insert(
el.content,
pandoc.RawInline('latex', '}')
)
end
-- highlight stuff with {highlight = "some-color"}
if not isempty(highlight) then
-- remove highlight attributes
el.attributes['highlight'] = nil
-- encapsulate in latex code
table.insert(
el.content, 1,
pandoc.RawInline('latex', '\\sethlcolor{' ..highlight..'}\\hl{')
)
table.insert(
el.content,
pandoc.RawInline('latex', '}\\sethlcolor{correctioncolor}')
)
end
-- color text with {color = "some-color"}
if not isempty(color) then
-- remove color attributes
el.attributes['color'] = nil
-- encapsulate in latex code
table.insert(
el.content, 1,
pandoc.RawInline('latex', '\\textcolor{'..color..'}{')
)
table.insert(
el.content,
pandoc.RawInline('latex', '}')
)
end
return el.content
end
return {
{ Meta = Meta },
{ Span = Span }
}
```
Something [here]{.correction}
使用我之前提到的内容,如果您更喜欢开关作为 function 格式的参数,您可以直接从 R 中作为元数据传递。
这是一种方法并保留一个过滤器。
现在在几个过滤器中
---
title: several Lua
output:
pdf_document:
keep_tex: true
pandoc_args: ["--lua-filter", "correction.lua", "--lua-filter", "hl-color.lua"]
extra_dependencies: soul
---
# write the lua files
## Correction
```{cat, engine.opts = list(file = "correction.lua")}
Span = function (el)
-- create a function to check for emptiness
local function isempty(s)
return s == nil or s == ''
end
-- highlight stuff that ends in {.correction}
if el.classes[1] == "correction" then
table.insert(
el.content, 1,
pandoc.RawInline('latex', '\\hl{')
)
table.insert(
el.content,
pandoc.RawInline('latex', '}')
)
end
return el
end
```
## Highlight + colors
```{cat, engine.opts = list(file = "hl-color.lua")}
Span = function (el)
-- store the attributes for color and highlight
color = el.attributes['color']
highlight = el.attributes['highlight']
-- create a function to check for emptiness
local function isempty(s)
return s == nil or s == ''
end
-- highlight stuff with {highlight = "some-color"}
if not isempty(highlight) then
-- remove highlight attributes
el.attributes['highlight'] = nil
-- encapsulate in latex code
table.insert(
el.content, 1,
pandoc.RawInline('latex', '\\sethlcolor{' ..highlight..'}\\hl{')
)
table.insert(
el.content,
pandoc.RawInline('latex', '}\\sethlcolor{correctioncolor}')
)
end
-- color text with {color = "some-color"}
if not isempty(color) then
-- remove color attributes
el.attributes['color'] = nil
-- encapsulate in latex code
table.insert(
el.content, 1,
pandoc.RawInline('latex', '\\textcolor{'..color..'}{')
)
table.insert(
el.content,
pandoc.RawInline('latex', '}')
)
end
return el
end
```
Something [here]{.correction}
Something [like this]{highlight="red"}
Or [like this]{color="blue"}
把它放在test.Rmd
尝试
rmarkdown::render("test.Rmd")
rmarkdown::render("test.Rmd", output_options = list(pandoc_args = rmarkdown::pandoc_lua_filter_args("correction.lua")))
rmarkdown::render("test.Rmd", output_options = list(pandoc_args = rmarkdown::pandoc_lua_filter_args("hl-color.lua")))
我希望您能设法将其集成到您的工具中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.