{-# 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 GGPlot2 plots code blocks
-}

module Text.Pandoc.Filter.Plot.Renderers.GGPlot2 (
      ggplot2SupportedSaveFormats
    , ggplot2Command
    , ggplot2Capture
    , ggplot2Available
) where

import qualified Data.Text as T
import           Text.Pandoc.Filter.Plot.Renderers.Prelude


ggplot2SupportedSaveFormats :: [SaveFormat]
ggplot2SupportedSaveFormats :: [SaveFormat]
ggplot2SupportedSaveFormats = [SaveFormat
PNG, SaveFormat
PDF, SaveFormat
SVG, SaveFormat
JPG, SaveFormat
EPS, SaveFormat
TIF]


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


ggplot2Available :: PlotM Bool
ggplot2Available :: PlotM Bool
ggplot2Available = do
    Maybe Executable
mexe <- Toolkit -> PlotM (Maybe Executable)
executable Toolkit
GGPlot2
    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} -e 'if(!require("ggplot2")) {quit(status=1)}'|]


ggplot2Capture :: FigureSpec -> FilePath -> Script
ggplot2Capture :: FigureSpec -> FilePath -> Text
ggplot2Capture FigureSpec
fs FilePath
fp = 
    [Text] -> Text
T.unlines [ Text
"pdf(NULL)" -- Prevent the creation of empty Rplots.pdf

              , FigureSpec -> Text
script FigureSpec
fs
              , FigureSpec -> FilePath -> Text
ggplot2CaptureFragment FigureSpec
fs FilePath
fp
              ]


ggplot2CaptureFragment :: FigureSpec -> FilePath -> Script
ggplot2CaptureFragment :: FigureSpec -> FilePath -> Text
ggplot2CaptureFragment 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|
library(ggplot2) # just in case
ggsave("#{toRPath fname}", plot = last_plot(), dpi = #{dpi})
|]