module Hakyll.Web.R where
import Hakyll
import Text.Printf
import System.Process
import Control.Applicative
import System.FilePath
import System.Exit
import Text.Pandoc
import System.Directory as SD
import Text.Pandoc.SelfContained
buildRmd :: Rules ()
buildRmd = do
match "*.Rmd" $ do
route idRoute
compile $ pandocRmdCompiler
pandocRmdCompilerWith :: ReaderOptions -> WriterOptions -> Compiler (Item String)
pandocRmdCompilerWith ropt wopt = do
item <- getResourceBody
if isRmd item
then cached cacheName $ do
fp <- getResourceFilePath
unsafeCompiler $ saveDir $ do
abfp <- canonicalizePath fp
setCurrentDirectory (dropFileName abfp)
mdContent <- rMarkdown (takeFileName abfp)
let html = (writePandocWith wopt (readMarkdown ropt <$> item {itemBody = mdContent}))
html' <- makeSelfContained wopt (itemBody html)
--clean
SD.removeDirectoryRecursive "figure"
return $ item {itemBody = html'}
else pandocCompilerWith ropt wopt where
cacheName = "Rmd.pandocRmdCompilerWith"
pandocRmdCompiler :: Compiler (Item String)
pandocRmdCompiler = pandocRmdCompilerWith defaultHakyllReaderOptions defaultHakyllWriterOptions
rMarkdown :: FilePath -> IO (String)
rMarkdown fp = do
(e,_,_) <- readProcessWithExitCode "R" ["--no-save","--quiet"] $ printf "library(knitr); knit('%s')" fp
if (e==ExitSuccess)
then do
let nf = replaceExtension (takeFileName fp) "md"
content <- readFile nf
removeFile nf
return content
else error "Error while processing Rmd file"
isRmd :: Item a -> Bool
isRmd i = ex == ".Rmd"
where
ex = snd . splitExtension . toFilePath . itemIdentifier $ i
saveDir :: IO a -> IO a
saveDir m = do
origDir <- getCurrentDirectory
m <* setCurrentDirectory origDir