on master help sign in
Update simpleblog example to work with Hakyll 3.
8723429Benedict Eastaugh
examples/simpleblog/hacknew file

Binary file not shown.

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