{-# 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 -> PlotM Text
gnuplotCommand :: OutputSpec -> PlotM Text
gnuplotCommand OutputSpec{..} = do
    FilePath
exe <- Toolkit -> PlotM FilePath
executable Toolkit
GNUPlot
    Text -> PlotM Text
forall (m :: * -> *) a. Monad m => a -> m a
return [st|#{exe} -c "#{oScriptPath}"|]


gnuplotAvailable :: PlotM Bool
gnuplotAvailable :: PlotM Bool
gnuplotAvailable = do
    FilePath
exe <- Toolkit -> PlotM FilePath
executable Toolkit
GNUPlot 
    Text -> PlotM Bool
commandSuccess [st|#{exe} -h|]


gnuplotCapture :: FigureSpec -> FilePath -> Script
gnuplotCapture :: FigureSpec -> FilePath -> Text
gnuplotCapture FigureSpec{..} fname :: FilePath
fname = [st|
set terminal #{terminalString saveFormat}
set output '#{fname}'
|]

-- | Terminal name for supported save formats

terminalString :: SaveFormat -> Text
terminalString :: SaveFormat -> Text
terminalString PNG = "pngcairo"
terminalString SVG = "svg"
terminalString EPS = "postscript eps"
terminalString GIF = "gif"
terminalString JPG = "jpeg"
terminalString PDF = "pdfcairo"
terminalString fmt :: SaveFormat
fmt = FilePath -> Text
forall a. HasCallStack => FilePath -> a
error (FilePath -> Text) -> FilePath -> Text
forall a b. (a -> b) -> a -> b
$ "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