{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE NoImplicitPrelude #-}

-- |

-- Module      : $header$

-- Copyright   : (c) Laurent P René de Cotret, 2019 - 2021

-- License     : GNU GPL, version 2 or above

-- Maintainer  : laurent.decotret@outlook.com

-- Stability   : internal

-- Portability : portable

--

-- Rendering gnuplot plots code blocks

module Text.Pandoc.Filter.Plot.Renderers.GNUPlot
  ( gnuplot,
    gnuplotSupportedSaveFormats,
  )
where

import Text.Pandoc.Filter.Plot.Renderers.Prelude

gnuplot :: PlotM (Maybe Renderer)
gnuplot :: PlotM (Maybe Renderer)
gnuplot = do
  Bool
avail <- PlotM Bool
gnuplotAvailable
  if Bool -> Bool
not Bool
avail
    then Maybe Renderer -> PlotM (Maybe Renderer)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Renderer
forall a. Maybe a
Nothing
    else do
      Text
cmdargs <- (Configuration -> Text) -> PlotM Text
forall a. (Configuration -> a) -> PlotM a
asksConfig Configuration -> Text
gnuplotCmdArgs
      Maybe Executable
mexe <- Toolkit -> PlotM (Maybe Executable)
executable Toolkit
GNUPlot
      Maybe Renderer -> PlotM (Maybe Renderer)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Renderer -> PlotM (Maybe Renderer))
-> Maybe Renderer -> PlotM (Maybe Renderer)
forall a b. (a -> b) -> a -> b
$
        Maybe Executable
mexe Maybe Executable
-> (Executable -> Maybe Renderer) -> Maybe Renderer
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \exe :: Executable
exe@(Executable FilePath
_ Text
exename) ->
          Renderer -> Maybe Renderer
forall (m :: * -> *) a. Monad m => a -> m a
return
            Renderer :: Toolkit
-> Executable
-> (FigureSpec -> FilePath -> Text)
-> (OutputSpec -> Text)
-> [SaveFormat]
-> [Text -> CheckResult]
-> Text
-> (Text -> Text)
-> FilePath
-> Renderer
Renderer
              { rendererToolkit :: Toolkit
rendererToolkit = Toolkit
GNUPlot,
                rendererExe :: Executable
rendererExe = Executable
exe,
                rendererCapture :: FigureSpec -> FilePath -> Text
rendererCapture = FigureSpec -> FilePath -> Text
gnuplotCapture,
                rendererCommand :: OutputSpec -> Text
rendererCommand = Text -> Text -> OutputSpec -> Text
gnuplotCommand Text
cmdargs Text
exename,
                rendererSupportedSaveFormats :: [SaveFormat]
rendererSupportedSaveFormats = [SaveFormat]
gnuplotSupportedSaveFormats,
                rendererChecks :: [Text -> CheckResult]
rendererChecks = [Text -> CheckResult]
forall a. Monoid a => a
mempty,
                rendererLanguage :: Text
rendererLanguage = Text
"gnuplot",
                rendererComment :: Text -> Text
rendererComment = Text -> Text -> Text
forall a. Monoid a => a -> a -> a
mappend Text
"# ",
                rendererScriptExtension :: FilePath
rendererScriptExtension = FilePath
".gp"
              }

gnuplotSupportedSaveFormats :: [SaveFormat]
gnuplotSupportedSaveFormats :: [SaveFormat]
gnuplotSupportedSaveFormats = [SaveFormat
PNG, SaveFormat
SVG, SaveFormat
EPS, SaveFormat
GIF, SaveFormat
JPG, SaveFormat
PDF]

