{-# 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 <- forall a. (Configuration -> a) -> PlotM a
asksConfig Configuration -> Text
plotlyPythonCmdArgs
      forall (m :: * -> *) a. Monad m => a -> m a
return 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 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 = forall a. Monoid a => a
mempty,
                rendererLanguage :: Text
rendererLanguage = Text
"python",
                rendererComment :: Text -> Text
rendererComment = 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
oCWD :: OutputSpec -> String
oExecutable :: OutputSpec -> Executable
oFigurePath :: OutputSpec -> String
oScriptPath :: OutputSpec -> String
oFigureSpec :: OutputSpec -> FigureSpec
oCWD :: String
oExecutable :: Executable
oFigurePath :: String
oScriptPath :: String
oFigureSpec :: FigureSpec
..} = [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
blockAttrs :: FigureSpec -> Attr
extraAttrs :: FigureSpec -> [(Text, Text)]
dependencies :: FigureSpec -> [String]
dpi :: FigureSpec -> Int
directory :: FigureSpec -> String
saveFormat :: FigureSpec -> SaveFormat
script :: FigureSpec -> Text
withSource :: FigureSpec -> Bool
caption :: FigureSpec -> Text
fsExecutable :: FigureSpec -> Executable
renderer_ :: FigureSpec -> Renderer
blockAttrs :: Attr
extraAttrs :: [(Text, Text)]
dependencies :: [String]
dpi :: Int
directory :: String
saveFormat :: SaveFormat
script :: Text
withSource :: Bool
caption :: Text
fsExecutable :: Executable
renderer_ :: Renderer
..} 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
_ -> forall a. Monoid a => a
mempty