on master help sign in
Add renderMissingValue function
50459dcJasper Van der Jeugt
src/Text/Hakyll/ContextManipulations.hs+19−6
@@ -2,6 +2,7 @@
2-- manipulate @Context@s.
2-- manipulate @Context@s.
3module Text.Hakyll.ContextManipulations
3module Text.Hakyll.ContextManipulations
4 ( renderValue
4 ( renderValue
5+ , renderMissingValue
5 , changeValue
6 , changeValue
6 , changeUrl
7 , changeUrl
7 , copyValue
8 , copyValue
@@ -13,7 +14,7 @@ module Text.Hakyll.ContextManipulations
13 ) where
14 ) where
14
15
15import Control.Monad (liftM)
16import Control.Monad (liftM)
16−import Control.Arrow (arr)
17+import Control.Arrow (arr, (>>>))
17import System.Locale (TimeLocale, defaultTimeLocale)
18import System.Locale (TimeLocale, defaultTimeLocale)
18import System.FilePath (takeFileName, addExtension, dropExtension)
19import System.FilePath (takeFileName, addExtension, dropExtension)
19import Data.Time.Format (parseTime, formatTime)
20import Data.Time.Format (parseTime, formatTime)
@@ -27,14 +28,26 @@ import Text.Hakyll.Context (Context (..))
27
28
28-- | Do something with a value in a @Context@, but keep the old value as well.
29-- | Do something with a value in a @Context@, but keep the old value as well.
29-- If the key given is not present in the @Context@, nothing will happen.
30-- If the key given is not present in the @Context@, nothing will happen.
31+--
30−renderValue :: String -- ^ Key of which the value should be copied.
32+renderValue :: String -- ^ Key of which the value should be copied.
31− -> String -- ^ Key the value should be copied to.
33+ -> String -- ^ Key the value should be copied to.
32− -> (String -> String) -- ^ Function to apply on the value.
34+ -> (String -> String) -- ^ Function to apply on the value.
33 -> HakyllAction Context Context
35 -> 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 $
35 case M.lookup source context of
48 case M.lookup source context of
36 Nothing -> context
49 Nothing -> context
37− (Just value) -> M.insert destination (f value) context
50+ (Just value) -> M.insertWith (flip const) destination (f value) context
38
51
39-- | Change a value in a @Context@.
52-- | Change a value in a @Context@.
40--
53--