module Text.Pandoc.Filter.Pyplot.Types where
import Data.Char (toLower)
import Data.Default.Class (Default, def)
import Data.Hashable (Hashable, hashWithSalt)
import Data.Text (Text)
import Text.Pandoc.Definition (Attr)
type PythonScript = Text
data ScriptResult
= ScriptSuccess
| ScriptFailure Int
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]
data Configuration
= Configuration
{ defaultDirectory :: FilePath
, defaultIncludeScript :: PythonScript
, defaultSaveFormat :: SaveFormat
, defaultDPI :: Int
, interpreter :: String
}
deriving (Eq, Show)
instance Default Configuration where
def = Configuration {
defaultDirectory = "generated/"
, defaultIncludeScript = mempty
, defaultSaveFormat = PNG
, defaultDPI = 80
, interpreter = "python"
}
data FigureSpec = FigureSpec
{ caption :: String
, 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
)