{-# 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 (m :: * -> *) a. Monad m => a -> m a
return (Renderer -> PlotM Renderer) -> Renderer -> PlotM Renderer
forall a b. (a -> b) -> a -> b
$
            Renderer :: Toolkit
-> (FigureSpec -> FilePath -> Text)
-> (OutputSpec -> Text)
-> AvailabilityCheck
-> [SaveFormat]
-> [Text -> CheckResult]
-> Text
-> (Text -> Text)
-> FilePath
-> Renderer
Renderer
              { rendererToolkit :: Toolkit
rendererToolkit = Toolkit
PlotlyPython,
                rendererCapture :: FigureSpec -> FilePath -> Text
rendererCapture = FigureSpec -> FilePath -> 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 :: FilePath
rendererScriptExtension = FilePath
".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 {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}"|]

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
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
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