提问人:Gill Bates 提问时间:11/15/2023 更新时间:11/25/2023 访问量:68
是否可以使用 Pandoc 删除未使用的脚注引用?
Is it possible to remove unused footnote references using Pandoc?
问:
如果想知道是否可以使用内置功能或自定义过滤器使用 Pandoc 从 Markdown 文档中删除未使用的脚注引用?
答:
我认为你需要创建一个自定义的 Lua 过滤器。Pandoc 没有内置功能。
你可以这样处理
Lua 脚本:创建一个 Pandoc 可以使用的 Lua 脚本 它应该检查文档中的所有脚注引用
筛选逻辑:脚本应识别文本中引用了哪些脚注,并删除所有未使用的脚注
使用 Lua 过滤器运行 Pandoc:使用 Pandoc 处理带有此 Lua 脚本作为过滤器的 Markdown 文件。
主要步骤是 Lua 脚本。
评论
[^blah-blah]
您的问题可能有两个问题:
悬空的笔记
当创建了脚注,但文档内容中没有引用它时,就是这种情况。
我试过了
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 添加一个新选项,以启用此无操作替换作为配置选项。
注意:这纯粹是一个思想实验,未经测试。
评论