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

时间:2014-10-16 14:23:47

标签: 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

谢谢!

1 个答案:

答案 0 :(得分:4)

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

这是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>\
相关问题