{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE NoImplicitPrelude #-}

-- |

-- Module      : $header$

-- Copyright   : (c) Laurent P René de Cotret, 2020

-- License     : GNU GPL, version 2 or above

-- Maintainer  : laurent.decotret@outlook.com

-- Stability   : internal

-- Portability : portable

--

-- Rendering Julia/Plots.jl code blocks

module Text.Pandoc.Filter.Plot.Renderers.Plotsjl
  ( plotsjlSupportedSaveFormats,
    plotsjlCommand,
    plotsjlCapture,
    plotsjlAvailable,
  )
where

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

-- Save formats support by most backends

-- https://docs.plotsjl.org/latest/output/#Supported-output-file-formats-1

plotsjlSupportedSaveFormats :: [SaveFormat]
plotsjlSupportedSaveFormats :: [SaveFormat]
plotsjlSupportedSaveFormats = [SaveFormat
PNG, SaveFormat
SVG, SaveFormat
PDF]

plotsjlCommand :: OutputSpec -> Text -> Text
plotsjlCommand :: OutputSpec -> Text -> Text
plotsjlCommand OutputSpec {FilePath
FigureSpec
oFigurePath :: OutputSpec -> FilePath
oScriptPath :: OutputSpec -> FilePath
oFigureSpec :: OutputSpec -> FigureSpec
oFigurePath :: FilePath
oScriptPath :: FilePath
oFigureSpec :: FigureSpec
..} Text
exe = [st|#{exe} "#{oScriptPath}"|]

plotsjlAvailable :: PlotM Bool
plotsjlAvailable :: PlotM Bool
plotsjlAvailable = do
  Maybe Executable
mexe <- Toolkit -> PlotM (Maybe Executable)
executable Toolkit
Plotsjl
  case Maybe Executable
mexe of
    Maybe Executable
Nothing -> Bool -> PlotM Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
    Just (Executable FilePath
dir Text
exe) ->
      FilePath -> Text -> PlotM Bool
commandSuccess FilePath
dir [st|#{exe} -e "using Plots"|]

plotsjlCapture :: FigureSpec -> FilePath -> Script
plotsjlCapture :: FigureSpec -> FilePath -> Text
plotsjlCapture = (FigureSpec -> FilePath -> Text) -> FigureSpec -> FilePath -> Text
appendCapture FigureSpec -> FilePath -> Text
plotsjlCaptureFragment

plotsjlCaptureFragment :: FigureSpec -> FilePath -> Script
plotsjlCaptureFragment :: FigureSpec -> FilePath -> Text
plotsjlCaptureFragment FigureSpec
_ FilePath
fname =
  [st|
savefig(raw"#{fname}")
|]