@@ -1,35 +1,59 @@
1+{-# LANGUAGE OverloadedStrings #-}
12module Main where
23
3−import Text.Hakyll (hakyll)
4−import Text.Hakyll.Render
5−import Text.Hakyll.Context
6−import Text.Hakyll.File (getRecursiveContents, directory)
7−import Text.Hakyll.CreateContext (createPage, createCustomPage, createListing)
8−import Data.List (sort)
9−import Control.Monad (forM_, liftM)
10−import Control.Monad.Reader (liftIO)
11−import Data.Either (Either(..))
12−
13−main = hakyll "http://example.com" $ do
14− -- Static directory.
15− directory css "css"
16−
17− -- Find all post paths.
18− postPaths <- liftM (reverse . sort) $ getRecursiveContents "posts"
19− let postPages = map createPage postPaths
20−
21− -- Render index, including recent posts.
22− let index = createListing "index.html" ["templates/postitem.html"]
23− (take 3 postPages) [("title", Left "Home")]
24− renderChain ["index.html", "templates/default.html"] index
25−
26− -- Render all posts list.
27− let posts = createListing "posts.html" ["templates/postitem.html"]
28− postPages [("title", Left "All posts")]
29− renderChain ["posts.html", "templates/default.html"] posts
30−
31− -- Render all posts.
32− liftIO $ putStrLn "Generating posts..."
33− forM_ postPages $ renderChain [ "templates/post.html"
34− , "templates/default.html"
35− ]
4+import Prelude hiding (id)
5+import Control.Category (id)
6+import Control.Arrow ((>>>), (***), arr)
7+import Data.Monoid (mempty, mconcat)
8+
9+import Hakyll
10+
11+main :: IO ()
12+main = hakyll $ do
13+ -- Compress CSS
14+ route "css/*" idRoute
15+ compile "css/*" compressCssCompiler
16+
17+ -- Render posts
18+ route "posts/*" $ setExtension ".html"
19+ compile "posts/*" $
20+ pageCompiler
21+ >>> applyTemplateCompiler "templates/post.html"
22+ >>> applyTemplateCompiler "templates/default.html"
23+ >>> relativizeUrlsCompiler
24+
25+ -- Render posts list
26+ route "posts.html" $ idRoute
27+ create "posts.html" $
28+ constA mempty
29+ >>> arr (setField "title" "All posts")
30+ >>> requireAllA "posts/*" addPostList
31+ >>> applyTemplateCompiler "templates/posts.html"
32+ >>> applyTemplateCompiler "templates/default.html"
33+ >>> relativizeUrlsCompiler
34+
35+ -- Index
36+ route "index.html" idRoute
37+ create "index.html" $
38+ constA mempty
39+ >>> arr (setField "title" "Home")
40+ >>> requireAllA "posts/*" (id *** arr (take 3 . reverse . sortByBaseName) >>> addPostList)
41+ >>> applyTemplateCompiler "templates/index.html"
42+ >>> applyTemplateCompiler "templates/default.html"
43+ >>> relativizeUrlsCompiler
44+
45+ -- Read templates
46+ compile "templates/*" templateCompiler
47+
48+ -- End
49+ return ()
50+
51+-- | Auxiliary compiler: generate a post list from a list of given posts, and
52+-- add it to the current page under @$posts@
53+--
54+addPostList :: Compiler (Page String, [Page String]) (Page String)
55+addPostList = setFieldA "posts" $
56+ arr (reverse . sortByBaseName)
57+ >>> require "templates/postitem.html" (\p t -> map (applyTemplate t) p)
58+ >>> arr mconcat
59+ >>> arr pageBody