{-# 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 Plotly-python code blocks
module Text.Pandoc.Filter.Plot.Renderers.PlotlyPython
  ( plotlyPython,
    plotlyPythonSupportedSaveFormats,
  )
where

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

plotlyPython :: PlotM Renderer
plotlyPython :: PlotM Renderer
plotlyPython = do
  Text
cmdargs <- (Configuration -> Text) -> PlotM Text
forall a. (Configuration -> a) -> PlotM a
asksConfig Configuration -> Text
plotlyPythonCmdArgs
  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
PlotlyPython,
        rendererCapture :: FigureSpec -> String -> Text
rendererCapture = FigureSpec -> String -> Text
plotlyPythonCapture,
        rendererCommand :: OutputSpec -> Text
rendererCommand = Text -> OutputSpec -> Text
plotlyPythonCommand 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} -c "import plotly.graph_objects"|],
        rendererSupportedSaveFormats :: [SaveFormat]
rendererSupportedSaveFormats = [SaveFormat]
plotlyPythonSupportedSaveFormats,
        rendererChecks :: [Text -> CheckResult]
rendererChecks = [Text -> CheckResult]
forall a. Monoid a => a
mempty,
        rendererLanguage :: Text
rendererLanguage = Text
"python",
        rendererComment :: Text -> Text
rendererComment = Text -> Text -> Text
forall a. Monoid a => a -> a -> a
mappend Text
"# ",
        rendererScriptExtension :: String
rendererScriptExtension = String
".py"
      }

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

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

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

plotlyPythonCaptureFragment :: FigureSpec -> FilePath -> Script
plotlyPythonCaptureFragment :: FigureSpec -> String -> Text
plotlyPythonCaptureFragment FigureSpec {Bool
Int
String
[String]
[(Text, Text)]
Attr
Text
Renderer
SaveFormat
Executable
renderer_ :: Renderer
fsExecutable :: Executable
caption :: Text
withSource :: Bool
script :: Text
saveFormat :: SaveFormat
directory :: String
dpi :: Int
dependencies :: [String]
extraAttrs :: [(Text, Text)]
blockAttrs :: Attr
renderer_ :: FigureSpec -> Renderer
fsExecutable :: FigureSpec -> Executable
caption :: FigureSpec -> Text
withSource :: FigureSpec -> Bool
script :: FigureSpec -> Text
saveFormat :: FigureSpec -> SaveFormat
directory :: FigureSpec -> String
dpi :: FigureSpec -> Int
dependencies :: FigureSpec -> [String]
extraAttrs :: FigureSpec -> [(Text, Text)]
blockAttrs :: FigureSpec -> Attr
..} String
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