{-# LANGUAGE OverloadedStrings #-}
module Text.Pandoc.Filter.Plot.Renderers.Prelude (
module Prelude
, module Text.Pandoc.Filter.Plot.Monad
, Text
, st
, unpack
, commandSuccess
, existsOnPath
, executable
, OutputSpec(..)
, Executable(..)
, appendCapture
, toRPath
) where
import Data.Maybe (isJust)
import Data.Text (Text, unpack, pack)
import System.Directory (findExecutable)
import System.FilePath (splitFileName, isPathSeparator)
import System.Exit (ExitCode(..))
import Text.Shakespeare.Text (st)
import Text.Pandoc.Filter.Plot.Monad
data Executable = Executable FilePath Text
exeFromPath :: FilePath -> Executable
exeFromPath fp = let (dir, name) = splitFileName fp
in Executable dir (pack name)
commandSuccess :: FilePath
-> Text
-> PlotM Bool
commandSuccess fp s = do
(ec, _) <- runCommand fp s
return $ ec == ExitSuccess
existsOnPath :: FilePath -> IO Bool
existsOnPath fp = findExecutable fp >>= fmap isJust . return
tryToFindExe :: String -> IO (Maybe Executable)
tryToFindExe fp = findExecutable fp >>= return . fmap exeFromPath
executable :: Toolkit -> PlotM (Maybe Executable)
executable Matplotlib = asksConfig matplotlibExe >>= liftIO . tryToFindExe
executable PlotlyPython = asksConfig plotlyPythonExe >>= liftIO . tryToFindExe
executable PlotlyR = asksConfig plotlyRExe >>= liftIO . tryToFindExe
executable Matlab = asksConfig matlabExe >>= liftIO . tryToFindExe
executable Mathematica = asksConfig mathematicaExe >>= liftIO . tryToFindExe
executable Octave = asksConfig octaveExe >>= liftIO . tryToFindExe
executable GGPlot2 = asksConfig ggplot2Exe >>= liftIO . tryToFindExe
executable GNUPlot = asksConfig gnuplotExe >>= liftIO . tryToFindExe
executable Graphviz = asksConfig graphvizExe >>= liftIO . tryToFindExe
executable Bokeh = asksConfig bokehExe >>= liftIO . tryToFindExe
executable Plotsjl = asksConfig plotsjlExe >>= liftIO . tryToFindExe
appendCapture :: (FigureSpec -> FilePath -> Script)
-> FigureSpec -> FilePath -> Script
appendCapture f s fp = mconcat [script s, "\n", f s fp]
data OutputSpec = OutputSpec
{ oFigureSpec :: FigureSpec
, oScriptPath :: FilePath
, oFigurePath :: FilePath
}
toRPath :: FilePath -> FilePath
toRPath = fmap (\c -> if isPathSeparator c then '/' else c)