【问题标题】:Pandoc filter in Lua to alter text that are not headingsLua 中的 Pandoc 过滤器以更改不是标题的文本
【发布时间】:2021-09-04 10:51:30
【问题描述】:

我正在为 pandoc 编写一个 Lua 过滤器,它将词汇表函数添加到 Markdown 文件的 HTML 输出。目标是在文档中每次出现的首字母缩写词或键定义中添加鼠标悬停文本。

但是,我不希望标题中的文本出现这种情况。

我的 MWE 适用于文档中的大多数*文本:

-- Parse glossary file (summarised here for brevity)
local glossary = {CO = "Cardiac Output", DBP = "Diastolic Blood Pressure", SBP = "Systolic Blood Pressure"}

-- Substitute glossary term for span with a mouseover link
function Str(elem)
  for key, value in next, glossary do
    if elem.text == key then
      return pandoc.Span (key, {title = value, class = "glossary"})
    end
  end
end

我对文档的理解和对 AST 的了解告诉我,我需要先使用块级函数,然后再使用 walk_block 来更改内联元素。

function Pandoc(doc)
  for i, el in pairs(doc.blocks) do
    if (el.t ~= "Header") then
      return pandoc.walk_block(el, {
        Str = function (el)
          for key, value in next, glossary do
            if el.text == key then
              return pandoc.Span (key, {title = value, class = "glossary"})
            end
          end
        end })
    end
  end
end

但是,此尝试不起作用并返回错误:“尝试从 Lua 堆栈获取过滤器的返回值时出错。 PandocLuaError“无法获取 Pandoc 值:预期的表,得到了 'nil' (nil)”。我认为我的返回结构是错误的,但我一直无法调试它。


我的测试降价文件包含:

# Acronyms: SBP, DBP & CO

Spaced acronyms: CO and SBP and DBP.

In a comma-separated list: CO, SBP, DBP; with backslashes; CO/DBP/SBP, and in bullet points:
  
* CO
* SBP
* DBP

*它在非空格相邻字符的术语上失败,例如标点符号。

【问题讨论】:

    标签: lua pandoc


    【解决方案1】:

    又过了几天,我找到了一个部分解决方案,可以帮助其他有类似问题的人。

    我认为(但不确定)Pandoc(doc) 需要返回块元素列表和doc.meta,我在上面没有这样做。

    我的解决方案是将词汇表函数分离出来,然后为每个所需的块元素单独调用它。这行得通,尽管它有点笨重。

    function glos_sub (el)
      return pandoc.walk_block(el, {
        Str = function (el)
          for key, value in next, glossary do
            if el.text == key then
              return pandoc.Span (key, {title = value, class = "glossary"})
            end
          end
        end
      })
    end
    
    -- Run on desired elements
    return {
      {BulletList = glos_sub},
      {Para = glos_sub},
      {Table = glos_sub}
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-04
      • 1970-01-01
      相关资源
      最近更新 更多