{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
module Text.Pandoc.Filter.Pyplot.Types where
import Control.Monad.Reader
import Data.Char (toLower)
import Data.Default.Class (Default, def)
import Data.Hashable (Hashable)
import Data.List (intersperse)
import Data.Semigroup as Sem
import Data.String (IsString(..))
import Data.Text (Text)
import Data.Yaml (ToJSON, object, toJSON, (.=))
import GHC.Generics (Generic)
import Text.Pandoc.Definition (Attr)
directoryKey, captionKey, dpiKey, includePathKey, saveFormatKey, withLinksKey, isTightBboxKey, isTransparentKey :: Text
directoryKey = "directory"
captionKey = "caption"
dpiKey = "dpi"
includePathKey = "include"
saveFormatKey = "format"
withLinksKey = "links"
isTightBboxKey = "tight_bbox"
isTransparentKey = "transparent"
inclusionKeys :: [Text]
inclusionKeys = [ directoryKey
, captionKey
, dpiKey
, includePathKey
, saveFormatKey
, withLinksKey
, isTightBboxKey
, isTransparentKey
]
type PyplotM a = ReaderT Configuration IO a
type PythonScript = Text
data RenderingLibrary
= Matplotlib
| Plotly
deriving (Show, Eq, Generic)
instance Hashable RenderingLibrary
data ScriptResult
= ScriptSuccess
| ScriptChecksFailed String
| ScriptFailure Int
data CheckResult
= CheckPassed
| CheckFailed String
deriving (Eq)
instance Sem.Semigroup CheckResult where
(<>) CheckPassed a = a
(<>) a CheckPassed = a
(<>) (CheckFailed msg1) (CheckFailed msg2) = CheckFailed (msg1 <> msg2)
instance Monoid CheckResult where
mempty = CheckPassed
#if !(MIN_VERSION_base(4,11,0))
mappend = (<>)
#endif
data PandocPyplotError
= ScriptError Int
| ScriptChecksFailedError String
deriving (Eq)
instance Show PandocPyplotError where
show (ScriptError exitcode) = "Script error: plot could not be generated. Exit code " <> (show exitcode)
show (ScriptChecksFailedError msg) = "Script did not pass all checks: " <> msg
data SaveFormat
= PNG
| PDF
| SVG
| JPG
| EPS
| GIF
| TIF
deriving (Bounded, Enum, Eq, Show, Generic)
instance IsString SaveFormat where
fromString s
| s `elem` ["png", "PNG", ".png"] = PNG
| s `elem` ["pdf", "PDF", ".pdf"] = PDF
| s `elem` ["svg", "SVG", ".svg"] = SVG
| s `elem` ["eps", "EPS", ".eps"] = EPS
| s `elem` ["gif", "GIF", ".gif"] = GIF
| s `elem` ["jpg", "jpeg", "JPG", "JPEG", ".jpg", ".jpeg"] = JPG
| s `elem` ["tif", "tiff", "TIF", "TIFF", ".tif", ".tiff"] = TIF
| otherwise = error $
mconcat [ s
, " is not one of valid save format : "
, mconcat $ intersperse ", " $ show <$> saveFormats
]
where
saveFormats = (enumFromTo minBound maxBound) :: [SaveFormat]
instance Hashable SaveFormat
extension :: SaveFormat -> String
extension fmt = mconcat [".", fmap toLower . show $ fmt]
defaultPlatformInterpreter :: String
#if defined(mingw32_HOST_OS)
defaultPlatformInterpreter = "python"
#else
defaultPlatformInterpreter = "python3"
#endif
data Configuration
= Configuration
{ defaultDirectory :: FilePath
, defaultIncludeScript :: PythonScript
, defaultWithLinks :: Bool
, defaultSaveFormat :: SaveFormat
, defaultDPI :: Int
, isTightBbox :: Bool
, isTransparent :: Bool
, interpreter :: String
, flags :: [String]
}
deriving (Eq, Show)
instance Default Configuration where
def = Configuration {
defaultDirectory = "generated/"
, defaultIncludeScript = mempty
, defaultWithLinks = True
, defaultSaveFormat = PNG
, defaultDPI = 80
, isTightBbox = False
, isTransparent = False
, interpreter = defaultPlatformInterpreter
, flags = mempty
}
instance ToJSON Configuration where
toJSON (Configuration dir' _ withLinks' savefmt' dpi' tightbbox' transparent' interp' flags') =
object [ directoryKey .= dir'
, includePathKey .= ("example.py" :: FilePath)
, withLinksKey .= withLinks'
, dpiKey .= dpi'
, saveFormatKey .= (fmap toLower . show $ savefmt')
, isTightBboxKey .= tightbbox'
, isTransparentKey .= transparent'
, "interpreter" .= interp'
, "flags" .= flags'
]
data FigureSpec = FigureSpec
{ caption :: Text
, withLinks :: Bool
, script :: PythonScript
, saveFormat :: SaveFormat
, directory :: FilePath
, dpi :: Int
, renderingLib :: RenderingLibrary
, tightBbox :: Bool
, transparent :: Bool
, blockAttrs :: Attr
} deriving Generic
instance Hashable FigureSpec