0

有人可以帮我编写一个 lua 过滤器,它在 html 页面中的所有 div 上运行,提取具有类“bibliographie”的过滤器并插入处理后的书目(index.bib 的内容)?

我已经尝试过了,但我离我想去的地方还很远。提前谢谢了 !

YAML 的一部分:

bibliography: index.bib

template.html 的一部分:

<div class="bibliographie">
<h2 class="sources-def-bib-title">Bibliographie</h2>
</div>

和我的 lua 脚本:

function Pandoc(doc)
    local hblocks = {}
    for i,el in pairs(doc.blocks) do
        if (el.t == "Div" and el.classes[1] == "bibliographie") then
           table.insert(meta.bibliography, value)
        end
    end
    return pandoc.Pandoc(hblocks, doc.meta)
end

编辑

我们正在开发一个 R 包,这里是我们使用的 pandoc_args:

pandoc_args <- c(pandoc_metadata_arg("lang", "fr"), pandoc_args)
# use the non-breaking space pandoc lua filter
pandoc_args <- c(nbsp_filter_pandoc_args(), pandoc_args)
# hyphenations
pandoc_args <- c(pandoc_metadata_arg("hyphenopoly"), pandoc_args)
4

1 回答 1

1

一步一步来看,我们首先要通过过滤所有div元素来提取div内容;Div为此可以使用过滤器函数,将内容存储在局部变量中。然后,我们需要将提取的内容添加到元数据中;Lua 过滤器可以使用一个Meta函数来访问和修改元数据。如果没有给出明确的顺序,pandoc 将首先过滤块,然后是元数据,这正是我们想要的。

-- This will contain the contents of the div with id "bibliographie".
local bibliographie

function Div (div)
  if div.classes[1] == 'bibliographie' then
    bibliographie = div.content
    return {} -- Removes the item from the document.
              -- Drop these lines to keep the div.
  end
end

function Meta (meta)
  meta.bibliographie = bibliographie
  return meta
end

这假设pandoc-citeproc已经运行,所以 Lua 过滤器必须在 pandoc-citeproc 过滤器之后给出:--filter pandoc-citeproc --lua-filter bibliographie-to-meta.lua

如果需要,官方文档会提供所有详细信息。

于 2020-06-07T06:00:34.753 回答