@@ -27,16 +27,14 @@ module Text.Pandoc.Filter.Plot.Renderers
2727where
2828
2929import Control.Concurrent.Async.Lifted (forConcurrently )
30- import Control.Concurrent.MVar (putMVar , takeMVar )
3130import Control.Monad.Reader (local )
32- import Control.Monad.State.Strict
33- ( MonadState (get , put ),
34- )
31+ import Data.Functor ((<&>) )
3532import Data.List ((\\) )
3633import Data.Map.Strict (Map )
3734import qualified Data.Map.Strict as M
3835import Data.Maybe (catMaybes , isJust )
3936import Data.Text (Text , pack )
37+ import System.Exit (ExitCode (.. ))
4038import Text.Pandoc.Filter.Plot.Monad
4139import Text.Pandoc.Filter.Plot.Monad.Logging
4240 ( Logger (lVerbosity ),
@@ -67,41 +65,25 @@ import Text.Pandoc.Filter.Plot.Renderers.Plotsjl
6765 ( plotsjl , plotsjlSupportedSaveFormats )
6866import Text.Pandoc.Filter.Plot.Renderers.SageMath
6967 ( sagemath , sagemathSupportedSaveFormats )
68+ import System.Directory (findExecutable )
7069
7170-- | Get the renderer associated with a toolkit.
7271-- If the renderer has not been used before,
7372-- initialize it and store where it is. It will be re-used.
74- renderer :: Toolkit -> PlotM (Maybe Renderer )
75- renderer tk = do
76- PlotState varHashes varRenderers <- get
77- renderers <- liftIO $ takeMVar varRenderers
78- (r', rs') <- case M. lookup tk renderers of
79- Nothing -> do
80- debug $ mconcat [" Looking for renderer for " , pack $ show tk]
81- r' <- sel tk
82- let rs' = M. insert tk r' renderers
83- return (r', rs')
84- Just e -> do
85- debug $ mconcat [" Renderer for \" " , pack $ show tk, " \" already initialized." ]
86- return (e, renderers)
87- liftIO $ putMVar varRenderers rs'
88- put $ PlotState varHashes varRenderers
89- return r'
90- where
91- sel :: Toolkit -> PlotM (Maybe Renderer )
92- sel Matplotlib = matplotlib
93- sel PlotlyPython = plotlyPython
94- sel PlotlyR = plotlyR
95- sel Matlab = matlab
96- sel Mathematica = mathematica
97- sel Octave = octave
98- sel GGPlot2 = ggplot2
99- sel GNUPlot = gnuplot
100- sel Graphviz = graphviz
101- sel Bokeh = bokeh
102- sel Plotsjl = plotsjl
103- sel PlantUML = plantuml
104- sel SageMath = sagemath
73+ renderer :: Toolkit -> PlotM Renderer
74+ renderer Matplotlib = matplotlib
75+ renderer PlotlyPython = plotlyPython
76+ renderer PlotlyR = plotlyR
77+ renderer Matlab = matlab
78+ renderer Mathematica = mathematica
79+ renderer Octave = octave
80+ renderer GGPlot2 = ggplot2
81+ renderer GNUPlot = gnuplot
82+ renderer Graphviz = graphviz
83+ renderer Bokeh = bokeh
84+ renderer Plotsjl = plotsjl
85+ renderer PlantUML = plantuml
86+ renderer SageMath = sagemath
10587
10688-- | Save formats supported by this renderer.
10789supportedSaveFormats :: Toolkit -> [SaveFormat ]
@@ -157,14 +139,29 @@ unavailableToolkits conf = runPlotM Nothing conf unavailableToolkitsM
157139availableToolkitsM :: PlotM [Toolkit ]
158140availableToolkitsM = asNonStrictAndSilent $ do
159141 mtks <- forConcurrently toolkits $ \ tk -> do
160- available <- isJust <$> renderer tk
161- if available
142+ r <- renderer tk
143+ exe <- executable tk
144+ a <- isAvailable exe (rendererAvailability r)
145+ if a
162146 then return $ Just tk
163147 else return Nothing
164148 return $ catMaybes mtks
165149 where
166150 asNonStrictAndSilent = local (\ (RuntimeEnv f c l d) -> RuntimeEnv f (c{strictMode = False }) (l{lVerbosity = Silent }) d)
167151
152+ -- | Check that the supplied command results in
153+ -- an exit code of 0 (i.e. no errors)
154+ commandSuccess :: Text -> PlotM Bool
155+ commandSuccess s = do
156+ cwd <- asks envCWD
157+ (ec, _) <- runCommand cwd s
158+ debug $ mconcat [" Command " , s, " resulted in " , pack $ show ec]
159+ return $ ec == ExitSuccess
160+
161+ isAvailable :: Executable -> AvailabilityCheck -> PlotM Bool
162+ isAvailable exe (CommandSuccess f) = commandSuccess (f exe)
163+ isAvailable exe (ExecutableExists ) = liftIO $ findExecutable (pathToExe exe) <&> isJust
164+
168165-- | Monadic version of @unavailableToolkits@
169166unavailableToolkitsM :: PlotM [Toolkit ]
170167unavailableToolkitsM = (\\) toolkits <$> availableToolkitsM
0 commit comments