@@ -1,5 +1,6 @@
1−-- | This module exports a number of functions that produce @HakyllAction@s to
2−-- manipulate @Context@s.
1+-- | This module exports a number of functions to manipulate metadata of
2+-- resources
3+--
34module Text.Hakyll.ContextManipulations
45 ( renderValue
56 , changeValue
@@ -8,12 +9,8 @@ module Text.Hakyll.ContextManipulations
89 , renderDate
910 , renderDateWithLocale
1011 , changeExtension
11− , renderBody
12− , takeBody
1312 ) where
1413
15−import Control.Monad (liftM)
16−import Control.Arrow (arr)
1714import System.Locale (TimeLocale, defaultTimeLocale)
1815import System.FilePath (takeFileName, addExtension, dropExtension)
1916import Data.Time.Format (parseTime, formatTime)
@@ -22,8 +19,8 @@ import Data.Maybe (fromMaybe)
2219import qualified Data.Map as M
2320
2421import Text.Hakyll.Regex (substituteRegex)
25−import Text.Hakyll.HakyllAction (HakyllAction (..))
26−import Text.Hakyll.Context (Context (..))
22+import Text.Hakyll.Transformer (Transformer (..), transformMetadata)
23+import Text.Hakyll.Resource
2724
2825-- | Do something with a value in a @Context@, but keep the old value as well.
2926-- If the key given is not present in the @Context@, nothing will happen.
@@ -31,35 +28,37 @@ import Text.Hakyll.Context (Context (..))
3128renderValue :: String -- ^ Key of which the value should be copied.
3229 -> String -- ^ Key the value should be copied to.
3330 -> (String -> String) -- ^ Function to apply on the value.
34− -> HakyllAction Context Context
35−renderValue source destination f = arr $ \(Context context) -> Context $
36− case M.lookup source context of
37− Nothing -> context
38− (Just value) -> M.insert destination (f value) context
31+ -> Transformer a a -- ^ Resulting transformer
32+renderValue source destination f = transformMetadata $ \(Metadata m) ->
33+ Metadata $ case M.lookup source m of
34+ Nothing -> m
35+ (Just value) -> M.insert destination (f value) m
3936
40−-- | Change a value in a @Context@.
37+-- | Change a value in the metadata
4138--
4239-- > import Data.Char (toUpper)
4340-- > changeValue "title" (map toUpper)
4441--
4542-- Will put the title in UPPERCASE.
46−changeValue :: String -- ^ Key to change.
47− -> (String -> String) -- ^ Function to apply on the value.
48− -> HakyllAction Context Context
43+changeValue :: String -- ^ Key to change.
44+ -> (String -> String) -- ^ Function to apply on the value.
45+ -> Transformer a a
4946changeValue key = renderValue key key
5047
51−-- | Change the URL of a page. This requires a special function, so dependency
52−-- handling can happen correctly.
48+-- | Change the URL of a page. You should always use this function instead of
49+-- 'changeValue' for this, because using 'changeValue' might break dependency
50+-- handling when changing the @url@ field.
5351--
54−changeUrl :: (String -> String) -- ^ Function to change URL with.
55− -> HakyllAction Context Context -- ^ Resulting action.
56−changeUrl f = let action = changeValue "url" f
57− in action {actionUrl = Right $ liftM f}
52+changeUrl :: (String -> String) -- ^ Function to change URL with.
53+ -> Transformer a a -- ^ Resulting action.
54+changeUrl f = let t = changeValue "url" f
55+ in t {transformerUrl = return . f}
5856
59−-- | Copy a value from one key to another in a @Context@.
60−copyValue :: String -- ^ Source key.
61− -> String -- ^ Destination key.
62− -> HakyllAction Context Context
57+-- | Copy a metadata value from one key to another
58+--
59+copyValue :: String -- ^ Source key.
60+ -> String -- ^ Destination key.
61+ -> Transformer a a -- ^ Resulting transformer
6362copyValue source destination = renderValue source destination id
6463
6564-- | When the context has a key called @path@ in a
@@ -73,7 +72,7 @@ copyValue source destination = renderValue source destination id
7372renderDate :: String -- ^ Key in which the rendered date should be placed.
7473 -> String -- ^ Format to use on the date.
7574 -> String -- ^ Default key, in case the date cannot be parsed.
76− -> HakyllAction Context Context
75+ -> Transformer a a
7776renderDate = renderDateWithLocale defaultTimeLocale
7877
7978-- | This is an extended version of 'renderDate' that allows you to specify a
@@ -84,7 +83,7 @@ renderDateWithLocale :: TimeLocale -- ^ Output time locale.
8483 -> String -- ^ Destination key.
8584 -> String -- ^ Format to use on the date.
8685 -> String -- ^ Default key.
87− -> HakyllAction Context Context
86+ -> Transformer a a
8887renderDateWithLocale locale key format defaultValue =
8988 renderValue "path" key renderDate'
9089 where
@@ -102,23 +101,8 @@ renderDateWithLocale locale key format defaultValue =
102101-- > changeExtension "php"
103102--
104103-- Will render @test.markdown@ to @test.php@ instead of @test.html@.
105−changeExtension :: String -- ^ Extension to change to.
106− -> HakyllAction Context Context
104+changeExtension :: String -- ^ Extension to change to.
105+ -> Transformer a a -- ^ Resulting transformer
107106changeExtension extension = changeValue "url" changeExtension'
108107 where
109108 changeExtension' = flip addExtension extension . dropExtension
110−
111−-- | Change the body of a file using a certain manipulation.
112−--
113−-- > import Data.Char (toUpper)
114−-- > renderBody (map toUpper)
115−--
116−-- Will put the entire body of the page in UPPERCASE.
117−renderBody :: (String -> String)
118− -> HakyllAction Context Context
119−renderBody = renderValue "body" "body"
120−
121−-- | Get the resulting body text from a context
122−--
123−takeBody :: HakyllAction Context String
124−takeBody = arr $ fromMaybe "" . M.lookup "body" . unContext