@@ -1,88 +1,7 @@
1−-- | Module containing various functions to manipulate contexts.
21module Text.Hakyll.Context
32 ( Context
4− , ContextManipulation
5− , renderValue
6− , changeValue
7− , copyValue
8− , renderDate
9− , changeExtension
103 ) where
114
12−import Control.Arrow (arr)
13−import System.Locale (defaultTimeLocale)
14−import System.FilePath (takeFileName, addExtension, dropExtension)
15−import Data.Time.Format (parseTime, formatTime)
16−import Data.Time.Clock (UTCTime)
17−import Data.Maybe (fromMaybe)
18−import qualified Data.Map as M
5+import Data.Map (Map)
196
20−import Text.Hakyll.Regex (substituteRegex)
21−import Text.Hakyll.HakyllAction (HakyllAction)
22−import Text.Hakyll.Hakyll (Context)
23−
24−-- | Type for context manipulating functions.
25−type ContextManipulation = Context -> Context
26−
27−-- | Do something with a value in a @Context@, but keep the old value as well.
28−-- This is probably the most common function to construct a
29−-- @ContextManipulation@.
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.
33− -> HakyllAction Context Context
34−renderValue source destination f = arr $ \context ->
35− case M.lookup source context of
36− Nothing -> context
37− (Just value) -> M.insert destination (f value) context
38−
39−-- | Change a value in a @Context@.
40−--
41−-- > import Data.Char (toUpper)
42−-- > changeValue "title" (map toUpper)
43−--
44−-- Will put the title in UPPERCASE.
45−changeValue :: String -- ^ Key to change.
46− -> (String -> String) -- ^ Function to apply on the value.
47− -> HakyllAction Context Context
48−changeValue key = renderValue key key
49−
50−-- | Copy a value from one key to another in a @Context@.
51−copyValue :: String -- ^ Source key.
52− -> String -- ^ Destination key.
53− -> HakyllAction Context Context
54−copyValue source destination = renderValue source destination id
55−
56−-- | When the context has a key called @path@ in a @yyyy-mm-dd-title.extension@
57−-- format (default for pages), this function can render the date.
58−--
59−-- > renderDate "date" "%B %e, %Y" "Date unknown"
60−--
61−-- Will render something like @January 32, 2010@.
62−renderDate :: String -- ^ Key in which the rendered date should be placed.
63− -> String -- ^ Format to use on the date.
64− -> String -- ^ Default key, in case the date cannot be parsed.
65− -> HakyllAction Context Context
66−renderDate key format defaultValue = renderValue "path" key renderDate'
67− where
68− renderDate' filePath = fromMaybe defaultValue $ do
69− let dateString = substituteRegex "^([0-9]*-[0-9]*-[0-9]*).*" "\\1"
70− (takeFileName filePath)
71− time <- parseTime defaultTimeLocale
72− "%Y-%m-%d"
73− dateString :: Maybe UTCTime
74− return $ formatTime defaultTimeLocale format time
75−
76−-- | Change the extension of a file. This is only needed when you want to
77−-- render, for example, mardown to @.php@ files instead of @.html@ files.
78−--
79−-- > renderChainWith (changeExtension "php")
80−-- > ["templates/default.html"]
81−-- > (createPagePath "test.markdown")
82−--
83−-- Will render to @test.php@ instead of @test.html@.
84−changeExtension :: String -- ^ Extension to change to.
85− -> HakyllAction Context Context
86−changeExtension extension = changeValue "url" changeExtension'
87− where
88− changeExtension' = flip addExtension extension . dropExtension
7+type Context = Map String String