{-# 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 SageMath figures
module Text.Pandoc.Filter.Plot.Renderers.SageMath
  ( sagemath,
    sagemathSupportedSaveFormats,
  )
where

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

sagemath :: PlotM Renderer
sagemath :: PlotM Renderer
sagemath = do
      Text
cmdargs <- forall a. (Configuration -> a) -> PlotM a
asksConfig Configuration -> Text
sagemathCmdArgs
      forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
        Renderer
          { rendererToolkit :: Toolkit
rendererToolkit = Toolkit
SageMath,
            rendererCapture :: FigureSpec -> String -> Text
rendererCapture = FigureSpec -> String -> Text
sagemathCapture,
            rendererCommand :: OutputSpec -> Text
rendererCommand = Text -> OutputSpec -> Text
sagemathCommand Text
cmdargs,
            rendererAvailability :: AvailabilityCheck
rendererAvailability = (Executable -> Text) -> AvailabilityCheck
CommandSuccess  forall a b. (a -> b) -> a -> b
$ \Executable
exe -> [st|#{pathToExe exe} -v|],
            rendererSupportedSaveFormats :: [SaveFormat]
rendererSupportedSaveFormats = [SaveFormat]
sagemathSupportedSaveFormats,
            rendererChecks :: [Text -> CheckResult]
rendererChecks = forall a. Monoid a => a
mempty,
            rendererLanguage :: Text
rendererLanguage = Text
"sagemath",
            rendererComment :: Text -> Text
rendererComment = forall a. Monoid a => a -> a -> a
mappend Text
"# ",
            rendererScriptExtension :: String
rendererScriptExtension = String
".sage"
          }

-- See here:
-- https://doc.sagemath.org/html/en/reference/plotting/sage/plot/graphics.html#sage.plot.graphics.Graphics.save
sagemathSupportedSaveFormats :: [SaveFormat]
sagemathSupportedSaveFormats :: [SaveFormat]
sagemathSupportedSaveFormats = [SaveFormat
EPS, SaveFormat
PDF, SaveFormat
PNG, SaveFormat
SVG]

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

sagemathCapture :: FigureSpec -> FilePath -> Script
sagemathCapture :: FigureSpec -> String -> Text
sagemathCapture = (FigureSpec -> String -> Text) -> FigureSpec -> String -> Text
appendCapture FigureSpec -> String -> Text
sagemathCaptureFragment

-- This capture fragment is a bit ugly because sage does not have the
-- equivalent of matplotlib's `plt.gca()` to get a pointer to the most
-- recent graphical object. We must search for it
sagemathCaptureFragment :: FigureSpec -> FilePath -> Script
sagemathCaptureFragment :: FigureSpec -> String -> Text
sagemathCaptureFragment 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|
import sage.plot.graphics as go
import sage.plot.plot3d.base as go3d
import builtins
# Try to concatenate 3D graphics objects first; if this doesn't work, then 
# concatenate all 2D graphic objects.
__all_graphics = builtins.sum( (obj for obj in globals().values() if isinstance(obj, go3d.Graphics3d)))
if not __all_graphics:
    __all_graphics = builtins.sum( (obj for obj in globals().values() if isinstance(obj, go.Graphics)))
if not __all_graphics:
    raise RuntimeError(''.join([
        "No plotting objects detected. ",
        "Make sure that all of your plotting objects are named, e.g. `G = plot(...)` rather than just `plot(...)`. ",
        "This is a limitation specific to the interaction between sage and pandoc-plot."
    ]))
__all_graphics.save_image(r"#{fname}", dpi=#{dpi})
|]