~jack/jackkelly-name

ref: 8de6f1521045b2fbc086cb30d9fc901d7170ab5a jackkelly-name/src/Blog.hs -rw-r--r-- 2.3 KiB
8de6f152Jack Kelly Import site code from unified repo 2 years ago
                                                                                
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
72
73
74
75
76
77
78
79
80
81
module Blog (blog) where

import Blog.Archives (blogArchives)
import Blog.Images (blogImages)
import Blog.Posts (blogPostContext, blogPosts, blogPostsPattern)
import Blog.Util (baseBlogContext)
import Hakyll
import Site (sectionLabel)

blog :: Rules ()
blog = do
  tags <- buildTags blogPostsPattern (fromCapture "blog/archives/*/index.html")
  blogTags tags
  blogPosts tags
  blogArchives tags
  blogImages
  blogFront tags
  blogFeeds

blogTags :: Tags -> Rules ()
blogTags tags = tagsRules tags $ \tag pat -> do
  route idRoute
  compile $ do
    postContext <- blogPostContext tags

    let
      title = sectionLabel [ "Blog", "Archives for tag: " ++ tag ]
      posts = recentFirst =<< loadAll pat

      context = mconcat
        [ constField "title" title
        , listField "posts" postContext posts
        , baseBlogContext
        ]

    makeItem ""
      >>= loadAndApplyTemplate "templates/blog/tag_archive.html" context
      >>= loadAndApplyTemplate "templates/default.html" context
      >>= relativizeUrls

blogFront :: Tags -> Rules ()
blogFront tags = create ["blog/index.html"] $ do
  route idRoute
  compile $ do
    postContext <- blogPostContext tags

    let context = mconcat
          [ listField "posts" postContext (loadRecentPosts 5)
          , constField "title" (sectionLabel ["Blog"])
          , baseBlogContext
          ]

    makeItem ""
      >>= loadAndApplyTemplate "templates/blog/index.html" context
      >>= loadAndApplyTemplate "templates/default.html" context
      >>= relativizeUrls

blogFeeds :: Rules ()
blogFeeds = do
  create ["blog/atom.xml"] $ do
    route idRoute
    compile $ loadRecentPosts 10 >>= renderAtom config feedContext

  create ["blog/rss.xml"] $ do
    route idRoute
    compile $ loadRecentPosts 10 >>= renderRss config feedContext

  where
    config = FeedConfiguration
      { feedTitle = sectionLabel ["Blog"]
      , feedDescription = "Latest posts on jackkelly.name"
      , feedAuthorName = "Jack Kelly"
      , feedAuthorEmail = "jack at jackkelly dot name"
      , feedRoot = "https://jackkelly.name/blog"
      }

    feedContext = bodyField "description" <> defaultContext

loadRecentPosts :: Int -> Compiler [Item String]
loadRecentPosts n = fmap (take n) . recentFirst
  =<< loadAllSnapshots blogPostsPattern "content"