diff options
Diffstat (limited to 'src/Text/Hakyll/Context.hs')
-rw-r--r-- | src/Text/Hakyll/Context.hs | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/src/Text/Hakyll/Context.hs b/src/Text/Hakyll/Context.hs index d4d3436..11cc123 100644 --- a/src/Text/Hakyll/Context.hs +++ b/src/Text/Hakyll/Context.hs @@ -3,6 +3,7 @@ module Text.Hakyll.Context ( Context , ContextManipulation , renderValue + , changeValue , renderDate , changeExtension ) where @@ -23,7 +24,9 @@ type Context = Map String String -- | Type for context manipulating functions. type ContextManipulation = Context -> Context --- | Do something with a value of a context. +-- | Do something with a value in a "Context", but keep the old value as well. +-- This is probably the most common function to construct a +-- "ContextManipulation". 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. @@ -32,8 +35,23 @@ renderValue src dst f context = case M.lookup src context of Nothing -> context (Just value) -> M.insert dst (f value) context +-- | Change a value in a "Context". +-- +-- > import Data.Char (toUpper) +-- > changeValue "title" (map toUpper) +-- +-- Will put the title in UPPERCASE. +changeValue :: String -- ^ Key of which the value should be changed. + -> (String -> String) -- ^ Function to apply on the value. + -> ContextManipulation +changeValue key = renderValue key key + -- | When the context has a key called @path@ in a @yyyy-mm-dd-title.extension@ -- format (default for pages), this function can render the date. +-- +-- > renderDate "date" "%B %e, %Y" "Date unknown" +-- +-- Will render something like @January 32, 2010@. renderDate :: String -- ^ Key in which the rendered date should be placed. -> String -- ^ Format to use on the date. -> String -- ^ Default value when the date cannot be parsed. @@ -57,7 +75,8 @@ renderDate key format defaultValue context = M.insert key value context -- > (createPagePath "test.markdown") -- -- Will render to @test.php@ instead of @test.html@. -changeExtension :: String -> ContextManipulation -changeExtension extension = renderValue "url" "url" changeExtension' +changeExtension :: String -- ^ Extension to change to. + -> ContextManipulation +changeExtension extension = changeValue "url" changeExtension' where changeExtension' = flip addExtension extension . dropExtension |