gnuplotCommand :: Text -> Text -> OutputSpec -> Text
gnuplotCommand :: Text -> Text -> OutputSpec -> Text
gnuplotCommand Text
cmdargs Text
exe OutputSpec {FilePath
FigureSpec
oFigurePath :: OutputSpec -> FilePath
oScriptPath :: OutputSpec -> FilePath
oFigureSpec :: OutputSpec -> FigureSpec
oFigurePath :: FilePath
oScriptPath :: FilePath
oFigureSpec :: FigureSpec
..} = [st|#{exe} #{cmdargs} -c "#{oScriptPath}"|]

gnuplotAvailable :: PlotM Bool
gnuplotAvailable :: PlotM Bool
gnuplotAvailable = do
  Maybe Executable
mexe <- Toolkit -> PlotM (Maybe Executable)
executable Toolkit
GNUPlot
  case Maybe Executable
mexe of
    Maybe Executable
Nothing -> Bool -> PlotM Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
    Just (Executable FilePath
dir Text
exe) ->
      FilePath -> PlotM Bool -> PlotM Bool
forall a. FilePath -> PlotM a -> PlotM a
withPrependedPath FilePath
dir (PlotM Bool -> PlotM Bool) -> PlotM Bool -> PlotM Bool
forall a b. (a -> b) -> a -> b
$ (RuntimeEnv -> FilePath)
-> StateT PlotState (ReaderT RuntimeEnv IO) FilePath
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks RuntimeEnv -> FilePath
envCWD StateT PlotState (ReaderT RuntimeEnv IO) FilePath
-> (FilePath -> PlotM Bool) -> PlotM Bool
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (FilePath -> Text -> PlotM Bool) -> Text -> FilePath -> PlotM Bool
forall a b c. (a -> b -> c) -> b -> a -> c
flip FilePath -> Text -> PlotM Bool
commandSuccess [st|"#{exe}" -h|]

gnuplotCapture :: FigureSpec -> FilePath -> Script
gnuplotCapture :: FigureSpec -> FilePath -> Text
gnuplotCapture = (FigureSpec -> FilePath -> Text) -> FigureSpec -> FilePath -> Text
forall t. (FigureSpec -> t -> Text) -> FigureSpec -> t -> Text
prependCapture FigureSpec -> FilePath -> Text
gnuplotCaptureFragment
  where
    prependCapture :: (FigureSpec -> t -> Text) -> FigureSpec -> t -> Text
prependCapture FigureSpec -> t -> Text
f FigureSpec
s t
fp = [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat [FigureSpec -> t -> Text
f FigureSpec
s t
fp, Text
"\n", FigureSpec -> Text
script FigureSpec
s]

gnuplotCaptureFragment :: FigureSpec -> FilePath -> Script
gnuplotCaptureFragment :: FigureSpec -> FilePath -> Text
gnuplotCaptureFragment FigureSpec {Bool
Int
FilePath
[FilePath]
[(Text, Text)]
Attr
Text
Renderer
SaveFormat
blockAttrs :: FigureSpec -> Attr
extraAttrs :: FigureSpec -> [(Text, Text)]
dependencies :: FigureSpec -> [FilePath]
dpi :: FigureSpec -> Int
directory :: FigureSpec -> FilePath
saveFormat :: FigureSpec -> SaveFormat
withSource :: FigureSpec -> Bool
caption :: FigureSpec -> Text
renderer_ :: FigureSpec -> Renderer
blockAttrs :: Attr
extraAttrs :: [(Text, Text)]
dependencies :: [FilePath]
dpi :: Int
directory :: FilePath
saveFormat :: SaveFormat
script :: Text
withSource :: Bool
caption :: Text
renderer_ :: Renderer
script :: FigureSpec -> Text
..} FilePath
fname =
  [st|
set terminal #{terminalString saveFormat}
set output '#{fname}'
|]

-- | Terminal name for supported save formats

terminalString :: SaveFormat -> Text
terminalString :: SaveFormat -> Text
terminalString SaveFormat
PNG = Text
"pngcairo"
terminalString SaveFormat
SVG = Text
"svg"
terminalString SaveFormat
EPS = Text
"postscript eps"
terminalString SaveFormat
GIF = Text
"gif"
terminalString SaveFormat
JPG = Text
"jpeg"
terminalString SaveFormat
PDF = Text
"pdfcairo"
terminalString SaveFormat
fmt = FilePath -> Text
forall a. FilePath -> a
errorWithoutStackTrace (FilePath -> Text) -> FilePath -> Text
forall a b. (a -> b) -> a -> b
$ FilePath
"gnuplot: unsupported save format" FilePath -> FilePath -> FilePath
forall a. Semigroup a => a -> a -> a
<> SaveFormat -> FilePath
forall a. Show a => a -> FilePath
show SaveFormat
fmt