summaryrefslogtreecommitdiff
path: root/src/Hakyll/Web
diff options
context:
space:
mode:
Diffstat (limited to 'src/Hakyll/Web')
-rw-r--r--src/Hakyll/Web/Template/Context.hs50
1 files changed, 49 insertions, 1 deletions
diff --git a/src/Hakyll/Web/Template/Context.hs b/src/Hakyll/Web/Template/Context.hs
index 5ca1556..17db7ca 100644
--- a/src/Hakyll/Web/Template/Context.hs
+++ b/src/Hakyll/Web/Template/Context.hs
@@ -2,13 +2,26 @@
module Hakyll.Web.Template.Context
( Context
, field
+
+ , defaultContext
+ , bodyField
+ , urlField
+ , pathField
+ , categoryField
+ , titleField
) where
--------------------------------------------------------------------------------
-import Control.Applicative (empty)
+import Control.Applicative (empty, (<|>))
import Control.Arrow
+import System.FilePath (takeBaseName, takeDirectory)
+
+
+--------------------------------------------------------------------------------
import Hakyll.Core.Compiler
+import Hakyll.Core.Identifier
+import Hakyll.Web.Urls
--------------------------------------------------------------------------------
@@ -22,3 +35,38 @@ field key value = arr checkKey >>> empty ||| value
checkKey (k, x)
| k == key = Left ()
| otherwise = Right x
+
+
+--------------------------------------------------------------------------------
+defaultContext :: Context (Identifier String, String)
+defaultContext =
+ bodyField "body" <|>
+ urlField "url" <|>
+ pathField "path" <|>
+ categoryField "category" <|>
+ titleField "title"
+
+
+--------------------------------------------------------------------------------
+bodyField :: String -> Context (Identifier String, String)
+bodyField key = field key $ arr snd
+
+
+--------------------------------------------------------------------------------
+urlField :: String -> Context (Identifier a, a)
+urlField key = field key $ fst ^>> getRouteFor >>^ maybe empty toUrl
+
+
+--------------------------------------------------------------------------------
+pathField :: String -> Context (Identifier a, a)
+pathField key = field key $ arr $ toFilePath . fst
+
+
+--------------------------------------------------------------------------------
+categoryField :: String -> Context (Identifier a, a)
+categoryField key = pathField key >>^ (takeBaseName . takeDirectory)
+
+
+--------------------------------------------------------------------------------
+titleField :: String -> Context (Identifier a, a)
+titleField key = pathField key >>^ takeBaseName