2013-06-12 12 views
0

我正在閱讀模板文件,將降價文件轉換爲html文件並嘗試讓IO與純系統搭配使用。如何在使用IO字符串模板時使用Pandoc的writeHtmlString

template :: IO String 
template = readFile "/File/Path/template.html" 

siteOptions :: WriterOptions 
siteOptions = def { writerStandalone = True, writerTemplate = template } 

convertMdtoHtml :: FilePath -> IO() 
convertMdtoHtml file = do 
    contents <- readFile file 
    let pandoc = readMarkdown def contents 
    let html = writeHtmlString siteOptions pandoc 
    writeFile (file ++ ".html") html 

下面是writeHtmlString,我試圖用http://hackage.haskell.org/packages/archive/pandoc/1.11.1/doc/html/Text-Pandoc-Writers-HTML.html

試圖運行,這是

Couldn't match expected type `String' with actual type `IO String' 

,當我得到的錯誤的文檔有任何方式做到這一點haskell還是我需要將模板文件作爲字符串已經在代碼中。

謝謝

回答

2

templatesiteOptions一個參數:

siteOptions :: String -> WriterOptions 
siteOptions template = def { writerStandalone = True, writerTemplate = template } 

convertMdtoHtml :: FilePath -> IO() 
convertMdtoHtml file = do 
    ... 
    template <- readFile "/File/Path/template.html" 
    let html = writeHtmlString (siteOptions template) pandoc 

template :: IO StringIO動作 - 一塊不純的(副作用的)代碼,當被執行時,將產生String類型的結果。這就是爲什麼你不能在需要String的環境中使用它。

如果你想在包含編譯時的"/File/Path/template.html"程序中的內容,可以考慮使用Template Haskell

> :set -XTemplateHaskell 
> import Language.Haskell.TH.Syntax 
> import Language.Haskell.TH.Lib 
> let str = $(stringE =<< (runIO (readFile "/path/to/foo"))) 
> str 
"bar\n" 
> :t str 
str :: [Char] 
+0

感謝米哈伊爾回答這麼快。我認爲細節很多,忘記了整體情況。感謝TemplateHaskell上的其他細節,我認爲這樣的東西對我所要做的事情來說太過分了。 – Lazydancer

相關問題