{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes       #-}
{-# LANGUAGE RecordWildCards   #-}
{-|
Module      : $header$
Copyright   : (c) Laurent P René de Cotret, 2020
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 (
      gnuplotSupportedSaveFormats
    , gnuplotCommand
    , gnuplotCapture
    , gnuplotAvailable
) where

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

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


gnuplotCommand :: OutputSpec -> Text -> Text
gnuplotCommand :: OutputSpec -> Text -> Text
gnuplotCommand OutputSpec{FilePath
FigureSpec
oFigurePath :: OutputSpec -> FilePath
oScriptPath :: OutputSpec -> FilePath
oFigureSpec :: OutputSpec -> FigureSpec
oFigurePath :: FilePath
oScriptPath :: FilePath
oFigureSpec :: FigureSpec
..} Text
exe = [st|#{exe} -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 -> Text -> PlotM Bool
commandSuccess FilePath
dir [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
SaveFormat
Toolkit
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
toolkit :: FigureSpec -> Toolkit
blockAttrs :: Attr
extraAttrs :: [(Text, Text)]
dependencies :: [FilePath]
dpi :: Int
directory :: FilePath
saveFormat :: SaveFormat
script :: Text
withSource :: Bool
caption :: Text
toolkit :: Toolkit
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