-- | Defines a Monad context for rendering latex formulas as SVG images.

module Capabilities.LatexSvg (
  MonadLatexSvg (..),
  renderLatexSvg,
  ) where

import Control.Monad.Trans.Class        (MonadTrans (lift))
import Control.OutputCapable.Blocks.Generic (
  GenericReportT
  )
import Data.ByteString                  (ByteString)
import Data.Maybe                       (fromMaybe)
import Image.LaTeX.Render (
  Formula,
  FormulaOptions(..),
  EnvironmentOptions,
  defaultEnv,
  defaultFormulaOptions,
  )

class Monad m => MonadLatexSvg m where
  renderImage :: EnvironmentOptions -> FormulaOptions -> Formula -> m ByteString

instance MonadLatexSvg m => MonadLatexSvg (GenericReportT l o m) where
  renderImage :: EnvironmentOptions
-> FormulaOptions -> Formula -> GenericReportT l o m ByteString
renderImage EnvironmentOptions
opts FormulaOptions
mEnv = m ByteString -> GenericReportT l o m ByteString
forall (m :: * -> *) a. Monad m => m a -> GenericReportT l o m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m ByteString -> GenericReportT l o m ByteString)
-> (Formula -> m ByteString)
-> Formula
-> GenericReportT l o m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EnvironmentOptions -> FormulaOptions -> Formula -> m ByteString
forall (m :: * -> *).
MonadLatexSvg m =>
EnvironmentOptions -> FormulaOptions -> Formula -> m ByteString
renderImage EnvironmentOptions
opts FormulaOptions
mEnv


{- |
Renders the given Formula with provided options
or sensible default options if none are given.
-}
renderLatexSvg
  :: MonadLatexSvg m
  => Maybe EnvironmentOptions
  -> Maybe FormulaOptions
  -> Formula
  -> m ByteString
renderLatexSvg :: forall (m :: * -> *).
MonadLatexSvg m =>
Maybe EnvironmentOptions
-> Maybe FormulaOptions -> Formula -> m ByteString
renderLatexSvg Maybe EnvironmentOptions
env = EnvironmentOptions -> FormulaOptions -> Formula -> m ByteString
forall (m :: * -> *).
MonadLatexSvg m =>
EnvironmentOptions -> FormulaOptions -> Formula -> m ByteString
renderImage
  (EnvironmentOptions
-> Maybe EnvironmentOptions -> EnvironmentOptions
forall a. a -> Maybe a -> a
fromMaybe EnvironmentOptions
defaultEnv Maybe EnvironmentOptions
env)
  (FormulaOptions -> Formula -> m ByteString)
-> (Maybe FormulaOptions -> FormulaOptions)
-> Maybe FormulaOptions
-> Formula
-> m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FormulaOptions -> Maybe FormulaOptions -> FormulaOptions
forall a. a -> Maybe a -> a
fromMaybe FormulaOptions
defaultFormulaOptions