on master help sign in
Update simpleblog example to work with Hakyll 3.
commit87234295cf67aa00e7b8128896a05212f2fa9cdc authorBenedict Eastaugh <benedict@eastaugh.net> dateSat 26 Mar 2011, 19:24 (16 years ago) parent379cd0e
7 files changed+71−47
examples/simpleblog/hack examples/simpleblog/hakyll.hs+57−33 examples/simpleblog/templates/default.html+6−6 examples/simpleblog/index.html → examples/simpleblog/templates/index.html+2−2 examples/simpleblog/templates/post.html+3−3 examples/simpleblog/templates/postitem.html+2−2 examples/simpleblog/posts.html → examples/simpleblog/templates/posts.html+1−1
examples/simpleblog/hacknew file

Binary file not shown.

examples/simpleblog/hakyll.hs+57−33
@@ -1,35 +1,59 @@
1+{-# LANGUAGE OverloadedStrings #-}
12module Main where
23
3−import Text.Hakyll (hakyll)
4−import Text.Hakyll.Render
5−import Text.Hakyll.Context
6−import Text.Hakyll.File (getRecursiveContents, directory)
7−import Text.Hakyll.CreateContext (createPage, createCustomPage, createListing)
8−import Data.List (sort)
9−import Control.Monad (forM_, liftM)
10−import Control.Monad.Reader (liftIO)
11−import Data.Either (Either(..))
12−
13−main = hakyll "http://example.com" $ do
14− -- Static directory.
15− directory css "css"
16−
17− -- Find all post paths.
18− postPaths <- liftM (reverse . sort) $ getRecursiveContents "posts"
19− let postPages = map createPage postPaths
20−
21− -- Render index, including recent posts.
22− let index = createListing "index.html" ["templates/postitem.html"]
23− (take 3 postPages) [("title", Left "Home")]
24− renderChain ["index.html", "templates/default.html"] index
25−
26− -- Render all posts list.
27− let posts = createListing "posts.html" ["templates/postitem.html"]
28− postPages [("title", Left "All posts")]
29− renderChain ["posts.html", "templates/default.html"] posts
30−
31− -- Render all posts.
32− liftIO $ putStrLn "Generating posts..."
33− forM_ postPages $ renderChain [ "templates/post.html"
34− , "templates/default.html"
35− ]
4+import Prelude hiding (id)
5+import Control.Category (id)
6+import Control.Arrow ((>>>), (***), arr)
7+import Data.Monoid (mempty, mconcat)
8+
9+import Hakyll
10+
11+main :: IO ()
12+main = hakyll $ do
13+ -- Compress CSS
14+ route "css/*" idRoute
15+ compile "css/*" compressCssCompiler
16+
17+ -- Render posts
18+ route "posts/*" $ setExtension ".html"
19+ compile "posts/*" $
20+ pageCompiler
21+ >>> applyTemplateCompiler "templates/post.html"
22+ >>> applyTemplateCompiler "templates/default.html"
23+ >>> relativizeUrlsCompiler
24+
25+ -- Render posts list
26+ route "posts.html" $ idRoute
27+ create "posts.html" $
28+ constA mempty
29+ >>> arr (setField "title" "All posts")
30+ >>> requireAllA "posts/*" addPostList
31+ >>> applyTemplateCompiler "templates/posts.html"
32+ >>> applyTemplateCompiler "templates/default.html"
33+ >>> relativizeUrlsCompiler
34+
35+ -- Index
36+ route "index.html" idRoute
37+ create "index.html" $
38+ constA mempty
39+ >>> arr (setField "title" "Home")
40+ >>> requireAllA "posts/*" (id *** arr (take 3 . reverse . sortByBaseName) >>> addPostList)
41+ >>> applyTemplateCompiler "templates/index.html"
42+ >>> applyTemplateCompiler "templates/default.html"
43+ >>> relativizeUrlsCompiler
44+
45+ -- Read templates
46+ compile "templates/*" templateCompiler
47+
48+ -- End
49+ return ()
50+
51+-- | Auxiliary compiler: generate a post list from a list of given posts, and
52+-- add it to the current page under @$posts@
53+--
54+addPostList :: Compiler (Page String, [Page String]) (Page String)
55+addPostList = setFieldA "posts" $
56+ arr (reverse . sortByBaseName)
57+ >>> require "templates/postitem.html" (\p t -> map (applyTemplate t) p)
58+ >>> arr mconcat
59+ >>> arr pageBody
examples/simpleblog/templates/default.html+6−6
@@ -4,16 +4,16 @@
44<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
55 <head>
66 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
7− <title>SimpleBlog - $title</title>
8− <link rel="stylesheet" type="text/css" href="$root/css/default.css" />
7+ <title>SimpleBlog - $title$</title>
8+ <link rel="stylesheet" type="text/css" href="/css/default.css" />
99 </head>
1010 <body>
11− <h1>SimpleBlog - $title</h1>
11+ <h1>SimpleBlog - $title$</h1>
1212 <div id="navigation">
13− <a href="$root/index.html">Home</a>
14− <a href="$root/posts.html">All posts</a>
13+ <a href="/index.html">Home</a>
14+ <a href="/posts.html">All posts</a>
1515 </div>
1616
17− $body
17+ $body$
1818 </body>
1919</html>
examples/simpleblog/index.html → examples/simpleblog/templates/index.htmlrenamed+2−2
@@ -1,9 +1,9 @@
11<div id="posts">
22 <h1>Recent posts</h1>
33 <ul>
4− $body
4+ $posts$
55 </ul>
6− <a href="$root/posts.html">All posts...</a>
6+ <a href="/posts.html">All posts...</a>
77</div>
88
99<div class="about">
examples/simpleblog/templates/post.html+3−3
@@ -1,4 +1,4 @@
1−<h1>$title</h1>
2−by <em>$author</em> on <strong>$date</strong>
1+<h1>$title$</h1>
2+by <em>$author$</em> on <strong>$date$</strong>
33
4−$body
4+$body$
examples/simpleblog/templates/postitem.html+2−2
@@ -1,4 +1,4 @@
11<li>
2− <a href="$root/$url">$title</a>
3− - <em>$date</em> - by <em>$author</em>
2+ <a href="/$url$">$title$</a>
3+ - <em>$date$</em> - by <em>$author$</em>
44</li>
examples/simpleblog/posts.html → examples/simpleblog/templates/posts.htmlrenamed+1−1
@@ -1,4 +1,4 @@
11<h1>All posts</h1>
22<ul>
3− $body
3+ $posts$
44</ul>