{-# 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 Bokeh code blocks
-}

module Text.Pandoc.Filter.Plot.Renderers.Bokeh (
      bokehSupportedSaveFormats
    , bokehCommand
    , bokehCapture
    , bokehAvailable
    , bokehCheckIfShow
) where

import           Data.Monoid                               (Any(..))
import qualified Data.Text                                 as T
import           Text.Pandoc.Filter.Plot.Renderers.Prelude


bokehSupportedSaveFormats :: [SaveFormat]
bokehSupportedSaveFormats :: [SaveFormat]
bokehSupportedSaveFormats = [SaveFormat
PNG, SaveFormat
SVG, SaveFormat
HTML]


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


bokehAvailable :: PlotM Bool
bokehAvailable :: PlotM Bool
bokehAvailable = do
    Maybe Executable
mexe <- Toolkit -> PlotM (Maybe Executable)
executable Toolkit
Bokeh
    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 bokeh; import selenium"|]


-- | Check if `matplotlib.pyplot.show()` calls are present in the script,

-- which would halt pandoc-plot

bokehCheckIfShow :: Script -> CheckResult
bokehCheckIfShow :: Text -> CheckResult
bokehCheckIfShow Text
s = 
    if Any -> Bool
getAny (Any -> Bool) -> Any -> Bool
forall a b. (a -> b) -> a -> b
$ [Any] -> Any
forall a. Monoid a => [a] -> a
mconcat [Any]
showPresent
        then Text -> CheckResult
CheckFailed Text
"encountered a call to `bokeh.io.show`."
        else CheckResult
CheckPassed
    where
        showPresent :: [Any]
showPresent = (\Text
n -> Bool -> Any
Any (Text -> Text -> Bool
T.isInfixOf Text
n Text
s)) (Text -> Any) -> [Text] -> [Any]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [
                  Text
"bokeh.io.show("
                , Text
"show("
            ]


bokehCapture :: FigureSpec -> FilePath -> Script
bokehCapture :: FigureSpec -> FilePath -> Text
bokehCapture = (FigureSpec -> FilePath -> Text) -> FigureSpec -> FilePath -> Text
appendCapture FigureSpec -> FilePath -> Text
bokehCaptureFragment


bokehCaptureFragment :: FigureSpec -> FilePath -> Script
bokehCaptureFragment :: FigureSpec -> FilePath -> Text
bokehCaptureFragment 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|
from bokeh.io import export_png, export_svgs, save
from bokeh.models import Model
from bokeh.resources import CDN

# The heuristic to determine the current Model is to find all objects which are
# at least subclasses of bokeh.models.Model, and then find the one which was
# created last. This is a dirty hack, so if you're reading this, don't hesitate to
# suggest something else.
__current_model = [obj for obj in globals().values() if isinstance(obj, Model)][-1]
#{write}
|]
    where  
        write :: Text
write = case SaveFormat
saveFormat of
            SaveFormat
HTML -> [st|save(__current_model, filename=r"#{fname}", resources=CDN)|]
            SaveFormat
SVG  -> [st|__current_model.output_backend="svg"; export_svgs(__current_model, filename=r"#{fname}")|]
            SaveFormat
PNG  -> [st|export_png(obj = __current_model, filename=r"#{fname}")|]
            SaveFormat
fmt  -> FilePath -> Text
forall a. FilePath -> a
errorWithoutStackTrace (FilePath -> Text) -> FilePath -> Text
forall a b. (a -> b) -> a -> b
$ FilePath
"Save format not supported: " FilePath -> FilePath -> FilePath
forall a. Semigroup a => a -> a -> a
<> SaveFormat -> FilePath
forall a. Show a => a -> FilePath
show SaveFormat
fmt