{-# 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 Text.Pandoc.Filter.Plot.Renderers.Prelude

matlab :: PlotM Renderer
matlab :: PlotM Renderer
matlab = do
      Text
cmdargs <- forall a. (Configuration -> a) -> PlotM a
asksConfig Configuration -> Text
matlabCmdArgs
      forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
        Renderer
          { rendererToolkit :: Toolkit
rendererToolkit = Toolkit
Matlab,
            rendererCapture :: FigureSpec -> String -> Text
rendererCapture = FigureSpec -> String -> Text
matlabCapture,
            rendererCommand :: OutputSpec -> Text
rendererCommand = Text -> OutputSpec -> Text
matlabCommand Text
cmdargs,
            -- 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.
            rendererAvailability :: AvailabilityCheck
rendererAvailability = AvailabilityCheck
ExecutableExists,
            rendererSupportedSaveFormats :: [SaveFormat]
rendererSupportedSaveFormats = [SaveFormat]
matlabSupportedSaveFormats,
            rendererChecks :: [Text -> CheckResult]
rendererChecks = forall a. Monoid a => a
mempty,
            rendererLanguage :: Text
rendererLanguage = Text
"matlab",
            rendererComment :: Text -> Text
rendererComment = forall a. Monoid a => a -> a -> a
mappend Text
"% ",
            rendererScriptExtension :: String
rendererScriptExtension = String
".m"
          }

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

matlabCommand :: Text  -> OutputSpec -> Text
matlabCommand :: Text -> OutputSpec -> Text
matlabCommand 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
..} = 
  -- 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|#{pathToExe oExecutable} #{cmdargs} -sd '#{oCWD}' -noFigureWindows -batch "pandoc_plot_cwd=pwd; run('#{oScriptPath}')"|]

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

matlabCaptureFragment :: FigureSpec -> FilePath -> Script
matlabCaptureFragment :: FigureSpec -> String -> Text
matlabCaptureFragment 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|
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
|]