{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE FlexibleContexts #-}
module Text.Pandoc.Filter.Plot (
makePlot
, plotTransform
, cleanOutputDirs
, configuration
, defaultConfiguration
, Configuration(..)
, Verbosity(..)
, LogSink(..)
, SaveFormat(..)
, Script
, availableToolkits
, unavailableToolkits
, make
, makeEither
, PandocPlotError(..)
, readDoc
) where
import Control.Concurrent.Async (mapConcurrently)
import Data.Text (Text, unpack)
import Text.Pandoc.Definition (Pandoc(..), Block)
import Text.Pandoc.Walk (walkM, Walkable)
import Text.Pandoc.Filter.Plot.Internal
plotTransform :: Configuration
-> Pandoc
-> IO Pandoc
plotTransform :: Configuration -> Pandoc -> IO Pandoc
plotTransform conf :: Configuration
conf (Pandoc meta :: Meta
meta blocks :: [Block]
blocks) =
(Block -> IO Block) -> [Block] -> IO [Block]
forall (t :: * -> *) a b.
Traversable t =>
(a -> IO b) -> t a -> IO (t b)
mapConcurrently (Configuration -> Block -> IO Block
forall a. Walkable Block a => Configuration -> a -> IO a
makePlot Configuration
conf) [Block]
blocks IO [Block] -> ([Block] -> IO Pandoc) -> IO Pandoc
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Pandoc -> IO Pandoc
forall (m :: * -> *) a. Monad m => a -> m a
return (Pandoc -> IO Pandoc)
-> ([Block] -> Pandoc) -> [Block] -> IO Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Meta -> [Block] -> Pandoc
Pandoc Meta
meta
makePlot :: Walkable Block a
=> Configuration
-> a
-> IO a
makePlot :: Configuration -> a -> IO a
makePlot conf :: Configuration
conf = (Block -> IO Block) -> a -> IO a
forall a b (m :: * -> *).
(Walkable a b, Monad m, Applicative m, Functor m) =>
(a -> m a) -> b -> m b
walkM (Configuration -> Block -> IO Block
make Configuration
conf)
make :: Configuration
-> Block
-> IO Block
make :: Configuration -> Block -> IO Block
make conf :: Configuration
conf blk :: Block
blk =
(PandocPlotError -> IO Block)
-> (Block -> IO Block) -> Either PandocPlotError Block -> IO Block
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (IO Block -> PandocPlotError -> IO Block
forall a b. a -> b -> a
const (Block -> IO Block
forall (m :: * -> *) a. Monad m => a -> m a
return Block
blk) ) Block -> IO Block
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PandocPlotError Block -> IO Block)
-> IO (Either PandocPlotError Block) -> IO Block
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Configuration -> Block -> IO (Either PandocPlotError Block)
makeEither Configuration
conf Block
blk
makeEither :: Configuration
-> Block
-> IO (Either PandocPlotError Block)
makeEither :: Configuration -> Block -> IO (Either PandocPlotError Block)
makeEither conf :: Configuration
conf block :: Block
block = Configuration
-> PlotM (Either PandocPlotError Block)
-> IO (Either PandocPlotError Block)
forall a. Configuration -> PlotM a -> IO a
runPlotM Configuration
conf (Block -> PlotM (Either PandocPlotError Block)
go Block
block)
where
go :: Block -> PlotM (Either PandocPlotError Block)
go :: Block -> PlotM (Either PandocPlotError Block)
go blk :: Block
blk = Block -> PlotM (Maybe FigureSpec)
parseFigureSpec Block
blk
PlotM (Maybe FigureSpec)
-> (Maybe FigureSpec -> PlotM (Either PandocPlotError Block))
-> PlotM (Either PandocPlotError Block)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= PlotM (Either PandocPlotError Block)
-> (FigureSpec -> PlotM (Either PandocPlotError Block))
-> Maybe FigureSpec
-> PlotM (Either PandocPlotError Block)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
(Either PandocPlotError Block
-> PlotM (Either PandocPlotError Block)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PandocPlotError Block
-> PlotM (Either PandocPlotError Block))
-> Either PandocPlotError Block
-> PlotM (Either PandocPlotError Block)
forall a b. (a -> b) -> a -> b
$ Block -> Either PandocPlotError Block
forall a b. b -> Either a b
Right Block
blk)
(\s :: FigureSpec
s -> FigureSpec -> PlotM ScriptResult
runScriptIfNecessary FigureSpec
s PlotM ScriptResult
-> (ScriptResult -> PlotM (Either PandocPlotError Block))
-> PlotM (Either PandocPlotError Block)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= FigureSpec -> ScriptResult -> PlotM (Either PandocPlotError Block)
handleResult FigureSpec
s)
where
handleResult :: FigureSpec -> ScriptResult -> PlotM (Either PandocPlotError Block)
handleResult :: FigureSpec -> ScriptResult -> PlotM (Either PandocPlotError Block)
handleResult spec :: FigureSpec
spec ScriptSuccess = Either PandocPlotError Block
-> PlotM (Either PandocPlotError Block)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PandocPlotError Block
-> PlotM (Either PandocPlotError Block))
-> Either PandocPlotError Block
-> PlotM (Either PandocPlotError Block)
forall a b. (a -> b) -> a -> b
$ Block -> Either PandocPlotError Block
forall a b. b -> Either a b
Right (Block -> Either PandocPlotError Block)
-> Block -> Either PandocPlotError Block
forall a b. (a -> b) -> a -> b
$ Format -> FigureSpec -> Block
toImage (Configuration -> Format
captionFormat Configuration
conf) FigureSpec
spec
handleResult _ (ScriptFailure msg :: Text
msg code :: Int
code) = Either PandocPlotError Block
-> PlotM (Either PandocPlotError Block)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PandocPlotError Block
-> PlotM (Either PandocPlotError Block))
-> Either PandocPlotError Block
-> PlotM (Either PandocPlotError Block)
forall a b. (a -> b) -> a -> b
$ PandocPlotError -> Either PandocPlotError Block
forall a b. a -> Either a b
Left (Text -> Int -> PandocPlotError
ScriptRuntimeError Text
msg Int
code)
handleResult _ (ScriptChecksFailed msg :: Text
msg) = Either PandocPlotError Block
-> PlotM (Either PandocPlotError Block)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PandocPlotError Block
-> PlotM (Either PandocPlotError Block))
-> Either PandocPlotError Block
-> PlotM (Either PandocPlotError Block)
forall a b. (a -> b) -> a -> b
$ PandocPlotError -> Either PandocPlotError Block
forall a b. a -> Either a b
Left (Text -> PandocPlotError
ScriptChecksFailedError Text
msg)
handleResult _ (ToolkitNotInstalled tk' :: Toolkit
tk') = Either PandocPlotError Block
-> PlotM (Either PandocPlotError Block)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PandocPlotError Block
-> PlotM (Either PandocPlotError Block))
-> Either PandocPlotError Block
-> PlotM (Either PandocPlotError Block)
forall a b. (a -> b) -> a -> b
$ PandocPlotError -> Either PandocPlotError Block
forall a b. a -> Either a b
Left (Toolkit -> PandocPlotError
ToolkitNotInstalledError Toolkit
tk')
data PandocPlotError
= ScriptRuntimeError Text Int
| ScriptChecksFailedError Text
| ToolkitNotInstalledError Toolkit
instance Show PandocPlotError where
show :: PandocPlotError -> String
show (ScriptRuntimeError _ exitcode :: Int
exitcode) = "ERROR (pandoc-plot) The script failed with exit code " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
exitcode String -> ShowS
forall a. Semigroup a => a -> a -> a
<> "."
show (ScriptChecksFailedError msg :: Text
msg) = "ERROR (pandoc-plot) A script check failed with message: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
unpack Text
msg String -> ShowS
forall a. Semigroup a => a -> a -> a
<> "."
show (ToolkitNotInstalledError tk :: Toolkit
tk) = "ERROR (pandoc-plot) The " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Toolkit -> String
forall a. Show a => a -> String
show Toolkit
tk String -> ShowS
forall a. Semigroup a => a -> a -> a
<> " toolkit is required but not installed."