summaryrefslogtreecommitdiff
path: root/examples/tagblog/hakyll.hs
blob: df5e9d09641fa6d883f64268fa2f863feffb85b0 (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
module Main where

import Text.Hakyll (hakyll)
import Text.Hakyll.Render
import Text.Hakyll.Tags (readTagMap, renderTagCloud, renderTagLinks)
import Text.Hakyll.File (getRecursiveContents, directory, removeSpaces)
import Text.Hakyll.Renderables (createPagePath, createCustomPage)
import Text.Hakyll.Context (ContextManipulation, renderDate)
import Data.List (sort)
import Data.Map (toList)
import Control.Monad (mapM_, liftM)
import Data.Either (Either(..))

main = hakyll $ do
    -- Static directory.
    directory css "css"

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

    -- Read tag map.
    tagMap <- readTagMap postPaths

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

    -- Render post list per tag
    mapM_ (\(tag, posts) -> renderPostList (tagToURL tag) ("Posts tagged " ++ tag) posts)
          (toList tagMap)

    -- Render index, including recent posts.
    let recentPosts = renderAndConcatWith postManipulation
                                          ["templates/postitem.html"]
                                          (take 3 renderablePosts)
    renderChain ["index.html", "templates/default.html"] $
        createCustomPage "index.html" ("templates/postitem.html" : take 3 postPaths)
            [("title", Left "Home"), ("posts", Right recentPosts),
             ("tagcloud", Left $ renderTagCloud tagMap tagToURL 100 200)]

    -- Render all posts.
    mapM_ (renderChainWith postManipulation
                           ["templates/post.html"
                           ,"templates/default.html"
                           ]) renderablePosts

    -- Render rss feed
    let recentRSSItems = renderAndConcat ["templates/rssitem.xml"] $ take 3 renderablePosts
    let rssPage = createCustomPage "rss.xml"
                        ("templates/postitem.html" : take 3 postPaths)
                        [("items", Right recentRSSItems)]
    renderChain ["templates/rss.xml"] rssPage

    -- Render index.
    renderChain ["templates/default.html"] $ createPagePath "index.html"

    where postManipulation :: ContextManipulation
          postManipulation = renderDate "date" "%B %e, %Y" "Date unknown"
                           . renderTagLinks tagToURL 

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

          renderPostList url title posts = do
              let postItems = renderAndConcatWith postManipulation
                                                  ["templates/postitem.html"]
                                                  (map createPagePath posts)
                  customPage = createCustomPage url
                                                ("templates/postitem.html" : posts)
                                                [("title", Left title), ("posts", Right postItems)]
              renderChain ["posts.html", "templates/default.html"] customPage