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

-- |
-- Module      : $header$
-- Copyright   : (c) Laurent P René de Cotret, 2019 - present
-- 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 Renderer
gnuplot :: PlotM Renderer
gnuplot = do
      Text
cmdargs <- (Configuration -> Text) -> PlotM Text
forall a. (Configuration -> a) -> PlotM a
asksConfig Configuration -> Text
gnuplotCmdArgs
      Renderer -> PlotM Renderer
forall (m :: * -> *) a. Monad m => a -> m a
return (Renderer -> PlotM Renderer) -> Renderer -> PlotM Renderer
forall a b. (a -> b) -> a -> b
$
        Renderer :: Toolkit
-> (FigureSpec -> FilePath -> Text)
-> (OutputSpec -> Text)
-> AvailabilityCheck
-> [SaveFormat]
-> [Text -> CheckResult]
-> Text
-> (Text -> Text)
-> FilePath
-> Renderer
Renderer
          { rendererToolkit :: Toolkit
rendererToolkit = Toolkit
GNUPlot,
            rendererCapture :: FigureSpec -> FilePath -> Text
rendererCapture = FigureSpec -> FilePath -> Text
gnuplotCapture,
            rendererCommand :: OutputSpec -> Text
rendererCommand = Text -> OutputSpec -> Text
gnuplotCommand Text
cmdargs,
            rendererAvailability :: AvailabilityCheck
rendererAvailability = (Executable -> Text) -> AvailabilityCheck
CommandSuccess ((Executable -> Text) -> AvailabilityCheck)
-> (Executable -> Text) -> AvailabilityCheck
forall a b. (a -> b) -> a -> b
$ \Executable
exe -> [st|#{pathToExe exe} -h|],
            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
LaTeX, SaveFormat
PNG, SaveFormat
SVG, SaveFormat
EPS, SaveFormat
GIF, SaveFormat
JPG, SaveFormat
PDF]

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

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
Executable
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
fsExecutable :: FigureSpec -> Executable
renderer_ :: FigureSpec -> Renderer
blockAttrs :: Attr
extraAttrs :: [(Text, Text)]
dependencies :: [FilePath]
dpi :: Int
directory :: FilePath
saveFormat :: SaveFormat
script :: Text
withSource :: Bool
caption :: Text
fsExecutable :: Executable
renderer_ :: Renderer
script :: FigureSpec -> Text
..} FilePath
fname =
  [st|
set terminal #{terminalString saveFormat}
set output '#{normalizePath fname}'
|]
  where
    normalizePath :: FilePath -> FilePath
normalizePath = (Char -> Char) -> FilePath -> FilePath
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
f
      where
        f :: Char -> Char
f Char
'\\' = Char
'/'
        f Char
x = Char
x

-- | 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
LaTeX = Text
"cairolatex"
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