hakyll/src/Hakyll/Web/Template/Context.hs

90 lines
3 KiB
Haskell
Raw Normal View History

2012-11-10 12:26:39 +00:00
--------------------------------------------------------------------------------
module Hakyll.Web.Template.Context
2012-11-13 18:03:58 +00:00
( Context (..)
, mapContext
2012-11-10 12:26:39 +00:00
, field
2012-11-10 15:30:38 +00:00
, defaultContext
, bodyField
, urlField
, pathField
, categoryField
, titleField
2012-11-10 12:26:39 +00:00
) where
--------------------------------------------------------------------------------
2012-11-13 18:03:58 +00:00
import Control.Applicative (Alternative (..), (<$>))
import Data.Monoid (Monoid (..))
2012-11-10 17:11:46 +00:00
import System.FilePath (takeBaseName, takeDirectory)
2012-11-10 15:30:38 +00:00
--------------------------------------------------------------------------------
2012-11-10 12:26:39 +00:00
import Hakyll.Core.Compiler
2012-11-10 15:30:38 +00:00
import Hakyll.Core.Identifier
2012-11-10 17:11:46 +00:00
import Hakyll.Web.Page.Internal
2012-11-10 15:30:38 +00:00
import Hakyll.Web.Urls
2012-11-10 12:26:39 +00:00
--------------------------------------------------------------------------------
2012-11-13 18:03:58 +00:00
newtype Context a = Context
{ unContext :: String -> Identifier -> a -> Compiler String
}
--------------------------------------------------------------------------------
instance Monoid (Context a) where
mempty = Context $ \_ _ _ -> empty
mappend (Context f) (Context g) = Context $ \k i x -> f k i x <|> g k i x
--------------------------------------------------------------------------------
mapContext :: (String -> String) -> Context a -> Context a
mapContext f (Context g) = Context $ \k i x -> f <$> g k i x
2012-11-10 12:26:39 +00:00
--------------------------------------------------------------------------------
2012-11-13 16:31:03 +00:00
field :: String -> (Identifier -> a -> Compiler String) -> Context a
2012-11-13 18:03:58 +00:00
field key value = Context $ \k i x -> if k == key then value i x else empty
2012-11-10 15:30:38 +00:00
--------------------------------------------------------------------------------
2012-11-10 17:11:46 +00:00
defaultContext :: Context Page
2012-11-10 15:30:38 +00:00
defaultContext =
2012-11-13 18:03:58 +00:00
bodyField "body" `mappend`
urlField "url" `mappend`
pathField "path" `mappend`
categoryField "category" `mappend`
titleField "title" `mappend`
2012-11-10 17:11:46 +00:00
missingField
2012-11-10 15:30:38 +00:00
--------------------------------------------------------------------------------
2012-11-10 17:11:46 +00:00
bodyField :: String -> Context Page
2012-11-13 16:31:03 +00:00
bodyField key = field key $ \_ x -> return x
2012-11-10 15:30:38 +00:00
--------------------------------------------------------------------------------
2012-11-10 17:11:46 +00:00
urlField :: String -> Context a
2012-11-13 18:03:58 +00:00
urlField key = field key $ \i _ -> maybe empty toUrl <$> getRouteFor i
2012-11-10 15:30:38 +00:00
--------------------------------------------------------------------------------
2012-11-10 17:11:46 +00:00
pathField :: String -> Context a
2012-11-13 18:03:58 +00:00
pathField key = field key $ \i _ -> return $ toFilePath i
2012-11-10 15:30:38 +00:00
--------------------------------------------------------------------------------
2012-11-10 17:11:46 +00:00
categoryField :: String -> Context a
2012-11-13 18:03:58 +00:00
categoryField key = mapContext (takeBaseName . takeDirectory) $ pathField key
2012-11-10 15:30:38 +00:00
--------------------------------------------------------------------------------
2012-11-10 17:11:46 +00:00
titleField :: String -> Context a
2012-11-13 18:03:58 +00:00
titleField key = mapContext takeBaseName $ pathField key
2012-11-10 17:11:46 +00:00
--------------------------------------------------------------------------------
missingField :: Context a
2012-11-13 18:03:58 +00:00
missingField = Context $ \k _ _ -> return $ "$" ++ k ++ "$"