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

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

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

mermaid :: PlotM Renderer
mermaid :: PlotM Renderer
mermaid = do
  Text
cmdargs <- (Configuration -> Text) -> PlotM Text
forall a. (Configuration -> a) -> PlotM a
asksConfig Configuration -> Text
mermaidCmdArgs
  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
Mermaid,
        rendererCapture :: FigureSpec -> FilePath -> Text
rendererCapture = FigureSpec -> FilePath -> Text
mermaidCapture,
        rendererCommand :: OutputSpec -> Text
rendererCommand = Text -> OutputSpec -> Text
mermaidCommand 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} -V|],
        rendererSupportedSaveFormats :: [SaveFormat]
rendererSupportedSaveFormats = [SaveFormat]
mermaidSupportedSaveFormats,
        rendererChecks :: [Text -> CheckResult]
rendererChecks = [Text -> CheckResult]
forall a. Monoid a => a
mempty,
        rendererLanguage :: Text
rendererLanguage = Text
"mermaid",
        rendererComment :: Text -> Text
rendererComment = Text -> Text
forall a. Monoid a => a
mempty,
        rendererScriptExtension :: FilePath
rendererScriptExtension = FilePath
".mermaid"
      }

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

mermaidCommand :: Text -> OutputSpec -> Text
mermaidCommand :: Text -> OutputSpec -> Text
mermaidCommand 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
..} =
  let fmt :: FilePath
fmt = (Char -> Char) -> FilePath -> FilePath
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Char -> Char
toLower (FilePath -> FilePath)
-> (FigureSpec -> FilePath) -> FigureSpec -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SaveFormat -> FilePath
forall a. Show a => a -> FilePath
show (SaveFormat -> FilePath)
-> (FigureSpec -> SaveFormat) -> FigureSpec -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FigureSpec -> SaveFormat
saveFormat (FigureSpec -> FilePath) -> FigureSpec -> FilePath
forall a b. (a -> b) -> a -> b
$ FigureSpec
oFigureSpec
   in [st|#{pathToExe oExecutable} #{cmdargs} -q -e #{fmt} -i "#{oScriptPath}" -o "#{oFigurePath}"|]

-- Mermaid export is entirely based on command-line arguments
-- so there is no need to modify the script itself.
mermaidCapture :: FigureSpec -> FilePath -> Script
mermaidCapture :: FigureSpec -> FilePath -> Text
mermaidCapture 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