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

import Text.Pandoc.Filter.Plot.Renderers.Prelude

mathematica :: PlotM Renderer
mathematica :: PlotM Renderer
mathematica = do
      Text
cmdargs <- (Configuration -> Text) -> PlotM Text
forall a. (Configuration -> a) -> PlotM a
asksConfig Configuration -> Text
mathematicaCmdArgs
      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
Mathematica,
            rendererCapture :: FigureSpec -> FilePath -> Text
rendererCapture = FigureSpec -> FilePath -> Text
mathematicaCapture,
            rendererCommand :: OutputSpec -> Text
rendererCommand = Text -> OutputSpec -> Text
mathematicaCommand 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|], -- TODO: test this
            rendererSupportedSaveFormats :: [SaveFormat]
rendererSupportedSaveFormats = [SaveFormat]
mathematicaSupportedSaveFormats,
            rendererChecks :: [Text -> CheckResult]
rendererChecks = [Text -> CheckResult]
forall a. Monoid a => a
mempty,
            rendererLanguage :: Text
rendererLanguage = Text
"mathematica",
            rendererComment :: Text -> Text
rendererComment = \Text
t -> [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat [Text
"(*", Text
t, Text
"*)"],
            rendererScriptExtension :: FilePath
rendererScriptExtension = FilePath
".m"
          }

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

mathematicaCommand :: Text -> OutputSpec -> Text
mathematicaCommand :: Text -> OutputSpec -> Text
mathematicaCommand 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} -script "#{oScriptPath}"|]

mathematicaCapture :: FigureSpec -> FilePath -> Script
mathematicaCapture :: FigureSpec -> FilePath -> Text
mathematicaCapture = (FigureSpec -> FilePath -> Text) -> FigureSpec -> FilePath -> Text
appendCapture FigureSpec -> FilePath -> Text
mathematicaCaptureFragment

mathematicaCaptureFragment :: FigureSpec -> FilePath -> Script
mathematicaCaptureFragment :: FigureSpec -> FilePath -> Text
mathematicaCaptureFragment 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
script :: FigureSpec -> Text
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
..} FilePath
fname =
  [st|
Export["#{fname}", %, #{show saveFormat}, ImageResolution -> #{dpi}]
|]