{-# 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 GGPlot2 plots code blocks
module Text.Pandoc.Filter.Plot.Renderers.GGPlot2
  ( ggplot2,
    ggplot2SupportedSaveFormats,
  )
where

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

ggplot2 :: PlotM Renderer
ggplot2 :: PlotM Renderer
ggplot2 = do
  Text
cmdargs <- (Configuration -> Text) -> PlotM Text
forall a. (Configuration -> a) -> PlotM a
asksConfig Configuration -> Text
ggplot2CmdArgs
  Renderer -> PlotM Renderer
forall a. a -> StateT PlotState (ReaderT RuntimeEnv IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return
    (Renderer -> PlotM Renderer) -> Renderer -> PlotM Renderer
forall a b. (a -> b) -> a -> b
$ Renderer
      { rendererToolkit :: Toolkit
rendererToolkit = Toolkit
GGPlot2,
        rendererCapture :: FigureSpec -> String -> Text
rendererCapture = FigureSpec -> String -> Text
ggplot2Capture,
        rendererCommand :: OutputSpec -> Text
rendererCommand = Text -> OutputSpec -> Text
ggplot2Command 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} -e "if(!require('ggplot2')) {quit(status=1)}"|],
        rendererSupportedSaveFormats :: [SaveFormat]
rendererSupportedSaveFormats = [SaveFormat]
ggplot2SupportedSaveFormats,
        rendererChecks :: [Text -> CheckResult]
rendererChecks = [Text -> CheckResult]
forall a. Monoid a => a
mempty,
        rendererLanguage :: Text
rendererLanguage = Text
"r",
        rendererComment :: Text -> Text
rendererComment = Text -> Text -> Text
forall a. Monoid a => a -> a -> a
mappend Text
"# ",
        rendererScriptExtension :: String
rendererScriptExtension = String
".r"
      }

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

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

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

ggplot2CaptureFragment :: FigureSpec -> FilePath -> Script
ggplot2CaptureFragment :: FigureSpec -> String -> Text
ggplot2CaptureFragment FigureSpec {Bool
Int
String
[String]
[(Text, Text)]
Attr
Text
Renderer
SaveFormat
Executable
script :: FigureSpec -> Text
renderer_ :: Renderer
fsExecutable :: Executable
caption :: Text
withSource :: Bool
script :: Text
saveFormat :: SaveFormat
directory :: String
dpi :: Int
dependencies :: [String]
extraAttrs :: [(Text, Text)]
blockAttrs :: Attr
renderer_ :: FigureSpec -> Renderer
fsExecutable :: FigureSpec -> Executable
caption :: FigureSpec -> Text
withSource :: FigureSpec -> Bool
saveFormat :: FigureSpec -> SaveFormat
directory :: FigureSpec -> String
dpi :: FigureSpec -> Int
dependencies :: FigureSpec -> [String]
extraAttrs :: FigureSpec -> [(Text, Text)]
blockAttrs :: FigureSpec -> Attr
..} String
fname =
  [st|
library(ggplot2) # just in case
ggsave("#{toRPath fname}", plot = last_plot(), dpi = #{dpi})
|]