{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
module Text.Pandoc.Filter.Pyplot.Types where
import Data.Char (toLower)
import Data.Default.Class (Default, def)
import Data.Hashable (Hashable)
import Data.Semigroup as Sem
import Data.Text (Text, pack)
import Data.Yaml
import GHC.Generics (Generic)
import Text.Pandoc.Definition (Attr)
directoryKey, captionKey, dpiKey, includePathKey, saveFormatKey, withLinksKey, isTightBboxKey, isTransparentKey :: String
directoryKey = "directory"
captionKey = "caption"
dpiKey = "dpi"
includePathKey = "include"
saveFormatKey = "format"
withLinksKey = "links"
isTightBboxKey = "tight_bbox"
isTransparentKey = "transparent"
inclusionKeys :: [String]
inclusionKeys = [ directoryKey
, captionKey
, dpiKey
, includePathKey
, saveFormatKey
, withLinksKey
, isTightBboxKey
, isTransparentKey
]
type PythonScript = Text
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 Hashable SaveFormat
saveFormatFromString :: String -> Maybe SaveFormat
saveFormatFromString s
| s `elem` ["png", "PNG", ".png"] = Just PNG
| s `elem` ["pdf", "PDF", ".pdf"] = Just PDF
| s `elem` ["svg", "SVG", ".svg"] = Just SVG
| s `elem` ["eps", "EPS", ".eps"] = Just EPS
| s `elem` ["gif", "GIF", ".gif"] = Just GIF
| s `elem` ["jpg", "jpeg", "JPG", "JPEG", ".jpg", ".jpeg"] = Just JPG
| s `elem` ["tif", "tiff", "TIF", "TIFF", ".tif", ".tiff"] = Just TIF
| otherwise = Nothing
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 [ pack directoryKey .= dir'
, pack includePathKey .= ("example.py" :: FilePath)
, pack withLinksKey .= withLinks'
, pack dpiKey .= dpi'
, pack saveFormatKey .= (toLower <$> show savefmt')
, pack isTightBboxKey .= tightbbox'
, pack isTransparentKey .= transparent'
, "interpreter" .= interp'
, "flags" .= flags'
]
data FigureSpec = FigureSpec
{ caption :: String
, withLinks :: Bool
, script :: PythonScript
, saveFormat :: SaveFormat
, directory :: FilePath
, dpi :: Int
, tightBbox :: Bool
, transparent :: Bool
, blockAttrs :: Attr
} deriving Generic
instance Hashable FigureSpec