如何在R Markdown中对Span元素应用两个不同的lua过滤器?

使用lua过滤器,我试图让用户可以对R Markdown的PDF输出执行以下操作:

  1. 对文本应用预定义的高亮色[像这样]{.correction}
  2. 对文本应用用户定义的高亮色[像这样]{highlight ="red"}
  3. 对文本应用自定义字体颜色[像这样]{color ="blue"}

我可以使用结尾处显示的lua过滤器实现全部三个操作。但是,我想让用户可以关闭(1)。

所以我需要(1)和(2)+(3)由单独的**.lua**文件提供。

当我将代码拆分为两个文件时,但是我只能获得一个或另一个的功能。我认为这是因为我一次只能使用一个针对Span元素的过滤器

任何解决方案的建议都将不胜感激!

这是lua过滤器:

Span = function (el)
  -- 存储颜色和高亮的属性
  color = el.attributes['color']
  highlight = el.attributes['highlight']

  -- 创建用于检查空值的函数
  local function isempty(s)
    return s == nil or s == ''
  end

  -- 高亮以`.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 = "some-color"}的内容进行高亮处理
  if not isempty(highlight) then
    -- 移除高亮属性
    el.attributes['highlight'] = nil
    -- 将内容封装在latex代码中
    table.insert(
el.content, 1,
      pandoc.RawInline('latex', '\\sethlcolor{' ..highlight..'}\\hl{')
    )
    table.insert(
el.content,
      pandoc.RawInline('latex', '}\\sethlcolor{correctioncolor}')
    )
  end

  -- 用{color = "some-color"}为文本进行着色
  if not isempty(color) then
    -- 移除颜色属性
    el.attributes['color'] = nil
    -- 将内容封装在latex代码中
    table.insert(
el.content, 1,
      pandoc.RawInline('latex', '\\textcolor{'..color..'}{')
    )
    table.insert(
el.content,
      pandoc.RawInline('latex', '}')
    )
  end

  return el.content
end
点赞