{-# 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 Bokeh code blocks
module Text.Pandoc.Filter.Plot.Renderers.Bokeh
  ( bokeh,
    bokehSupportedSaveFormats,
  )
where

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

bokeh :: PlotM Renderer
bokeh :: PlotM Renderer
bokeh = do
      Text
cmdargs <- forall a. (Configuration -> a) -> PlotM a
asksConfig Configuration -> Text
bokehCmdArgs
      forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
        Renderer
          { rendererToolkit :: Toolkit
rendererToolkit = Toolkit
Bokeh,
            rendererCapture :: FigureSpec -> String -> Text
rendererCapture = (FigureSpec -> String -> Text) -> FigureSpec -> String -> Text
appendCapture FigureSpec -> String -> Text
bokehCaptureFragment,
            rendererCommand :: OutputSpec -> Text
rendererCommand = Text -> OutputSpec -> Text
bokehCommand Text
cmdargs,
            rendererAvailability :: AvailabilityCheck
rendererAvailability = (Executable -> Text) -> AvailabilityCheck
CommandSuccess forall a b. (a -> b) -> a -> b
$ \Executable
exe -> [st|#{pathToExe exe} -c "import bokeh; import selenium"|],
            rendererSupportedSaveFormats :: [SaveFormat]
rendererSupportedSaveFormats = [SaveFormat]
bokehSupportedSaveFormats,
            rendererChecks :: [Text -> CheckResult]
rendererChecks = [Text -> CheckResult
bokehCheckIfShow],
            rendererLanguage :: Text
rendererLanguage = Text
"python",
            rendererComment :: Text -> Text
rendererComment = forall a. Monoid a => a -> a -> a
mappend Text
"# ",
            rendererScriptExtension :: String
rendererScriptExtension = String
".py"
          }

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

bokehCommand :: Text -> OutputSpec -> Text
bokehCommand :: Text -> OutputSpec -> Text
bokehCommand 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}"|]

-- | Check if `bokeh.io.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 forall a b. (a -> b) -> a -> b
$ 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))
        forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [ Text
"bokeh.io.show(",
              Text
"show("
            ]

bokehCaptureFragment :: FigureSpec -> FilePath -> Script
bokehCaptureFragment :: FigureSpec -> String -> Text
bokehCaptureFragment 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|
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 -> forall a. String -> a
errorWithoutStackTrace forall a b. (a -> b) -> a -> b
$ String
"Save format not supported: " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show SaveFormat
fmt