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

-- |
-- Module      : $header$
-- Copyright   : (c) Sanchayan Maity, 2023 - present
-- License     : GNU GPL, version 2 or above
-- Maintainer  : sanchayan@sanchayanmaity.net
-- Stability   : internal
-- Portability : portable
--
-- Rendering D2 plots code blocks
module Text.Pandoc.Filter.Plot.Renderers.D2
  ( d2,
    d2SupportedSaveFormats,
  )
where

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

d2 :: PlotM Renderer
d2 :: PlotM Renderer
d2 = do
  Text
cmdargs <- forall a. (Configuration -> a) -> PlotM a
asksConfig Configuration -> Text
d2CmdArgs
  forall (m :: * -> *) a. Monad m => a -> m a
return
    forall a b. (a -> b) -> a -> b
$ Renderer
      { rendererToolkit :: Toolkit
rendererToolkit = Toolkit
D2,
        rendererCapture :: FigureSpec -> FilePath -> Text
rendererCapture = FigureSpec -> FilePath -> Text
d2Capture,
        rendererCommand :: OutputSpec -> Text
rendererCommand = Text -> OutputSpec -> Text
d2Command 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]
d2SupportedSaveFormats,
        rendererChecks :: [Text -> CheckResult]
rendererChecks = forall a. Monoid a => a
mempty,
        rendererLanguage :: Text
rendererLanguage = Text
"d2",
        rendererComment :: Text -> Text
rendererComment = forall a. Monoid a => a -> a -> a
mappend Text
"# ",
        rendererScriptExtension :: FilePath
rendererScriptExtension = FilePath
".d2"
      }

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

d2Command :: Text -> OutputSpec -> Text
d2Command :: Text -> OutputSpec -> Text
d2Command Text
cmdargs OutputSpec {FilePath
FigureSpec
Executable
oCWD :: OutputSpec -> FilePath
oExecutable :: OutputSpec -> Executable
oFigurePath :: OutputSpec -> FilePath
oScriptPath :: OutputSpec -> FilePath
oFigureSpec :: OutputSpec -> FigureSpec
oCWD :: FilePath
oExecutable :: Executable
oFigurePath :: FilePath
oScriptPath :: FilePath
oFigureSpec :: FigureSpec
..} = [st|#{pathToExe oExecutable} #{cmdargs} "#{oScriptPath}" "#{oFigurePath}"|]

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