[英]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.