{-# 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 Matlab code blocks
module Text.Pandoc.Filter.Plot.Renderers.Matlab
  ( matlab,
    matlabSupportedSaveFormats,
  )
where

import System.Directory (exeExtension)
import Text.Pandoc.Filter.Plot.Renderers.Prelude

matlab :: PlotM (Maybe Renderer)
matlab :: PlotM (Maybe Renderer)
matlab = do
  Bool
avail <- PlotM Bool
matlabAvailable
  if Bool -> Bool
not Bool
avail
    then Maybe Renderer -> PlotM (Maybe Renderer)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Renderer
forall a. Maybe a
Nothing
    else do
      Text
cmdargs <- (Configuration -> Text) -> PlotM Text
forall a. (Configuration -> a) -> PlotM a
asksConfig Configuration -> Text
matlabCmdArgs
      Maybe Executable
mexe <- Toolkit -> PlotM (Maybe Executable)
executable Toolkit
Matlab
      Maybe Renderer -> PlotM (Maybe Renderer)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Renderer -> PlotM (Maybe Renderer))
-> Maybe Renderer -> PlotM (Maybe Renderer)
forall a b. (a -> b) -> a -> b
$
        Maybe Executable
mexe Maybe Executable
-> (Executable -> Maybe Renderer) -> Maybe Renderer
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \exe :: Executable
exe@(Executable FilePath
_ Text
exename) ->
          Renderer -> Maybe Renderer
forall (m :: * -> *) a. Monad m => a -> m a
return
            Renderer :: Toolkit
-> Executable
-> (FigureSpec -> FilePath -> Text)
-> (OutputSpec -> Text)
-> [SaveFormat]
-> [Text -> CheckResult]
-> Text
-> (Text -> Text)
-> FilePath
-> Renderer
Renderer
              { rendererToolkit :: Toolkit
rendererToolkit = Toolkit
Matlab,
                rendererExe :: Executable
rendererExe = Executable
exe,
                rendererCapture :: FigureSpec -> FilePath -> Text
rendererCapture = FigureSpec -> FilePath -> Text
matlabCapture,
                rendererCommand :: OutputSpec -> Text
rendererCommand = Text -> Text -> OutputSpec -> Text
matlabCommand Text
cmdargs Text
exename,
                rendererSupportedSaveFormats :: [SaveFormat]
rendererSupportedSaveFormats = [SaveFormat]
matlabSupportedSaveFormats,
                rendererChecks :: [Text -> CheckResult]
rendererChecks = [Text -> CheckResult]
forall a. Monoid a => a
mempty,
                rendererLanguage :: Text
rendererLanguage = Text
"matlab",
                rendererComment :: Text -> Text
rendererComment = Text -> Text -> Text
forall a. Monoid a => a -> a -> a
mappend Text
"% ",
                rendererScriptExtension :: FilePath
rendererScriptExtension = FilePath
".m"
              }

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

matlabCommand :: Text -> Text -> OutputSpec -> Text
matlabCommand :: Text -> Text -> OutputSpec -> Text
matlabCommand Text
cmdargs Text
exe OutputSpec {FilePath
FigureSpec
oCWD :: OutputSpec -> FilePath
oFigurePath :: OutputSpec -> FilePath
oScriptPath :: OutputSpec -> FilePath
oFigureSpec :: OutputSpec -> FigureSpec
oCWD :: FilePath
oFigurePath :: FilePath
oScriptPath :: FilePath
oFigureSpec :: FigureSpec
..} = 
  -- The MATLAB 'run' function will switch to the directory where the script
  -- is located before executing the script. Therefore, we first save the current
  -- working directory in the variable 'pandoc_plot_cwd' so that we can use it 
  -- when exporting the figure
  [st|#{exe} #{cmdargs} -sd '#{oCWD}' -noFigureWindows -batch "pandoc_plot_cwd=pwd; run('#{oScriptPath}')"|]

-- On Windows at least, "matlab -help"  actually returns -1, even though the
-- help text is shown successfully!
-- Therefore, we cannot rely on this behavior to know if matlab is present,
-- like other toolkits.
matlabAvailable :: PlotM Bool
matlabAvailable :: PlotM Bool
matlabAvailable = 
  (Configuration -> FilePath) -> PlotM FilePath
forall a. (Configuration -> a) -> PlotM a
asksConfig Configuration -> FilePath
matlabExe PlotM FilePath -> (FilePath -> PlotM Bool) -> PlotM Bool
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\FilePath
exe -> IO Bool -> PlotM Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> PlotM Bool) -> IO Bool -> PlotM Bool
forall a b. (a -> b) -> a -> b
$ FilePath -> IO Bool
existsOnPath (FilePath
exe FilePath -> FilePath -> FilePath
forall a. Semigroup a => a -> a -> a
<> FilePath
exeExtension))

matlabCapture :: FigureSpec -> FilePath -> Script
matlabCapture :: FigureSpec -> FilePath -> Text
matlabCapture = (FigureSpec -> FilePath -> Text) -> FigureSpec -> FilePath -> Text
appendCapture FigureSpec -> FilePath -> Text
matlabCaptureFragment

matlabCaptureFragment :: FigureSpec -> FilePath -> Script
matlabCaptureFragment :: FigureSpec -> FilePath -> Text
matlabCaptureFragment FigureSpec {Bool
Int
FilePath
[FilePath]
[(Text, Text)]
Attr
Text
Renderer
SaveFormat
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
renderer_ :: FigureSpec -> Renderer
blockAttrs :: Attr
extraAttrs :: [(Text, Text)]
dependencies :: [FilePath]
dpi :: Int
directory :: FilePath
saveFormat :: SaveFormat
script :: Text
withSource :: Bool
caption :: Text
renderer_ :: Renderer
..} FilePath
fname =
  [st|
if java.io.File('#{fname}').isAbsolute() > 0
  exportpath = '#{fname}';
else
  exportpath = fullfile(pandoc_plot_cwd, '#{fname}');
end

if exist("exportgraphics")>0
    exportgraphics(gcf, exportpath, 'Resolution', #{dpi});
else
    saveas(gcf, exportpath);
end
|]