2014-10-16 59 views
2

我廣泛使用Markdown和Pandoc。但是,我想生成一個帶有嵌入式鏈接的PDF(與往常一樣),但是如果打印文檔,我想在文檔的末尾添加一個鏈接表。有沒有辦法自動做到這一點?Pandoc:有沒有辦法在markdown中的PDF中包含鏈接的附錄?

Ex。

Title 
----- 


[Python][] is cool! 

... 

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

在那裏我會在我的PDF的東西實際上是獲得一個額外的頁面一樣

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

謝謝!

回答

3

這是用濾波器很容易實現的。

這裏是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>\ 
相關問題