summaryrefslogtreecommitdiff
path: root/src/Text/Hakyll/Context.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Text/Hakyll/Context.hs')
-rw-r--r--src/Text/Hakyll/Context.hs26
1 files changed, 14 insertions, 12 deletions
diff --git a/src/Text/Hakyll/Context.hs b/src/Text/Hakyll/Context.hs
index 22409bf..074c88f 100644
--- a/src/Text/Hakyll/Context.hs
+++ b/src/Text/Hakyll/Context.hs
@@ -1,20 +1,23 @@
-- | Module containing various functions to manipulate contexts.
module Text.Hakyll.Context
- ( ContextManipulation
+ ( Context
+ , ContextManipulation
, renderValue
, renderDate
) where
import qualified Data.Map as M
-import qualified Data.ByteString.Lazy.Char8 as B
+import Data.Map (Map)
import System.Locale (defaultTimeLocale)
import System.FilePath (takeFileName)
-import Text.Template (Context)
import Data.Time.Format (parseTime, formatTime)
import Data.Time.Clock (UTCTime)
import Data.Maybe (fromMaybe)
-import Text.Hakyll.Regex (substitute)
+import Text.Hakyll.Regex (substituteRegex)
+
+-- | Type for a context.
+type Context = Map String String
-- | Type for context manipulating functions.
type ContextManipulation = Context -> Context
@@ -22,11 +25,11 @@ type ContextManipulation = Context -> Context
-- | Do something with a value of a context.
renderValue :: String -- ^ Key of which the value should be copied.
-> String -- ^ Key the value should be copied to.
- -> (B.ByteString -> B.ByteString) -- ^ Function to apply on the value.
+ -> (String -> String) -- ^ Function to apply on the value.
-> ContextManipulation
-renderValue src dst f context = case M.lookup (B.pack src) context of
+renderValue src dst f context = case M.lookup src context of
Nothing -> context
- (Just value) -> M.insert (B.pack dst) (f value) context
+ (Just value) -> M.insert dst (f value) context
-- | 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.
@@ -34,12 +37,11 @@ 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.
-> ContextManipulation
-renderDate key format defaultValue context =
- M.insert (B.pack key) (B.pack value) context
+renderDate key format defaultValue context = M.insert key value context
where value = fromMaybe defaultValue pretty
- pretty = do filePath <- M.lookup (B.pack "path") context
- let dateString = substitute "^([0-9]*-[0-9]*-[0-9]*).*" "\\1"
- (takeFileName $ B.unpack filePath)
+ pretty = do filePath <- M.lookup "path" context
+ let dateString = substituteRegex "^([0-9]*-[0-9]*-[0-9]*).*" "\\1"
+ (takeFileName filePath)
time <- parseTime defaultTimeLocale
"%Y-%m-%d"
dateString :: Maybe UTCTime