{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
module Text.Pandoc.Filter.Plot.Renderers.Matplotlib (
matplotlibSupportedSaveFormats
, matplotlibCommand
, matplotlibCapture
, matplotlibExtraAttrs
, matplotlibAvailable
, matplotlibCheckIfShow
) where
import Text.Pandoc.Filter.Plot.Renderers.Prelude
import qualified Data.Map.Strict as M
import Data.Monoid (Any(..))
import qualified Data.Text as T
matplotlibSupportedSaveFormats :: [SaveFormat]
matplotlibSupportedSaveFormats :: [SaveFormat]
matplotlibSupportedSaveFormats = [SaveFormat
PNG, SaveFormat
PDF, SaveFormat
SVG, SaveFormat
JPG, SaveFormat
EPS, SaveFormat
GIF, SaveFormat
TIF]
matplotlibCommand :: OutputSpec -> PlotM Text
matplotlibCommand :: OutputSpec -> PlotM Text
matplotlibCommand OutputSpec{..} = do
FilePath
exe <- Toolkit -> PlotM FilePath
executable Toolkit
Matplotlib
Text -> PlotM Text
forall (m :: * -> *) a. Monad m => a -> m a
return [st|#{exe} "#{oScriptPath}"|]
matplotlibCapture :: FigureSpec -> FilePath -> Script
matplotlibCapture :: FigureSpec -> FilePath -> Text
matplotlibCapture FigureSpec{..} fname :: FilePath
fname = [st|
import matplotlib.pyplot as plt
plt.savefig(r"#{fname}", dpi=#{dpi}, transparent=#{transparent}, bbox_inches=#{tightBox})
|]
where attrs :: Map Text Text
attrs = [(Text, Text)] -> Map Text Text
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [(Text, Text)]
extraAttrs
tight_ :: Bool
tight_ = Text -> Bool
readBool (Text -> Bool) -> Text -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> Text -> Map Text Text -> Text
forall k a. Ord k => a -> k -> Map k a -> a
M.findWithDefault "False" "tight" Map Text Text
attrs
transparent_ :: Bool
transparent_ = Text -> Bool
readBool (Text -> Bool) -> Text -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> Text -> Map Text Text -> Text
forall k a. Ord k => a -> k -> Map k a -> a
M.findWithDefault "False" "transparent" Map Text Text
attrs
tightBox :: Text
tightBox = if Bool
tight_ then ("'tight'"::Text) else ("None"::Text)
transparent :: Text
transparent = if Bool
transparent_ then ("True"::Text) else ("False"::Text)
matplotlibExtraAttrs :: M.Map Text Text -> (M.Map Text Text)
kv :: Map Text Text
kv = (Text -> Text -> Bool) -> Map Text Text -> Map Text Text
forall k a. (k -> a -> Bool) -> Map k a -> Map k a
M.filterWithKey (\k :: Text
k _ -> Text
k Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ["tight_bbox", "transparent"]) Map Text Text
kv
matplotlibAvailable :: PlotM Bool
matplotlibAvailable :: PlotM Bool
matplotlibAvailable = do
FilePath
exe <- Toolkit -> PlotM FilePath
executable Toolkit
Matplotlib
Text -> PlotM Bool
commandSuccess [st|#{exe} -c "import matplotlib"|]
matplotlibCheckIfShow :: Script -> CheckResult
matplotlibCheckIfShow :: Text -> CheckResult
matplotlibCheckIfShow s :: Text
s =
if Any -> Bool
getAny (Any -> Bool) -> Any -> Bool
forall a b. (a -> b) -> a -> b
$ [Any] -> Any
forall a. Monoid a => [a] -> a
mconcat [Any]
showPresent
then Text -> CheckResult
CheckFailed "encountered a call to `matplotlib.pyplot.show`."
else CheckResult
CheckPassed
where
showPresent :: [Any]
showPresent = (\n :: Text
n -> Bool -> Any
Any (Text -> Text -> Bool
T.isInfixOf Text
n Text
s)) (Text -> Any) -> [Text] -> [Any]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [
"matplotlib.pyplot.show()"
, "pyplot.show()"
, "plt.show()"
]
readBool :: Text -> Bool
readBool :: Text -> Bool
readBool s :: Text
s | Text
s Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ["True", "true", "'True'", "'true'", "1"] = Bool
True
| Text
s Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ["False", "false", "'False'", "'false'", "0"] = Bool
False
| Bool
otherwise = FilePath -> Bool
forall a. HasCallStack => FilePath -> a
error (FilePath -> Bool) -> FilePath -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> FilePath
unpack (Text -> FilePath) -> Text -> FilePath
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat ["Could not parse '", Text
s, "' into a boolean. Please use 'True' or 'False'"]