{-# 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 <- forall a. (Configuration -> a) -> PlotM a
asksConfig Configuration -> Text
gnuplotCmdArgs
      forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
        Renderer
          { rendererToolkit :: Toolkit
rendererToolkit = Toolkit
GNUPlot,
            rendererCapture :: FigureSpec -> [Char] -> Text
rendererCapture = FigureSpec -> [Char] -> Text
gnuplotCapture,
            rendererCommand :: OutputSpec -> Text
rendererCommand = Text -> OutputSpec -> Text
gnuplotCommand Text
cmdargs,
            rendererAvailability :: AvailabilityCheck
rendererAvailability = (Executable -> Text) -> AvailabilityCheck
CommandSuccess forall a b. (a -> b) -> a -> b
$ \Executable
exe -> [st|#{pathToExe exe} -h|],
            rendererSupportedSaveFormats :: [SaveFormat]
rendererSupportedSaveFormats = [SaveFormat]
gnuplotSupportedSaveFormats,
            rendererChecks :: [Text -> CheckResult]
rendererChecks = forall a. Monoid a => a
mempty,
            rendererLanguage :: Text
rendererLanguage = Text
"gnuplot",
            rendererComment :: Text -> Text
rendererComment = forall a. Monoid a => a -> a -> a
mappend Text
"# ",
            rendererScriptExtension :: [Char]
rendererScriptExtension = [Char]
".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 {[Char]
FigureSpec
Executable
oCWD :: OutputSpec -> [Char]
oExecutable :: OutputSpec -> Executable
oFigurePath :: OutputSpec -> [Char]
oScriptPath :: OutputSpec -> [Char]
oFigureSpec :: OutputSpec -> FigureSpec
oCWD :: [Char]
oExecutable :: Executable
oFigurePath :: [Char]
oScriptPath :: [Char]
oFigureSpec :: FigureSpec
..} = [st|#{pathToExe oExecutable} #{cmdargs} -c "#{oScriptPath}"|]

gnuplotCapture :: FigureSpec -> FilePath -> Script
gnuplotCapture :: FigureSpec -> [Char] -> Text
gnuplotCapture = forall {t}. (FigureSpec -> t -> Text) -> FigureSpec -> t -> Text
prependCapture FigureSpec -> [Char] -> Text
gnuplotCaptureFragment
  where
    prependCapture :: (FigureSpec -> t -> Text) -> FigureSpec -> t -> Text
prependCapture FigureSpec -> t -> Text
f FigureSpec
s t
fp = 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 -> [Char] -> Text
gnuplotCaptureFragment FigureSpec {Bool
Int
[Char]
[[Char]]
[(Text, Text)]
Attr
Text
Renderer
SaveFormat
Executable
blockAttrs :: FigureSpec -> Attr
extraAttrs :: FigureSpec -> [(Text, Text)]
dependencies :: FigureSpec -> [[Char]]
dpi :: FigureSpec -> Int
directory :: FigureSpec -> [Char]
saveFormat :: FigureSpec -> SaveFormat
withSource :: FigureSpec -> Bool
caption :: FigureSpec -> Text
fsExecutable :: FigureSpec -> Executable
renderer_ :: FigureSpec -> Renderer
blockAttrs :: Attr
extraAttrs :: [(Text, Text)]
dependencies :: [[Char]]
dpi :: Int
directory :: [Char]
saveFormat :: SaveFormat
script :: Text
withSource :: Bool
caption :: Text
fsExecutable :: Executable
renderer_ :: Renderer
script :: FigureSpec -> Text
..} [Char]
fname =
  [st|
set terminal #{terminalString saveFormat}
set output '#{normalizePath fname}'
|]
  where
    normalizePath :: [Char] -> [Char]
normalizePath = 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 = forall a. [Char] -> a
errorWithoutStackTrace forall a b. (a -> b) -> a -> b
$ [Char]
"gnuplot: unsupported save format" forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> [Char]
show SaveFormat
fmt