{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
module Text.Pandoc.Filter.Pyplot.Types where
import Data.Char (toLower)
import Data.Default.Class (Default, def)
import Data.Hashable (Hashable, hashWithSalt)
import Data.Semigroup as Sem
import Data.Text (Text)
import Data.Yaml
import Text.Pandoc.Definition (Attr)
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)
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
, interpreter :: String
, flags :: [String]
}
deriving (Eq, Show)
instance Default Configuration where
def = Configuration {
defaultDirectory = "generated/"
, defaultIncludeScript = mempty
, defaultWithLinks = True
, defaultSaveFormat = PNG
, defaultDPI = 80
, interpreter = defaultPlatformInterpreter
, flags = mempty
}
instance ToJSON Configuration where
toJSON (Configuration dir' _ withLinks' savefmt' dpi' interp' flags') =
object [ "directory" .= dir'
, "include" .= ("example.py" :: FilePath)
, "links" .= withLinks'
, "dpi" .= dpi'
, "format" .= (toLower <$> show savefmt')
, "interpreter" .= interp'
, "flags" .= flags'
]
data FigureSpec = FigureSpec
{ caption :: String
, withLinks :: Bool
, script :: PythonScript
, saveFormat :: SaveFormat
, directory :: FilePath
, dpi :: Int
, blockAttrs :: Attr
}
instance Hashable FigureSpec where
hashWithSalt salt spec =
hashWithSalt salt ( caption spec
, script spec
, fromEnum . saveFormat $ spec
, directory spec, dpi spec
, blockAttrs spec
)