packages feed

R-pandoc 0.1 → 0.2

raw patch · 3 files changed

+36/−22 lines, 3 filesdep ~R-pandocPVP ok

version bump matches the API change (PVP)

Dependency ranges changed: R-pandoc

API changes (from Hackage documentation)

- Text.Pandoc.R: fileAttr :: String
+ Text.Pandoc.R: Above :: Echo
+ Text.Pandoc.R: Below :: Echo
+ Text.Pandoc.R: data Echo
+ Text.Pandoc.R: instance Read Echo
+ Text.Pandoc.R: instance Show Echo
+ Text.Pandoc.R: readEcho :: [(String, String)] -> Maybe Echo
+ Text.Pandoc.R: readImgFiles :: [(String, String)] -> [FilePath]
- Text.Pandoc.R: insertRplots :: FilePath -> Block -> IO Block
+ Text.Pandoc.R: insertRplots :: FilePath -> Bool -> Block -> IO Block
- Text.Pandoc.R: moveFiles :: [FilePath] -> FilePath -> IO [FilePath]
+ Text.Pandoc.R: moveFiles :: [FilePath] -> FilePath -> IO ()
- Text.Pandoc.R: renderRPandoc :: FilePath -> Pandoc -> IO Pandoc
+ Text.Pandoc.R: renderRPandoc :: FilePath -> Bool -> Pandoc -> IO Pandoc

Files

R-pandoc.cabal view
@@ -1,5 +1,5 @@ name: R-pandoc-version: 0.1+version: 0.2 cabal-version: >=1.10 build-type: Simple license: BSD3@@ -34,7 +34,7 @@     build-depends:         base         >=4.6  && <4.9,         pandoc-types >=1.12 && <1.13,-        R-pandoc     ==0.1+        R-pandoc     ==0.2     main-is: src/Main.hs     buildable: True     default-language: Haskell2010
src/Main.hs view
@@ -2,4 +2,4 @@ import           Text.Pandoc.R  main :: IO ()-main = toJSONFilter (insertRplots "plots")+main = toJSONFilter (insertRplots "plots" False)
src/Text/Pandoc/R.hs view
@@ -15,30 +15,44 @@ import           Text.Pandoc.Generic import           Text.Pandoc.JSON -rClass, fileAttr, defFile, defRplot, tmpRFile :: String+rClass, defFile, defRplot, tmpRFile :: String rClass = "Rplot"-fileAttr = "files" defFile = "Rplot.png" defRplot = "Rplots.pdf" tmpRFile = "plot.R" -renderRPandoc :: FilePath -> Pandoc -> IO Pandoc-renderRPandoc f p = bottomUpM (insertRplots f) p+data Echo = Above | Below+   deriving (Read, Show) -insertRplots :: FilePath -> Block -> IO Block-insertRplots outDir block@(CodeBlock (ident, classes, attrs) code) = do-   --hPutStrLn stderr $ "insertRplots classes: " ++ (show classes) ++ " attrs: " ++ (show attrs) ++ " ident: " ++ (show ident)-   if rClass `elem` classes then do-      let imgFiles = case lookup fileAttr attrs of-           Just is -> splitOn "," is-           Nothing   -> [defFile]-      d <- renderRPlot code-      when (imgFiles == [defFile]) $ void $ convertDefault-      imgFiles' <- moveFiles imgFiles outDir-      return $ if d then Plain (map insertImage imgFiles') else block-   else return block-insertRplots _ block = return block+renderRPandoc :: FilePath -> Bool -> Pandoc -> IO Pandoc+renderRPandoc f absolutePath p = bottomUpM (insertRplots f absolutePath) p +insertRplots :: FilePath -> Bool -> Block -> IO Block+insertRplots outDir absolutePath block@(CodeBlock (ident, classes, attrs) code) | rClass `elem` classes = do+   let imgFiles = readImgFiles attrs+   d <- renderRPlot code+   when (imgFiles == [defFile]) $ void $ convertDefault+   moveFiles imgFiles outDir+   let imgFiles' = map ((if absolutePath then pathSeparator : outDir else outDir) </>) imgFiles+   let imgBlock = Plain (map insertImage imgFiles')+   let codeBlock = CodeBlock (ident, "r":delete "Rplot" classes, attrs) code+   let block' = case readEcho attrs of+        (Just Above) -> Table [] [AlignLeft] [] [] [[[codeBlock]], [[imgBlock]]]+        (Just Below) -> Table [] [AlignLeft] [] [] [[[imgBlock]], [[codeBlock]]]+        Nothing -> imgBlock+   return $ if d then block' else block+insertRplots _ _ block = return block++readEcho :: [(String, String)] -> Maybe Echo+readEcho attrs = case lookup "echo" attrs of+   Just e  -> Just (read e)+   Nothing -> Nothing++readImgFiles :: [(String, String)] -> [FilePath]+readImgFiles attrs = case lookup "files" attrs of+   Just is -> splitOn "," is+   Nothing -> [defFile]+ insertImage :: FilePath -> Inline insertImage file = Image [] (file,"") @@ -58,11 +72,11 @@    whenM (doesFileExist "plot.Rout") $ removeFile "plot.Rout"    return $ (code==ExitSuccess) -moveFiles :: [FilePath] -> FilePath -> IO [FilePath]+moveFiles :: [FilePath] -> FilePath -> IO () moveFiles files outDir = do    createDirectoryIfMissing False outDir    mapM_ (\a -> whenM (doesFileExist a) $ renameFile a (outDir </> a)) files-   return $ map ((pathSeparator : outDir) </>) files+  convertDefault :: IO Bool convertDefault = do