{-# 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 <- forall a. (Configuration -> a) -> PlotM a
asksConfig Configuration -> Text
mathematicaCmdArgs
      forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
        Renderer
          { rendererToolkit :: Toolkit
rendererToolkit = Toolkit
Mathematica,
            rendererCapture :: FigureSpec -> String -> Text
rendererCapture = FigureSpec -> String -> Text
mathematicaCapture,
            rendererCommand :: OutputSpec -> Text
rendererCommand = Text -> OutputSpec -> Text
mathematicaCommand Text
cmdargs,
            rendererAvailability :: AvailabilityCheck
rendererAvailability = (Executable -> Text) -> AvailabilityCheck
CommandSuccess 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 = forall a. Monoid a => a
mempty,
            rendererLanguage :: Text
rendererLanguage = Text
"mathematica",
            rendererComment :: Text -> Text
rendererComment = \Text
t -> forall a. Monoid a => [a] -> a
mconcat [Text
"(*", Text
t, Text
"*)"],
            rendererScriptExtension :: String
rendererScriptExtension = String
".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 {String
FigureSpec
Executable
oCWD :: OutputSpec -> String
oExecutable :: OutputSpec -> Executable
oFigurePath :: OutputSpec -> String
oScriptPath :: OutputSpec -> String
oFigureSpec :: OutputSpec -> FigureSpec
oCWD :: String
oExecutable :: Executable
oFigurePath :: String
oScriptPath :: String
oFigureSpec :: FigureSpec
..} = [st|#{pathToExe oExecutable} #{cmdargs} -script "#{oScriptPath}"|]

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

mathematicaCaptureFragment :: FigureSpec -> FilePath -> Script
mathematicaCaptureFragment :: FigureSpec -> String -> Text
mathematicaCaptureFragment FigureSpec {Bool
Int
String
[String]
[(Text, Text)]
Attr
Text
Renderer
SaveFormat
Executable
blockAttrs :: FigureSpec -> Attr
extraAttrs :: FigureSpec -> [(Text, Text)]
dependencies :: FigureSpec -> [String]
dpi :: FigureSpec -> Int
directory :: FigureSpec -> String
saveFormat :: FigureSpec -> SaveFormat
script :: FigureSpec -> Text
withSource :: FigureSpec -> Bool
caption :: FigureSpec -> Text
fsExecutable :: FigureSpec -> Executable
renderer_ :: FigureSpec -> Renderer
blockAttrs :: Attr
extraAttrs :: [(Text, Text)]
dependencies :: [String]
dpi :: Int
directory :: String
saveFormat :: SaveFormat
script :: Text
withSource :: Bool
caption :: Text
fsExecutable :: Executable
renderer_ :: Renderer
..} String
fname =
  [st|
Export["#{fname}", %, #{show saveFormat}, ImageResolution -> #{dpi}]
|]