summaryrefslogtreecommitdiff
path: root/examples/tagblog/hakyll.hs
blob: a81c280c4ac926f45729540b165ad5801bfc3964 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
module Main where

import Control.Arrow ((>>>))
import Text.Hakyll (hakyll)
import Text.Hakyll.Render
import Text.Hakyll.Tags (readTagMap, renderTagCloud, renderTagLinks, withTagMap)
import Text.Hakyll.Feed (FeedConfiguration (..), renderRss)
import Text.Hakyll.File (getRecursiveContents, directory, removeSpaces)
import Text.Hakyll.CreateContext (createPage, createCustomPage, createListing)
import Text.Hakyll.ContextManipulations (renderDate, copyValue)
import Data.List (sort)
import Data.Map (toList)
import Control.Monad (forM_, liftM)
import Data.Either (Either(..))

main = hakyll "http://example.com" $ do
    -- Static directory.
    directory css "css"

    -- Find all post paths.
    postPaths <- liftM (reverse . sort) $ getRecursiveContents "posts"
    let renderablePosts = map ((>>> postManipulation) . createPage) postPaths

    -- Read tag map.
    let tagMap = readTagMap "postTags" postPaths

    -- Render all posts list.
    renderPostList "posts.html" "All posts" renderablePosts

    -- Render post list per tag
    let renderListForTag tag posts =
            renderPostList (tagToUrl tag) ("Posts tagged " ++ tag)
                           (map (>>> postManipulation) posts)
    withTagMap tagMap renderListForTag

    -- Render index, including recent posts.
    let tagCloud = tagMap >>> renderTagCloud tagToUrl 100 200
        index = createListing "index.html"
                              ["templates/postitem.html"]
                              (take 3 renderablePosts)
                              [ ("title", Left "Home")
                              , ("tagcloud", Right tagCloud)
                              ]
    renderChain ["index.html", "templates/default.html"] index

    -- Render all posts.
    forM_ renderablePosts $ renderChain [ "templates/post.html"
                                        , "templates/default.html"
                                        ]

    -- Render rss feed
    renderRss myFeedConfiguration $
        map (>>> copyValue "body" "description") (take 3 renderablePosts)

  where
    postManipulation =   renderDate "date" "%B %e, %Y" "Date unknown"
                     >>> renderTagLinks tagToUrl 

    tagToUrl tag = "$root/tags/" ++ removeSpaces tag ++ ".html"

    renderPostList url title posts = do
        let list = createListing url ["templates/postitem.html"]
                                 posts [("title", Left title)]
        renderChain ["posts.html", "templates/default.html"] list

myFeedConfiguration = FeedConfiguration
    { feedUrl         = "rss.xml"
    , feedTitle       = "SimpleBlog RSS feed."
    , feedDescription = "A simple demo of an RSS feed created with Hakyll."
    , feedAuthorName  = "Jasper Van der Jeugt"
    }