{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes       #-}
{-# LANGUAGE RecordWildCards   #-}
{-|
Module      : $header$
Copyright   : (c) Laurent P René de Cotret, 2020
License     : GNU GPL, version 2 or above
Maintainer  : laurent.decotret@outlook.com
Stability   : internal
Portability : portable

Rendering Plotly-python code blocks
-}

module Text.Pandoc.Filter.Plot.Renderers.PlotlyPython (
      plotlyPythonSupportedSaveFormats
    , plotlyPythonCommand
    , plotlyPythonCapture
    , plotlyPythonAvailable
) where

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


plotlyPythonSupportedSaveFormats :: [SaveFormat]
plotlyPythonSupportedSaveFormats :: [SaveFormat]
plotlyPythonSupportedSaveFormats = [SaveFormat
PNG, SaveFormat
JPG, SaveFormat
WEBP, SaveFormat
PDF, SaveFormat
SVG, SaveFormat
EPS, SaveFormat
HTML]


plotlyPythonCommand :: OutputSpec -> Text -> Text
plotlyPythonCommand :: OutputSpec -> Text -> Text
plotlyPythonCommand OutputSpec{FilePath
FigureSpec
oFigurePath :: OutputSpec -> FilePath
oScriptPath :: OutputSpec -> FilePath
oFigureSpec :: OutputSpec -> FigureSpec
oFigurePath :: FilePath
oScriptPath :: FilePath
oFigureSpec :: FigureSpec
..} Text
exe = [st|#{exe} "#{oScriptPath}"|]


plotlyPythonAvailable :: PlotM Bool
plotlyPythonAvailable :: PlotM Bool
plotlyPythonAvailable = do
    Maybe Executable
mexe <- Toolkit -> PlotM (Maybe Executable)
executable Toolkit
Matplotlib
    case Maybe Executable
mexe of 
        Maybe Executable
Nothing -> Bool -> PlotM Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
        Just (Executable FilePath
dir Text
exe) -> 
            FilePath -> Text -> PlotM Bool
commandSuccess FilePath
dir [st|#{exe} -c "import plotly.graph_objects"|]


plotlyPythonCapture :: FigureSpec -> FilePath -> Script
plotlyPythonCapture :: FigureSpec -> FilePath -> Text
plotlyPythonCapture = (FigureSpec -> FilePath -> Text) -> FigureSpec -> FilePath -> Text
appendCapture FigureSpec -> FilePath -> Text
plotlyPythonCaptureFragment


plotlyPythonCaptureFragment :: FigureSpec -> FilePath -> Script
plotlyPythonCaptureFragment :: FigureSpec -> FilePath -> Text
plotlyPythonCaptureFragment FigureSpec{Bool
Int
FilePath
[FilePath]
[(Text, Text)]
Attr
Text
SaveFormat
Toolkit
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
toolkit :: FigureSpec -> Toolkit
blockAttrs :: Attr
extraAttrs :: [(Text, Text)]
dependencies :: [FilePath]
dpi :: Int
directory :: FilePath
saveFormat :: SaveFormat
script :: Text
withSource :: Bool
caption :: Text
toolkit :: Toolkit
..} FilePath
fname = [st|
import plotly.graph_objects as go
__current_plotly_figure = next(obj for obj in globals().values() if type(obj) == go.Figure)
__current_plotly_figure.#{write_method}(r"#{fname}"#{extra_args})
|]
    where
        -- Note: the default behaviour for HTML export is

        --       to embed the entire Plotly.js content. This means

        --       that the resulting file can be used completely offline   

        write_method :: Text
write_method = case SaveFormat
saveFormat of
            SaveFormat
HTML -> Text
"write_html"::Text
            SaveFormat
_    -> Text
"write_image"
        extra_args :: Text
extra_args = case SaveFormat
saveFormat of
            SaveFormat
HTML -> Text
", include_plotlyjs='cdn'"::Text
            SaveFormat
_    -> Text
forall a. Monoid a => a
mempty