{-# 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 Asymptote plots code blocks
module Text.Pandoc.Filter.Plot.Renderers.Asymptote
  ( asymptote,
    asymptoteSupportedSaveFormats,
  )
where

import Text.Pandoc.Filter.Plot.Renderers.Prelude
import Data.Char(toLower)

asymptote :: PlotM Renderer
asymptote :: PlotM Renderer
asymptote = do
  Text
cmdargs <- (Configuration -> Text) -> PlotM Text
forall a. (Configuration -> a) -> PlotM a
asksConfig Configuration -> Text
asyCmdArgs
  Renderer -> PlotM Renderer
forall a. a -> StateT PlotState (ReaderT RuntimeEnv IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return
    (Renderer -> PlotM Renderer) -> Renderer -> PlotM Renderer
forall a b. (a -> b) -> a -> b
$ Renderer
      { rendererToolkit :: Toolkit
rendererToolkit = Toolkit
Asymptote,
        rendererCapture :: FigureSpec -> FilePath -> Text
rendererCapture = FigureSpec -> FilePath -> Text
asymptoteCapture,
        rendererCommand :: OutputSpec -> Text
rendererCommand = Text -> OutputSpec -> Text
asymptoteCommand Text
cmdargs,
        rendererAvailability :: AvailabilityCheck
rendererAvailability = (Executable -> Text) -> AvailabilityCheck
CommandSuccess ((Executable -> Text) -> AvailabilityCheck)
-> (Executable -> Text) -> AvailabilityCheck
forall a b. (a -> b) -> a -> b
$ \Executable
exe -> [st|#{pathToExe exe} -environment|],
        rendererSupportedSaveFormats :: [SaveFormat]
rendererSupportedSaveFormats = [SaveFormat]
asymptoteSupportedSaveFormats,
        rendererChecks :: [Text -> CheckResult]
rendererChecks = [Text -> CheckResult]
forall a. Monoid a => a
mempty,
        rendererLanguage :: Text
rendererLanguage = Text
"asy",
        rendererComment :: Text -> Text
rendererComment = Text -> Text -> Text
forall a. Monoid a => a -> a -> a
mappend Text
"// ",
        rendererScriptExtension :: FilePath
rendererScriptExtension = FilePath
".asy"
      }

asymptoteSupportedSaveFormats :: [SaveFormat]
asymptoteSupportedSaveFormats :: [SaveFormat]
asymptoteSupportedSaveFormats = [SaveFormat
PDF, SaveFormat
EPS, SaveFormat
PNG]

asymptoteCommand :: Text -> OutputSpec -> Text
asymptoteCommand :: Text -> OutputSpec -> Text
asymptoteCommand Text
cmdArgs OutputSpec {FilePath
FigureSpec
Executable
oFigureSpec :: FigureSpec
oScriptPath :: FilePath
oFigurePath :: FilePath
oExecutable :: Executable
oCWD :: FilePath
oFigureSpec :: OutputSpec -> FigureSpec
oScriptPath :: OutputSpec -> FilePath
oFigurePath :: OutputSpec -> FilePath
oExecutable :: OutputSpec -> Executable
oCWD :: OutputSpec -> FilePath
..} =
  [st|#{pathToExe oExecutable} #{cmdArgs} -f #{toLower <$> show (saveFormat oFigureSpec)} -o "#{oFigurePath}" "#{oScriptPath}"|]

-- Asymptote export is entirely based on command-line arguments
-- so there is no need to modify the script itself.
asymptoteCapture :: FigureSpec -> FilePath -> Script
asymptoteCapture :: FigureSpec -> FilePath -> Text
asymptoteCapture FigureSpec {Bool
Int
FilePath
[FilePath]
[(Text, Text)]
Attr
Text
Renderer
SaveFormat
Executable
saveFormat :: FigureSpec -> SaveFormat
renderer_ :: Renderer
fsExecutable :: Executable
caption :: Text
withSource :: Bool
script :: Text
saveFormat :: SaveFormat
directory :: FilePath
dpi :: Int
dependencies :: [FilePath]
extraAttrs :: [(Text, Text)]
blockAttrs :: Attr
renderer_ :: FigureSpec -> Renderer
fsExecutable :: FigureSpec -> Executable
caption :: FigureSpec -> Text
withSource :: FigureSpec -> Bool
script :: FigureSpec -> Text
directory :: FigureSpec -> FilePath
dpi :: FigureSpec -> Int
dependencies :: FigureSpec -> [FilePath]
extraAttrs :: FigureSpec -> [(Text, Text)]
blockAttrs :: FigureSpec -> Attr
..} FilePath
_ = Text
script