summaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Filter
diff options
context:
space:
mode:
authorLaurentRDC <>2019-06-04 18:23:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2019-06-04 18:23:00 (GMT)
commit2a1b2400f81e0e8df8158eacbbd3e87520373f00 (patch)
tree204148ae53713fc516fe6361ead61b4d50cade9f /src/Text/Pandoc/Filter
parentea33899f29e83431ba6ff02c9c2acf172bc75384 (diff)
version 2.1.3.02.1.3.0
Diffstat (limited to 'src/Text/Pandoc/Filter')
-rw-r--r--src/Text/Pandoc/Filter/Pyplot.hs12
-rw-r--r--src/Text/Pandoc/Filter/Pyplot/Configuration.hs24
-rw-r--r--src/Text/Pandoc/Filter/Pyplot/FigureSpec.hs3
-rw-r--r--src/Text/Pandoc/Filter/Pyplot/Types.hs22
4 files changed, 55 insertions, 6 deletions
diff --git a/src/Text/Pandoc/Filter/Pyplot.hs b/src/Text/Pandoc/Filter/Pyplot.hs
index 13b22c0..ced2a30 100644
--- a/src/Text/Pandoc/Filter/Pyplot.hs
+++ b/src/Text/Pandoc/Filter/Pyplot.hs
@@ -137,6 +137,13 @@ import Text.Pandoc.Filter.Pyplot.Internal
filterClass :: String
filterClass = "pyplot"
+
+-- | Flexible boolean parsing
+readBool :: String -> Bool
+readBool s | s `elem` ["True", "true", "'True'", "'true'", "1"] = True
+ | s `elem` ["False", "false", "'False'", "'false'", "0"] = False
+ | otherwise = error $ mconcat ["Could not parse '", s, "' into a boolean. Please use 'True' or 'False'"]
+
-- | Determine inclusion specifications from Block attributes.
-- Note that the @".pyplot"@ class is required, but all other parameters are optional
parseFigureSpec :: Configuration -> Block -> IO (Maybe FigureSpec)
@@ -157,9 +164,10 @@ parseFigureSpec config (CodeBlock (id', cls, attrs) content)
format = fromMaybe (defaultSaveFormat config) $ join $ saveFormatFromString <$> Map.lookup saveFormatKey attrs'
dir = makeValid $ Map.findWithDefault (defaultDirectory config) directoryKey attrs'
dpi' = fromMaybe (defaultDPI config) $ read <$> Map.lookup dpiKey attrs'
+ withLinks' = fromMaybe (defaultWithLinks config) $ readBool <$> Map.lookup withLinksKey attrs'
blockAttrs' = (id', filter (/= filterClass) cls, filteredAttrs)
- return $ FigureSpec caption' fullScript format dir dpi' blockAttrs'
-
+ return $ FigureSpec caption' withLinks' fullScript format dir dpi' blockAttrs'
+
parseFigureSpec _ _ = return Nothing
-- | Main routine to include Matplotlib plots.
diff --git a/src/Text/Pandoc/Filter/Pyplot/Configuration.hs b/src/Text/Pandoc/Filter/Pyplot/Configuration.hs
index c8766ef..c2b4152 100644
--- a/src/Text/Pandoc/Filter/Pyplot/Configuration.hs
+++ b/src/Text/Pandoc/Filter/Pyplot/Configuration.hs
@@ -13,12 +13,14 @@ 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
) where
import Data.Maybe (fromMaybe)
@@ -28,15 +30,18 @@ import qualified Data.Text.IO as T
import Data.Yaml
import Data.Yaml.Config (loadYamlSettings, ignoreEnv)
+import System.Directory (doesFileExist)
+
import Text.Pandoc.Filter.Pyplot.Types
-- | Keys that pandoc-pyplot will look for in code blocks. These are only exported for testing purposes.
-directoryKey, captionKey, dpiKey, includePathKey, saveFormatKey :: String
+directoryKey, captionKey, dpiKey, includePathKey, saveFormatKey, withLinksKey :: String
directoryKey = "directory"
captionKey = "caption"
dpiKey = "dpi"
includePathKey = "include"
saveFormatKey = "format"
+withLinksKey = "with-links"
-- | list of all keys related to pandoc-pyplot.
inclusionKeys :: [String]
@@ -45,6 +50,7 @@ inclusionKeys = [ directoryKey
, dpiKey
, includePathKey
, saveFormatKey
+ , withLinksKey
]
-- A @Configuration@ cannot be directly created from a YAML file
@@ -60,6 +66,7 @@ data ConfigPrecursor
= ConfigPrecursor
{ defaultDirectory_ :: FilePath
, defaultIncludePath_ :: Maybe FilePath
+ , defaultWithLinks_ :: Bool
, defaultSaveFormat_ :: String
, defaultDPI_ :: Int
, interpreter_ :: String
@@ -70,6 +77,7 @@ instance FromJSON ConfigPrecursor where
parseJSON (Object v) = ConfigPrecursor
<$> v .:? (T.pack directoryKey) .!= (defaultDirectory def)
<*> v .:? (T.pack includePathKey)
+ <*> v .:? (T.pack withLinksKey) .!= (defaultWithLinks def)
<*> v .:? (T.pack saveFormatKey) .!= (extension $ defaultSaveFormat def)
<*> v .:? (T.pack dpiKey) .!= (defaultDPI def)
<*> v .:? "interpreter" .!= (interpreter def)
@@ -84,6 +92,7 @@ renderConfiguration prec = do
return $ Configuration { defaultDirectory = defaultDirectory_ prec
, defaultIncludeScript = includeScript
, defaultSaveFormat = saveFormat'
+ , defaultWithLinks = defaultWithLinks_ prec
, defaultDPI = defaultDPI_ prec
, interpreter = interpreter_ prec
, flags = flags_ prec
@@ -98,4 +107,15 @@ renderConfiguration prec = do
--
-- @since 2.1.0.0
configuration :: FilePath -> IO Configuration
-configuration fp = loadYamlSettings [fp] [] ignoreEnv >>= renderConfiguration \ No newline at end of file
+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 \ No newline at end of file
diff --git a/src/Text/Pandoc/Filter/Pyplot/FigureSpec.hs b/src/Text/Pandoc/Filter/Pyplot/FigureSpec.hs
index 3cc8d3f..64b1d02 100644
--- a/src/Text/Pandoc/Filter/Pyplot/FigureSpec.hs
+++ b/src/Text/Pandoc/Filter/Pyplot/FigureSpec.hs
@@ -76,11 +76,12 @@ toImage spec = head . toList $ para $ imageWith attrs' target' "fig:" caption'
where
attrs' = blockAttrs spec
target' = figurePath spec
+ withLinks' = withLinks spec
srcLink = link (replaceExtension target' ".txt") mempty "Source code"
hiresLink = link (hiresFigurePath spec) mempty "high res."
captionText = fromList $ fromMaybe mempty (captionReader $ caption spec)
captionLinks = mconcat [" (", srcLink, ", ", hiresLink, ")"]
- caption' = captionText <> captionLinks
+ caption' = if withLinks' then captionText <> captionLinks else captionText
-- | Determine the path a figure should have.
figurePath :: FigureSpec -> FilePath
diff --git a/src/Text/Pandoc/Filter/Pyplot/Types.hs b/src/Text/Pandoc/Filter/Pyplot/Types.hs
index faf6616..dd368d4 100644
--- a/src/Text/Pandoc/Filter/Pyplot/Types.hs
+++ b/src/Text/Pandoc/Filter/Pyplot/Types.hs
@@ -1,4 +1,5 @@
-{-# LANGUAGE CPP #-}
+{-# LANGUAGE CPP #-}
+{-# LANGUAGE OverloadedStrings #-}
{-|
Module : Text.Pandoc.Filter.Pyplot.Types
Copyright : (c) Laurent P René de Cotret, 2019
@@ -17,6 +18,7 @@ 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)
@@ -120,6 +122,7 @@ data Configuration
= Configuration
{ defaultDirectory :: FilePath -- ^ The default directory where figures will be saved.
, defaultIncludeScript :: PythonScript -- ^ The default script to run before other instructions.
+ , defaultWithLinks :: Bool -- ^ The default behavior of whether or not to include links to source code and high-res
, defaultSaveFormat :: SaveFormat -- ^ The default save format of generated figures.
, defaultDPI :: Int -- ^ The default dots-per-inch value for generated figures.
, interpreter :: String -- ^ The name of the interpreter to use to render figures.
@@ -131,16 +134,31 @@ 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') =
+ -- We ignore the include script as we want to examplify that
+ -- this is for a filepath
+ object [ "directory" .= dir'
+ , "include" .= ("example.py" :: FilePath)
+ , "with-links" .= withLinks'
+ , "dpi" .= dpi'
+ , "format" .= (toLower <$> show savefmt')
+ , "interpreter" .= interp'
+ , "flags" .= flags'
+ ]
+
-- | Datatype containing all parameters required to run pandoc-pyplot
data FigureSpec = FigureSpec
{ caption :: String -- ^ Figure caption.
+ , withLinks :: Bool -- ^ Append links to source code and high-dpi figure to caption
, script :: PythonScript -- ^ Source code for the figure.
, saveFormat :: SaveFormat -- ^ Save format of the figure
, directory :: FilePath -- ^ Directory where to save the file
@@ -150,6 +168,8 @@ data FigureSpec = FigureSpec
instance Hashable FigureSpec where
hashWithSalt salt spec =
+ -- Some things are not included in the hash because they do not affect the outcome
+ -- of running scripts, e.g. whether links should be shown or not.
hashWithSalt salt ( caption spec
, script spec
, fromEnum . saveFormat $ spec