@@ -2,6 +2,7 @@
22-- manipulate @Context@s.
33module Text.Hakyll.ContextManipulations
44 ( renderValue
5+ , renderMissingValue
56 , changeValue
67 , changeUrl
78 , copyValue
@@ -13,7 +14,7 @@ module Text.Hakyll.ContextManipulations
1314 ) where
1415
1516import Control.Monad (liftM)
16−import Control.Arrow (arr)
17+import Control.Arrow (arr, (>>>))
1718import System.Locale (TimeLocale, defaultTimeLocale)
1819import System.FilePath (takeFileName, addExtension, dropExtension)
1920import Data.Time.Format (parseTime, formatTime)
@@ -27,14 +28,26 @@ import Text.Hakyll.Context (Context (..))
2728
2829-- | Do something with a value in a @Context@, but keep the old value as well.
2930-- If the key given is not present in the @Context@, nothing will happen.
30−renderValue :: String -- ^ Key of which the value should be copied.
31− -> String -- ^ Key the value should be copied to.
32− -> (String -> String) -- ^ Function to apply on the value.
31+--
32+renderValue :: String -- ^ Key of which the value should be copied.
33+ -> String -- ^ Key the value should be copied to.
34+ -> (String -> String) -- ^ Function to apply on the value.
3335 -> HakyllAction Context Context
34−renderValue source destination f = arr $ \(Context context) -> Context $
36+renderValue source destination f =
37+ arr (Context . M.delete destination . unContext)
38+ >>> renderMissingValue source destination f
39+
40+-- | Render a value, but do not overwrite the destination value if it already
41+-- exists.
42+--
43+renderMissingValue :: String -- ^ Source key
44+ -> String -- ^ Destination key
45+ -> (String -> String) -- ^ Function to apply on the value
46+ -> HakyllAction Context Context
47+renderMissingValue source destination f = arr $ \(Context context) -> Context $
3548 case M.lookup source context of
3649 Nothing -> context
37− (Just value) -> M.insert destination (f value) context
50+ (Just value) -> M.insertWith (flip const) destination (f value) context
3851
3952-- | Change a value in a @Context@.
4053--