hakyll/examples/categoryblog/hakyll.hs

76 lines
3 KiB
Haskell
Raw Normal View History

2010-01-29 14:16:08 +00:00
module Main where
import Control.Arrow ((>>>), arr)
2010-01-29 14:16:08 +00:00
import Text.Hakyll (hakyll)
import Text.Hakyll.Render
import Text.Hakyll.Tags (readCategoryMap, withTagMap)
import Text.Hakyll.Feed (FeedConfiguration (..), renderRss)
2010-01-29 17:36:54 +00:00
import Text.Hakyll.File (getRecursiveContents, directory, removeSpaces, sortByBaseName)
import Text.Hakyll.CreateContext (createPage, createCustomPage, createListing)
import Text.Hakyll.ContextManipulations (renderDate, copyValue, changeValue)
2010-01-29 14:16:08 +00:00
import Text.Hakyll.Util (link)
import Data.Map (toList)
import Control.Monad (mapM_, liftM, (<=<))
import Data.Either (Either(..))
main = hakyll "http://example.com" $ do
2010-01-29 14:16:08 +00:00
-- Static directory.
directory css "css"
-- Find all post paths.
2010-01-29 17:36:54 +00:00
postPaths <- liftM (reverse . sortByBaseName) $ getRecursiveContents "posts"
let renderablePosts = map ((>>> postManipulation) . createPage) postPaths
2010-01-29 14:16:08 +00:00
-- Read category map.
let categoryMap = readCategoryMap "categoryMap" postPaths
2010-01-29 14:16:08 +00:00
-- Render all posts list.
renderPostList "posts.html" "All posts" renderablePosts
-- Render post list per category
let renderListForCategory category posts =
renderPostList (categoryToUrl category) ("Posts about " ++ category)
(map (>>> postManipulation) posts)
withTagMap categoryMap renderListForCategory
2010-01-29 14:16:08 +00:00
-- Render index, including recent posts.
let index = createListing "index.html"
["templates/postitem.html"]
(take 3 renderablePosts)
[ ("title", Left "Home")
, ("categories", Right $ categoryMap >>> categoryList)
]
2010-01-29 14:16:08 +00:00
renderChain ["index.html", "templates/default.html"] index
-- Render all posts.
mapM_ (renderChain ["templates/post.html"
,"templates/default.html"
]) renderablePosts
2010-01-29 14:16:08 +00:00
-- Render rss feed
renderRss myFeedConfiguration $
map (>>> copyValue "body" "description") (take 3 renderablePosts)
2010-01-29 14:16:08 +00:00
where postManipulation = renderDate "date" "%B %e, %Y" "Date unknown"
>>> renderCategoryLink
renderCategoryLink = changeValue "category" (\c -> link c $ categoryToUrl c)
2010-01-29 14:16:08 +00:00
2010-01-31 10:19:57 +00:00
categoryToUrl category = "$root/categories/" ++ removeSpaces category ++ ".html"
2010-01-29 14:16:08 +00:00
categoryList = arr $ uncurry categoryListItem <=< toList
2010-01-29 14:16:08 +00:00
2010-01-31 10:19:57 +00:00
categoryListItem category posts = "<li>" ++ link category (categoryToUrl category)
2010-01-29 14:16:08 +00:00
++ " - " ++ show (length posts) ++ " items.</li>"
renderPostList url title posts = do
let list = createListing url ["templates/postitem.html"] posts [("title", Left title)]
2010-01-29 14:16:08 +00:00
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"
}