2012-11-10 12:26:39 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
module Hakyll.Web.Template.Context
|
|
|
|
( Context
|
|
|
|
, 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-10 17:11:46 +00:00
|
|
|
import Control.Applicative (empty, (<|>))
|
2012-11-10 12:26:39 +00:00
|
|
|
import Control.Arrow
|
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 16:31:03 +00:00
|
|
|
type Context a = String -> Identifier -> a -> Compiler String
|
2012-11-10 12:26:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2012-11-13 16:31:03 +00:00
|
|
|
field :: String -> (Identifier -> a -> Compiler String) -> Context a
|
|
|
|
field key value k' id' x
|
|
|
|
| k' == key = value id' x
|
|
|
|
| otherwise = 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 =
|
|
|
|
bodyField "body" <|>
|
|
|
|
urlField "url" <|>
|
|
|
|
pathField "path" <|>
|
|
|
|
categoryField "category" <|>
|
2012-11-10 17:11:46 +00:00
|
|
|
titleField "title" <|>
|
|
|
|
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-10 15:30:38 +00:00
|
|
|
urlField key = field key $ fst ^>> getRouteFor >>^ maybe empty toUrl
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2012-11-10 17:11:46 +00:00
|
|
|
pathField :: String -> Context a
|
2012-11-10 15:30:38 +00:00
|
|
|
pathField key = field key $ arr $ toFilePath . fst
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2012-11-10 17:11:46 +00:00
|
|
|
categoryField :: String -> Context a
|
2012-11-10 15:30:38 +00:00
|
|
|
categoryField key = pathField key >>^ (takeBaseName . takeDirectory)
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2012-11-10 17:11:46 +00:00
|
|
|
titleField :: String -> Context a
|
2012-11-10 15:30:38 +00:00
|
|
|
titleField key = pathField key >>^ takeBaseName
|
2012-11-10 17:11:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
missingField :: Context a
|
|
|
|
missingField = arr $ \(k, _) -> "$" ++ k ++ "$"
|