@@ -1,49 +1,72 @@
1+{-# LANGUAGE OverloadedStrings #-}
12module Main where
23
3−import Control.Arrow ((>>>))
4−import Text.Hakyll (hakyll)
5−import Text.Hakyll.Render
6−import Text.Hakyll.Context
7−import Text.Hakyll.File (getRecursiveContents, directory)
8−import Text.Hakyll.CreateContext (createPage, createCustomPage, createListing)
9−import Text.Hakyll.ContextManipulations (copyValue)
10−import Text.Hakyll.Feed (FeedConfiguration (..), renderRss)
11−import Data.List (sort)
12−import Control.Monad (forM_, liftM)
13−import Control.Monad.Reader (liftIO)
14−import Data.Either (Either(..))
15−
16−main = hakyll "http://example.com" $ do
17− -- Static directory.
18− directory css "css"
19−
20− -- Find all post paths.
21− postPaths <- liftM (reverse . sort) $ getRecursiveContents "posts"
22− let postPages = map createPage postPaths
23−
24− -- Render index, including recent posts.
25− let index = createListing "index.html" ["templates/postitem.html"]
26− (take 3 postPages) [("title", Left "Home")]
27− renderChain ["index.html", "templates/default.html"] index
28−
29− -- Render all posts list.
30− let posts = createListing "posts.html" ["templates/postitem.html"]
31− postPages [("title", Left "All posts")]
32− renderChain ["posts.html", "templates/default.html"] posts
33−
34− -- Render all posts.
35− liftIO $ putStrLn "Generating posts..."
36− forM_ postPages $ renderChain [ "templates/post.html"
37− , "templates/default.html"
38− ]
39−
40− -- Render RSS feed.
41− renderRss myFeedConfiguration $
42− map (>>> copyValue "body" "description") (take 3 postPages)
43−
44−myFeedConfiguration = FeedConfiguration
45− { feedUrl = "rss.xml"
46− , feedTitle = "SimpleBlog RSS feed."
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+ -- Render RSS feed
46+ route "rss.xml" $ idRoute
47+ create "rss.xml" $
48+ requireAll_ "posts/*" >>> renderRss feedConfiguration
49+
50+ -- Read templates
51+ compile "templates/*" templateCompiler
52+
53+ -- End
54+ return ()
55+
56+-- | Auxiliary compiler: generate a post list from a list of given posts, and
57+-- add it to the current page under @$posts@
58+--
59+addPostList :: Compiler (Page String, [Page String]) (Page String)
60+addPostList = setFieldA "posts" $
61+ arr (reverse . sortByBaseName)
62+ >>> require "templates/postitem.html" (\p t -> map (applyTemplate t) p)
63+ >>> arr mconcat
64+ >>> arr pageBody
65+
66+feedConfiguration :: FeedConfiguration
67+feedConfiguration = FeedConfiguration
68+ { feedTitle = "SimpleBlog RSS feed."
4769 , feedDescription = "A simple demo of an RSS feed created with Hakyll."
4870 , feedAuthorName = "Jasper Van der Jeugt"
71+ , feedRoot = "http://example.com"
4972 }