@@ -1,5 +1,6 @@
1−-- | This module exports a number of functions that produce @HakyllAction@s to
1+-- | This module exports a number of functions to manipulate metadata of
2−-- manipulate @Context@s.
2+-- resources
3module Text.Hakyll.ContextManipulations
4module Text.Hakyll.ContextManipulations
4 ( renderValue
5 ( renderValue
5 , changeValue
6 , changeValue
@@ -8,12 +9,8 @@ module Text.Hakyll.ContextManipulations
8 , renderDate
9 , renderDate
9 , renderDateWithLocale
10 , renderDateWithLocale
10 , changeExtension
11 , changeExtension
15−import Control.Monad (liftM)
16−import Control.Arrow (arr)
17import System.Locale (TimeLocale, defaultTimeLocale)
14import System.Locale (TimeLocale, defaultTimeLocale)
18import System.FilePath (takeFileName, addExtension, dropExtension)
15import System.FilePath (takeFileName, addExtension, dropExtension)
19import Data.Time.Format (parseTime, formatTime)
16import Data.Time.Format (parseTime, formatTime)
@@ -22,8 +19,8 @@ import Data.Maybe (fromMaybe)
22import qualified Data.Map as M
19import qualified Data.Map as M
24import Text.Hakyll.Regex (substituteRegex)
21import Text.Hakyll.Regex (substituteRegex)
25−import Text.Hakyll.HakyllAction (HakyllAction (..))
22+import Text.Hakyll.Transformer (Transformer (..), transformMetadata)
26−import Text.Hakyll.Context (Context (..))
23+import Text.Hakyll.Resource
28-- | Do something with a value in a @Context@, but keep the old value as well.
25-- | 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.
26-- If the key given is not present in the @Context@, nothing will happen.
@@ -31,35 +28,37 @@ import Text.Hakyll.Context (Context (..))
31renderValue :: String -- ^ Key of which the value should be copied.
28renderValue :: String -- ^ Key of which the value should be copied.
32 -> String -- ^ Key the value should be copied to.
29 -> String -- ^ Key the value should be copied to.
33 -> (String -> String) -- ^ Function to apply on the value.
30 -> (String -> String) -- ^ Function to apply on the value.
34− -> HakyllAction Context Context
31+ -> Transformer a a -- ^ Resulting transformer
35−renderValue source destination f = arr $ \(Context context) -> Context $
32+renderValue source destination f = transformMetadata $ \(Metadata m) ->
36− case M.lookup source context of
33+ Metadata $ case M.lookup source m of
37− Nothing -> context
34+ Nothing -> m
38− (Just value) -> M.insert destination (f value) context
35+ (Just value) -> M.insert destination (f value) m
40−-- | Change a value in a @Context@.
37+-- | Change a value in the metadata
42-- > import Data.Char (toUpper)
39-- > import Data.Char (toUpper)
43-- > changeValue "title" (map toUpper)
40-- > changeValue "title" (map toUpper)
45-- Will put the title in UPPERCASE.
42-- Will put the title in UPPERCASE.
46−changeValue :: String -- ^ Key to change.
43+changeValue :: String -- ^ Key to change.
47− -> (String -> String) -- ^ Function to apply on the value.
44+ -> (String -> String) -- ^ Function to apply on the value.
48− -> HakyllAction Context Context
45+ -> Transformer a a
49changeValue key = renderValue key key
46changeValue key = renderValue key key
51−-- | Change the URL of a page. This requires a special function, so dependency
48+-- | Change the URL of a page. You should always use this function instead of
52−-- handling can happen correctly.
49+-- 'changeValue' for this, because using 'changeValue' might break dependency
50+-- handling when changing the @url@ field.
54−changeUrl :: (String -> String) -- ^ Function to change URL with.
52+changeUrl :: (String -> String) -- ^ Function to change URL with.
55− -> HakyllAction Context Context -- ^ Resulting action.
53+ -> Transformer a a -- ^ Resulting action.
56−changeUrl f = let action = changeValue "url" f
54+changeUrl f = let t = changeValue "url" f
57− in action {actionUrl = Right $ liftM f}
55+ in t {transformerUrl = return . f}
59−-- | Copy a value from one key to another in a @Context@.
57+-- | Copy a metadata value from one key to another
60−copyValue :: String -- ^ Source key.
59+copyValue :: String -- ^ Source key.
61− -> String -- ^ Destination key.
60+ -> String -- ^ Destination key.
62− -> HakyllAction Context Context
61+ -> Transformer a a -- ^ Resulting transformer
63copyValue source destination = renderValue source destination id
62copyValue source destination = renderValue source destination id
65-- | When the context has a key called @path@ in a
64-- | When the context has a key called @path@ in a
@@ -73,7 +72,7 @@ copyValue source destination = renderValue source destination id
73renderDate :: String -- ^ Key in which the rendered date should be placed.
72renderDate :: String -- ^ Key in which the rendered date should be placed.
74 -> String -- ^ Format to use on the date.
73 -> String -- ^ Format to use on the date.
75 -> String -- ^ Default key, in case the date cannot be parsed.
74 -> String -- ^ Default key, in case the date cannot be parsed.
76− -> HakyllAction Context Context
75+ -> Transformer a a
77renderDate = renderDateWithLocale defaultTimeLocale
76renderDate = renderDateWithLocale defaultTimeLocale
79-- | This is an extended version of 'renderDate' that allows you to specify a
78-- | This is an extended version of 'renderDate' that allows you to specify a
@@ -84,7 +83,7 @@ renderDateWithLocale :: TimeLocale -- ^ Output time locale.
84 -> String -- ^ Destination key.
83 -> String -- ^ Destination key.
85 -> String -- ^ Format to use on the date.
84 -> String -- ^ Format to use on the date.
86 -> String -- ^ Default key.
85 -> String -- ^ Default key.
87− -> HakyllAction Context Context
86+ -> Transformer a a
88renderDateWithLocale locale key format defaultValue =
87renderDateWithLocale locale key format defaultValue =
89 renderValue "path" key renderDate'
88 renderValue "path" key renderDate'
@@ -102,23 +101,8 @@ renderDateWithLocale locale key format defaultValue =
102-- > changeExtension "php"
101-- > changeExtension "php"
104-- Will render @test.markdown@ to @test.php@ instead of @test.html@.
103-- Will render @test.markdown@ to @test.php@ instead of @test.html@.
105−changeExtension :: String -- ^ Extension to change to.
104+changeExtension :: String -- ^ Extension to change to.
106− -> HakyllAction Context Context
105+ -> Transformer a a -- ^ Resulting transformer
107changeExtension extension = changeValue "url" changeExtension'
106changeExtension extension = changeValue "url" changeExtension'
109 changeExtension' = flip addExtension extension . dropExtension
108 changeExtension' = flip addExtension extension . dropExtension
111−-- | Change the body of a file using a certain manipulation.
113−-- > import Data.Char (toUpper)
114−-- > renderBody (map toUpper)
116−-- Will put the entire body of the page in UPPERCASE.
117−renderBody :: (String -> String)
118− -> HakyllAction Context Context
119−renderBody = renderValue "body" "body"
121−-- | Get the resulting body text from a context
123−takeBody :: HakyllAction Context String
124−takeBody = arr $ fromMaybe "" . M.lookup "body" . unContext