hakyll-R (empty) → 0.1.0.0
raw patch · 3 files changed
+97/−0 lines, 3 filesdep +basedep +binarydep +bytestringsetup-changed
Dependencies added: base, binary, bytestring, directory, filepath, hakyll, pandoc, process
Files
- Setup.hs +2/−0
- hakyll-R.cabal +24/−0
- src/Hakyll/Web/R.hs +71/−0
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ hakyll-R.cabal view
@@ -0,0 +1,24 @@+name: hakyll-R+version: 0.1.0.0+synopsis: A module allowing to write Hakyll blog posts in Rmd +description: This package declares a Compiler for Rmd posts, which allows to include R code in Rmd and have them rendered (for example as plots).+license: BSD3 +author: Corentin Dupont+maintainer: corentin.dupont@gmail.com+category: Web+build-type: Simple+cabal-version: >=1.10++library+ exposed-modules: Hakyll.Web.R + build-depends: base >=4.7 && <4.8,+ hakyll -any,+ process -any,+ filepath -any,+ pandoc -any,+ directory -any,+ bytestring -any,+ binary+ hs-source-dirs: src+ ghc-options: -Wall -threaded+ default-language: Haskell2010
+ src/Hakyll/Web/R.hs view
@@ -0,0 +1,71 @@+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE DoAndIfThenElse #-}+{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE DeriveDataTypeable #-}++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++--Compile the underlying Rmd file and returns its content+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)+ -- convert Rmd to md+ mdContent <- rMarkdown (takeFileName abfp)+ -- get html file from md+ let html = (writePandocWith wopt (readMarkdown ropt <$> item {itemBody = mdContent}))+ -- make the html self-contained (imgs are embedded as data URIs)+ 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+++--get the markdown content from an R markdown file+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