是否可以使用 Pandoc 删除未使用的脚注引用?

Is it possible to remove unused footnote references using Pandoc?

提问人:Gill Bates 提问时间:11/15/2023 更新时间:11/25/2023 访问量:68

问:

这个赏金已经结束了。这个问题的答案有资格获得 +200 声望赏金。赏金宽限期在 18 小时内结束。吉尔·贝茨(Gill Bates)希望引起人们对这个问题的更多关注

如果想知道是否可以使用内置功能或自定义过滤器使用 Pandoc 从 Markdown 文档中删除未使用的脚注引用?

Markdown pandoc 脚注

评论

0赞 Jerome WAGNER 11/25/2023
我给出并回答了悬而未决的脚注(创建了一个脚注,但文档中没有引用它)。这是否与您的问题相对应,或者您是否正在尝试删除悬空的引用(文档中看起来像引用,但引用了不存在的脚注)

答:

0赞 TristanMas 11/20/2023 #1

我认为你需要创建一个自定义的 Lua 过滤器。Pandoc 没有内置功能。

你可以这样处理

  1. Lua 脚本:创建一个 Pandoc 可以使用的 Lua 脚本 它应该检查文档中的所有脚注引用

  2. 筛选逻辑:脚本应识别文本中引用了哪些脚注,并删除所有未使用的脚注

  3. 使用 Lua 过滤器运行 Pandoc:使用 Pandoc 处理带有此 Lua 脚本作为过滤器的 Markdown 文件。

主要步骤是 Lua 脚本。

评论

0赞 Gill Bates 11/22/2023
似乎 Pandoc 过滤器 API 不允许您访问脚注引用,只允许您访问脚注内容。当我使用 Pandoc 将 Markdown 文档转换为 JSON 表示形式时 - 无处可寻引用。[^blah-blah]
0赞 Jerome WAGNER 11/24/2023 #2

您的问题可能有两个问题:

悬空的笔记

当创建了脚注,但文档内容中没有引用它时,就是这种情况。

我试过了

Here is a footnote reference,[^1].

[^1]: Here is the footnote.

[^2]: Here is the unused footnote.

在配置设置为“Markdown to Markdown”的 https://pandoc.org/try/ 上 -pandoc --from markdown --to markdown

结果是

Here is a footnote reference,[^1].

[^1]: Here is the footnote.

带有警告“请注意,在第 5 行第 1 列中定义了键'2',但未使用。

因此,也许您可以先进行此“Markdown 到 Markdown”转换,或者看看“Markdown 到 Markdown”转换如何设法删除未使用的注释。

在 pandoc 源码中,您可以找到

src/文本/Pandoc/Readers/Markdown.hs#L332

-- check for notes with no corresponding note references
checkNotes :: PandocMonad m => MarkdownParser m ()
checkNotes = do
  st <- getState
  let notesUsed = stateNoteRefs st
  let notesDefined = M.keys (stateNotes' st)
  mapM_ (\n -> unless (n `Set.member` notesUsed) $
                case M.lookup n (stateNotes' st) of
                   Just (pos, _) -> report (NoteDefinedButNotUsed n pos)
                   Nothing -> throwError $
                     PandocShouldNeverHappenError "note not found")
         notesDefined

所以我怀疑这不是你的问题,因为 pandoc 似乎会自动处理这种情况。

另一种情况是

悬空参考

当你有一个底部有很多脚注的 Markdown 文档时,你可能希望删除脚注,但引用仍然在文档中。

我试过了

Here is a footnote reference.[^1]

在 pandoc 中进行 Markdown 到 Markdown 的转换,我得到

Here is a footnote reference.\[\^1\]

因此,脚注似乎已转换为文本。

实际上,在 pandoc AST 中,注释和注释引用之间没有区别,因此处理这种情况有点困难。

我能看到的唯一方法是创建一个新的 MarkdownCustom 解析器来修补当前的 Markdown Reader。

src/Text/Pandoc/Readers/Markdown.hs#L2029-L2051

note :: PandocMonad m => MarkdownParser m (F Inlines)
note = try $ do
  guardEnabled Ext_footnotes
  ref <- noteMarker
  updateState $ \st -> st{ stateNoteRefs = Set.insert ref (stateNoteRefs st)
                         , stateNoteNumber = stateNoteNumber st + 1 }
  noteNum <- stateNoteNumber <$> getState
  return $ do
    notes <- asksF stateNotes'
    case M.lookup ref notes of
        Nothing       -> return $ B.str $ "[^" <> ref <> "]"
        Just (_pos, contents) -> do
          st <- askF
          -- process the note in a context that doesn't resolve
          -- notes, to avoid infinite looping with notes inside
          -- notes:
          let contents' = runF contents st{ stateNotes' = M.empty }
          let addCitationNoteNum c@Citation{} =
                c{ citationNoteNum = noteNum }
          let adjustCite (Cite cs ils) =
                Cite (map addCitationNoteNum cs) ils
              adjustCite x = x
          return $ B.note $ walk adjustCite contents'

您需要将return $ B.str $ "[^" <> ref <> "]"return $ B.str $ ""

我认为您目前无法使用内置功能(除了实现新的阅读器)或自定义过滤器来做到这一点。

您可以向 pandoc Markdown Reader 添加一个新选项,以启用此无操作替换作为配置选项。

注意:这纯粹是一个思想实验,未经测试。