summaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Filter/Pyplot/Configuration.hs
blob: 7b212310f7c54a4464540e4364537adef5502a32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
{-# LANGUAGE OverloadedStrings #-}
{-|
Module      : $header$
Copyright   : (c) Laurent P René de Cotret, 2019
License     : MIT
Maintainer  : laurent.decotret@outlook.com
Stability   : internal
Portability : portable

Configuration for pandoc-pyplot
-}

module Text.Pandoc.Filter.Pyplot.Configuration (
      configuration
    -- * For testing and internal purposes only
    , writeConfig
    , inclusionKeys
    , directoryKey
    , captionKey
    , dpiKey
    , includePathKey
    , saveFormatKey
    , withLinksKey
    , isTightBboxKey
    , isTransparentKey
) where

import           Data.Default.Class              (def)
import           Data.Maybe                      (fromMaybe)
import           Data.String                     (fromString)
import qualified Data.Text.IO                    as TIO
import           Data.Yaml
import           Data.Yaml.Config                (ignoreEnv, loadYamlSettings)

import           System.Directory                (doesFileExist)

import           Text.Pandoc.Filter.Pyplot.Types

-- | A @Configuration@ cannot be directly created from a YAML file
-- for two reasons:
--
--     * we want to store an include script. However, it makes more sense to
--       specify the script path in a YAML file.
--     * Save format is best specified by a string, and this must be parsed later
--
-- Therefore, we have another type, ConfigPrecursor, which CAN be created directly from
-- a YAML file.
data ConfigPrecursor
    = ConfigPrecursor
        { defaultDirectory_   :: FilePath
        , defaultIncludePath_ :: Maybe FilePath
        , defaultWithLinks_   :: Bool
        , defaultSaveFormat_  :: String
        , defaultDPI_         :: Int
        , tightBbox_          :: Bool
        , transparent_        :: Bool
        , interpreter_        :: String
        , flags_              :: [String]
        }

instance FromJSON ConfigPrecursor where
    parseJSON (Object v) =
        ConfigPrecursor
            <$> v .:? directoryKey     .!= (defaultDirectory def)
            <*> v .:? includePathKey
            <*> v .:? withLinksKey     .!= (defaultWithLinks def)
            <*> v .:? saveFormatKey    .!= (show $ defaultSaveFormat def)
            <*> v .:? dpiKey           .!= (defaultDPI def)
            <*> v .:? isTightBboxKey   .!= (isTightBbox def)
            <*> v .:? isTransparentKey .!= (isTransparent def)
            <*> v .:? "interpreter"    .!= (interpreter def)
            <*> v .:? "flags"          .!= (flags def)

    parseJSON _ = fail "Could not parse the configuration"


renderConfiguration :: ConfigPrecursor -> IO Configuration
renderConfiguration prec = do
    includeScript <- fromMaybe mempty $ TIO.readFile <$> defaultIncludePath_ prec
    let saveFormat' = fromString $ defaultSaveFormat_ prec
    return $ Configuration
        { defaultDirectory     = defaultDirectory_ prec
        , defaultIncludeScript = includeScript
        , defaultSaveFormat    = saveFormat'
        , defaultWithLinks     = defaultWithLinks_ prec
        , defaultDPI           = defaultDPI_ prec
        , isTightBbox          = tightBbox_ prec
        , isTransparent        = transparent_ prec
        , interpreter          = interpreter_ prec
        , flags                = flags_ prec
        }


-- | Building configuration from a YAML file. The
-- keys are exactly the same as for Markdown code blocks.
--
-- If a key is either not present or unreadable, its value will be set
-- to the default value.
--
-- @since 2.1.0.0
configuration :: FilePath -> IO Configuration
configuration fp = loadYamlSettings [fp] [] ignoreEnv >>= renderConfiguration


-- | Write a configuration to file. An exception will be raised in case the file would be overwritten.
--
-- @since 2.1.3.0
writeConfig :: FilePath -> Configuration -> IO ()
writeConfig fp config = do
    fileExists <- doesFileExist fp
    if fileExists
        then error $ mconcat ["File ", fp, " already exists."]
        else encodeFile fp config