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

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

graphviz :: PlotM Renderer
graphviz :: PlotM Renderer
graphviz = do
      Text
cmdargs <- forall a. (Configuration -> a) -> PlotM a
asksConfig Configuration -> Text
graphvizCmdArgs
      forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
        Renderer
          { rendererToolkit :: Toolkit
rendererToolkit = Toolkit
Graphviz,
            rendererCapture :: FigureSpec -> FilePath -> Text
rendererCapture = FigureSpec -> FilePath -> Text
graphvizCapture,
            rendererCommand :: OutputSpec -> Text
rendererCommand = Text -> OutputSpec -> Text
graphvizCommand Text
cmdargs,
            rendererAvailability :: AvailabilityCheck
rendererAvailability = (Executable -> Text) -> AvailabilityCheck
CommandSuccess forall a b. (a -> b) -> a -> b
$ \Executable
exe -> [st|#{pathToExe exe} -?|],
            rendererSupportedSaveFormats :: [SaveFormat]
rendererSupportedSaveFormats = [SaveFormat]
graphvizSupportedSaveFormats,
            rendererChecks :: [Text -> CheckResult]
rendererChecks = forall a. Monoid a => a
mempty,
            rendererLanguage :: Text
rendererLanguage = Text
"dot",
            rendererComment :: Text -> Text
rendererComment = forall a. Monoid a => a -> a -> a
mappend Text
"// ",
            rendererScriptExtension :: FilePath
rendererScriptExtension = FilePath
".dot"
          }

graphvizSupportedSaveFormats :: [SaveFormat]
graphvizSupportedSaveFormats :: [SaveFormat]
graphvizSupportedSaveFormats = [SaveFormat
PNG, SaveFormat
PDF, SaveFormat
SVG, SaveFormat
JPG, SaveFormat
EPS, SaveFormat
WEBP, SaveFormat
GIF]

graphvizCommand :: Text -> OutputSpec -> Text
graphvizCommand :: Text -> OutputSpec -> Text
graphvizCommand 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
..} =
  let fmt :: FilePath
fmt = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Char -> Char
toLower forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> FilePath
show forall b c a. (b -> c) -> (a -> b) -> a -> c
. FigureSpec -> SaveFormat
saveFormat forall a b. (a -> b) -> a -> b
$ FigureSpec
oFigureSpec
      -- Specifying a DPI when the output format is SVG crops the final figure
      -- See issue #40
      -- TODO: does this also affect other vector formats like EPS?
      dpi' :: Text
dpi' = if FigureSpec -> SaveFormat
saveFormat FigureSpec
oFigureSpec forall a. Eq a => a -> a -> Bool
== SaveFormat
SVG then forall a. Monoid a => a
mempty else [st|-Gdpi=#{dpi oFigureSpec}|]
   in [st|#{pathToExe oExecutable} #{cmdargs} -T#{fmt} #{dpi'} -o "#{oFigurePath}" "#{oScriptPath}"|]

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