on master help sign in
Updated categoryblog example to Hakyll-2.x.
commit82931dd6661221f1d138f2de5af0668e9080ef26 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateTue 30 Mar 2010, 17:36 (17 years ago) parent6fd3734
4 files changed+37−40
examples/categoryblog/hakyll.hs+36−25 examples/categoryblog/templates/post.html+1−1 examples/categoryblog/templates/rss.xml−9 examples/categoryblog/templates/rssitem.xml−5
examples/categoryblog/hakyll.hs+36−25
@@ -1,64 +1,75 @@
11module Main where
22
3+import Control.Arrow ((>>>), arr)
34import Text.Hakyll (hakyll)
45import Text.Hakyll.Render
5−import Text.Hakyll.Tags (TagMap, readCategoryMap)
6+import Text.Hakyll.Tags (readCategoryMap, withTagMap)
7+import Text.Hakyll.Feed (FeedConfiguration (..), renderRss)
68import Text.Hakyll.File (getRecursiveContents, directory, removeSpaces, sortByBaseName)
7−import Text.Hakyll.Renderables (createPagePath, createCustomPage, createListingWith, createListing)
8−import Text.Hakyll.Context (ContextManipulation, renderDate)
9+import Text.Hakyll.CreateContext (createPage, createCustomPage, createListing)
10+import Text.Hakyll.ContextManipulations (renderDate, copyValue, changeValue)
911import Text.Hakyll.Util (link)
1012import Data.Map (toList)
1113import Control.Monad (mapM_, liftM, (<=<))
1214import Data.Either (Either(..))
1315
14−main = hakyll $ do
16+main = hakyll "http://example.com" $ do
1517 -- Static directory.
1618 directory css "css"
1719
1820 -- Find all post paths.
1921 postPaths <- liftM (reverse . sortByBaseName) $ getRecursiveContents "posts"
20− let renderablePosts = map createPagePath postPaths
22+ let renderablePosts = map ((>>> postManipulation) . createPage) postPaths
2123
2224 -- Read category map.
23− categoryMap <- readCategoryMap "categoryMap" renderablePosts
25+ let categoryMap = readCategoryMap "categoryMap" postPaths
2426
2527 -- Render all posts list.
2628 renderPostList "posts.html" "All posts" renderablePosts
2729
2830 -- Render post list per category
29− mapM_ (\(category, posts) -> renderPostList (categoryToUrl category) ("Posts about " ++ category) posts)
30− (toList categoryMap)
31+ let renderListForCategory category posts =
32+ renderPostList (categoryToUrl category) ("Posts about " ++ category)
33+ (map (>>> postManipulation) posts)
34+ withTagMap categoryMap renderListForCategory
3135
3236 -- Render index, including recent posts.
33− let index = createListingWith postManipulation "index.html"
34− "templates/postitem.html"
35− (take 3 renderablePosts)
36− [ ("title", "Home")
37− , ("categories", categoryList categoryMap)
38− ]
37+ let index = createListing "index.html"
38+ ["templates/postitem.html"]
39+ (take 3 renderablePosts)
40+ [ ("title", Left "Home")
41+ , ("categories", Right $ categoryMap >>> categoryList)
42+ ]
3943 renderChain ["index.html", "templates/default.html"] index
4044
4145 -- Render all posts.
42− mapM_ (renderChainWith postManipulation
43− ["templates/post.html"
44− ,"templates/default.html"
45− ]) renderablePosts
46+ mapM_ (renderChain ["templates/post.html"
47+ ,"templates/default.html"
48+ ]) renderablePosts
4649
4750 -- Render rss feed
48− let rss = createListing "rss.xml" "templates/rssitem.xml" (take 3 renderablePosts) []
49− renderChain ["templates/rss.xml"] rss
51+ renderRss myFeedConfiguration $
52+ map (>>> copyValue "body" "description") (take 3 renderablePosts)
5053
51− where postManipulation :: ContextManipulation
52− postManipulation = renderDate "date" "%B %e, %Y" "Date unknown"
54+ where postManipulation = renderDate "date" "%B %e, %Y" "Date unknown"
55+ >>> renderCategoryLink
56+
57+ renderCategoryLink = changeValue "category" (\c -> link c $ categoryToUrl c)
5358
5459 categoryToUrl category = "$root/categories/" ++ removeSpaces category ++ ".html"
5560
56− categoryList :: TagMap -> String
57− categoryList = uncurry categoryListItem <=< toList
61+ categoryList = arr $ uncurry categoryListItem <=< toList
5862
5963 categoryListItem category posts = "<li>" ++ link category (categoryToUrl category)
6064 ++ " - " ++ show (length posts) ++ " items.</li>"
6165
6266 renderPostList url title posts = do
63− let list = createListingWith postManipulation url "templates/postitem.html" posts [("title", title)]
67+ let list = createListing url ["templates/postitem.html"] posts [("title", Left title)]
6468 renderChain ["posts.html", "templates/default.html"] list
69+
70+myFeedConfiguration = FeedConfiguration
71+ { feedUrl = "rss.xml"
72+ , feedTitle = "SimpleBlog RSS feed."
73+ , feedDescription = "A simple demo of an RSS feed created with Hakyll."
74+ , feedAuthorName = "Jasper Van der Jeugt"
75+ }
examples/categoryblog/templates/post.html+1−1
@@ -1,5 +1,5 @@
11<h1>$title</h1>
22by <em>$author</em> on <strong>$date</strong>
3−<div>Tagged as: $tags.</div>
3+<div>Posted in: $category.</div>
44
55$body
examples/categoryblog/templates/rss.xmldeleted−9
@@ -1,9 +0,0 @@
1−<?xml version="1.0" ?>
2−<rss version="2.0">
3− <channel>
4− <title>The SimpleBlog</title>
5− <link>http://example.com</link>
6− <description>Simple blog in hakyll</description>
7− $items
8− </channel>
9−</rss>
examples/categoryblog/templates/rssitem.xmldeleted−5
@@ -1,5 +0,0 @@
1−<item>
2− <title>$title</title>
3− <link>http://example.com/$url</link>
4− <description>$title by $author</description>
5−</item>