diff options
author | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2010-11-12 09:10:03 +0100 |
---|---|---|
committer | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2010-11-12 09:10:03 +0100 |
commit | 50459dcad88df2900cc23f4a9877da47c3c3af20 (patch) | |
tree | 7a1483174dec99350476b83f07ee7c742533f4a3 | |
parent | 46de95a1b0ea809671291d24906120c7b5a3d741 (diff) | |
download | hakyll-50459dcad88df2900cc23f4a9877da47c3c3af20.tar.gz |
Add renderMissingValue function
-rw-r--r-- | src/Text/Hakyll/ContextManipulations.hs | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/src/Text/Hakyll/ContextManipulations.hs b/src/Text/Hakyll/ContextManipulations.hs index 46ee5ed..f73e88c 100644 --- a/src/Text/Hakyll/ContextManipulations.hs +++ b/src/Text/Hakyll/ContextManipulations.hs @@ -2,6 +2,7 @@ -- manipulate @Context@s. module Text.Hakyll.ContextManipulations ( renderValue + , renderMissingValue , changeValue , changeUrl , copyValue @@ -13,7 +14,7 @@ module Text.Hakyll.ContextManipulations ) where import Control.Monad (liftM) -import Control.Arrow (arr) +import Control.Arrow (arr, (>>>)) import System.Locale (TimeLocale, defaultTimeLocale) import System.FilePath (takeFileName, addExtension, dropExtension) import Data.Time.Format (parseTime, formatTime) @@ -27,14 +28,26 @@ import Text.Hakyll.Context (Context (..)) -- | Do something with a value in a @Context@, but keep the old value as well. -- If the key given is not present in the @Context@, nothing will happen. -renderValue :: String -- ^ Key of which the value should be copied. - -> String -- ^ Key the value should be copied to. - -> (String -> String) -- ^ Function to apply on the value. +-- +renderValue :: String -- ^ Key of which the value should be copied. + -> String -- ^ Key the value should be copied to. + -> (String -> String) -- ^ Function to apply on the value. -> HakyllAction Context Context -renderValue source destination f = arr $ \(Context context) -> Context $ +renderValue source destination f = + arr (Context . M.delete destination . unContext) + >>> renderMissingValue source destination f + +-- | Render a value, but do not overwrite the destination value if it already +-- exists. +-- +renderMissingValue :: String -- ^ Source key + -> String -- ^ Destination key + -> (String -> String) -- ^ Function to apply on the value + -> HakyllAction Context Context +renderMissingValue source destination f = arr $ \(Context context) -> Context $ case M.lookup source context of Nothing -> context - (Just value) -> M.insert destination (f value) context + (Just value) -> M.insertWith (flip const) destination (f value) context -- | Change a value in a @Context@. -- |