pdf - Pandoc:有没有办法在 PDF 中包含来自 markdown 的链接附录?

标签 pdf markdown pandoc

我广泛使用 Markdown 和 Pandoc。但是,我想生成一个带有嵌入式链接的 PDF(像往常一样),但如果打印文档,我还想在文档末尾包含一个链接表。有没有办法自动执行此操作?

例如

Title
-----


[Python][] is cool!

...

## Links ##
[Python]: http://python.org
[Pip]: https://pip.readthedocs.org

我实际上会在我的 PDF 中得到一个额外的页面,比如

Python: http://python.org
Pip: https://pip.readthedocs.org

谢谢!

最佳答案

使用过滤器很容易实现这一点。

这里是linkTable.hs。将链接表添加到文档末尾的过滤器。

import Text.Pandoc.JSON
import Text.Pandoc.Walk
import Data.Monoid

main :: IO ()
main = toJSONFilter appendLinkTable

appendLinkTable :: Pandoc -> Pandoc
appendLinkTable (Pandoc m bs) = Pandoc m (bs ++ linkTable bs)

linkTable :: [Block] -> [Block]
linkTable p = [Header 2 ("linkTable", [], []) [Str "Links"] , Para links]
  where
    links = concatMap makeRow $ query getLink p
    getLink (Link txt (url, _)) = [(url,txt)]
    getLink _ = []
    makeRow (url, txt) = txt ++ [Str ":", Space, Link [Str url] (url, ""), LineBreak]

使用 ghc linkTable.hs 编译过滤器。输出结果如下。

> ghc linkTable.hs 
[1 of 1] Compiling Main             ( linkTable.hs, linkTable.o )
Linking linkTable ...

> cat example.md 
Title
-----


[Python][] is cool!

[Pip] is a package manager.

...

[Python]: http://python.org
[Pip]: https://pip.readthedocs.org

然后使用过滤器运行 pandoc

> pandoc -t markdown --filter=./linkTable example.md
Title
-----

[Python](http://python.org) is cool!

[Pip](https://pip.readthedocs.org) is a package manager.

...

Links {#linkTable}
-----

Python: <http://python.org>\
Pip: <https://pip.readthedocs.org>\

关于pdf - Pandoc:有没有办法在 PDF 中包含来自 markdown 的链接附录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26406816/

相关文章:

php - 您将如何自动检测 Textile 与 Markdown?

r - 是否可以将 RMarkdown block 放入 Rmd 文件的表格单元格中?

html - 使用 pandoc 从第一个标题设置 html 标题

markdown - 将 markdown 转换为 reStructured 文本?

c++ - C++/Qt (Windows) 报告生成器

Android:从设备浏览并上传应用程序中的 PDF 或 Word 文件

qt - 如何在 QML 5.14 中使用 Markdown 格式

python - Django / python : Convert Markdown to Secure HTML

c# - 使用免费的 C# 解决方案将 pdf 转换为 jpeg

php - 像 Verisign/Docusign 一样非常非常快地将 PDF 转换为 PNG