From 87234295cf67aa00e7b8128896a05212f2fa9cdc Mon Sep 17 00:00:00 2001 From: Benedict Eastaugh Date: Sat, 26 Mar 2011 19:24:03 +0000 Subject: Update simpleblog example to work with Hakyll 3. --- examples/simpleblog/hack | Bin 0 -> 39309764 bytes examples/simpleblog/hakyll.hs | 90 ++++++++++++++++++---------- examples/simpleblog/index.html | 14 ----- examples/simpleblog/posts.html | 4 -- examples/simpleblog/templates/default.html | 12 ++-- examples/simpleblog/templates/index.html | 14 +++++ examples/simpleblog/templates/post.html | 6 +- examples/simpleblog/templates/postitem.html | 4 +- examples/simpleblog/templates/posts.html | 4 ++ 9 files changed, 86 insertions(+), 62 deletions(-) create mode 100755 examples/simpleblog/hack delete mode 100644 examples/simpleblog/index.html delete mode 100644 examples/simpleblog/posts.html create mode 100644 examples/simpleblog/templates/index.html create mode 100644 examples/simpleblog/templates/posts.html (limited to 'examples') diff --git a/examples/simpleblog/hack b/examples/simpleblog/hack new file mode 100755 index 0000000..42e295e Binary files /dev/null and b/examples/simpleblog/hack differ diff --git a/examples/simpleblog/hakyll.hs b/examples/simpleblog/hakyll.hs index 38472d5..db4230f 100644 --- a/examples/simpleblog/hakyll.hs +++ b/examples/simpleblog/hakyll.hs @@ -1,35 +1,59 @@ +{-# LANGUAGE OverloadedStrings #-} module Main where -import Text.Hakyll (hakyll) -import Text.Hakyll.Render -import Text.Hakyll.Context -import Text.Hakyll.File (getRecursiveContents, directory) -import Text.Hakyll.CreateContext (createPage, createCustomPage, createListing) -import Data.List (sort) -import Control.Monad (forM_, liftM) -import Control.Monad.Reader (liftIO) -import Data.Either (Either(..)) - -main = hakyll "http://example.com" $ do - -- Static directory. - directory css "css" - - -- Find all post paths. - postPaths <- liftM (reverse . sort) $ getRecursiveContents "posts" - let postPages = map createPage postPaths - - -- Render index, including recent posts. - let index = createListing "index.html" ["templates/postitem.html"] - (take 3 postPages) [("title", Left "Home")] - renderChain ["index.html", "templates/default.html"] index - - -- Render all posts list. - let posts = createListing "posts.html" ["templates/postitem.html"] - postPages [("title", Left "All posts")] - renderChain ["posts.html", "templates/default.html"] posts - - -- Render all posts. - liftIO $ putStrLn "Generating posts..." - forM_ postPages $ renderChain [ "templates/post.html" - , "templates/default.html" - ] +import Prelude hiding (id) +import Control.Category (id) +import Control.Arrow ((>>>), (***), arr) +import Data.Monoid (mempty, mconcat) + +import Hakyll + +main :: IO () +main = hakyll $ do + -- Compress CSS + route "css/*" idRoute + compile "css/*" compressCssCompiler + + -- Render posts + route "posts/*" $ setExtension ".html" + compile "posts/*" $ + pageCompiler + >>> applyTemplateCompiler "templates/post.html" + >>> applyTemplateCompiler "templates/default.html" + >>> relativizeUrlsCompiler + + -- Render posts list + route "posts.html" $ idRoute + create "posts.html" $ + constA mempty + >>> arr (setField "title" "All posts") + >>> requireAllA "posts/*" addPostList + >>> applyTemplateCompiler "templates/posts.html" + >>> applyTemplateCompiler "templates/default.html" + >>> relativizeUrlsCompiler + + -- Index + route "index.html" idRoute + create "index.html" $ + constA mempty + >>> arr (setField "title" "Home") + >>> requireAllA "posts/*" (id *** arr (take 3 . reverse . sortByBaseName) >>> addPostList) + >>> applyTemplateCompiler "templates/index.html" + >>> applyTemplateCompiler "templates/default.html" + >>> relativizeUrlsCompiler + + -- Read templates + compile "templates/*" templateCompiler + + -- End + return () + +-- | Auxiliary compiler: generate a post list from a list of given posts, and +-- add it to the current page under @$posts@ +-- +addPostList :: Compiler (Page String, [Page String]) (Page String) +addPostList = setFieldA "posts" $ + arr (reverse . sortByBaseName) + >>> require "templates/postitem.html" (\p t -> map (applyTemplate t) p) + >>> arr mconcat + >>> arr pageBody diff --git a/examples/simpleblog/index.html b/examples/simpleblog/index.html deleted file mode 100644 index 88cc0a2..0000000 --- a/examples/simpleblog/index.html +++ /dev/null @@ -1,14 +0,0 @@ -
-

Recent posts

- - All posts... -
- -
-

About

-

- This is a sample blog for educational purposes. -

-
diff --git a/examples/simpleblog/posts.html b/examples/simpleblog/posts.html deleted file mode 100644 index 7db1a59..0000000 --- a/examples/simpleblog/posts.html +++ /dev/null @@ -1,4 +0,0 @@ -

All posts

- diff --git a/examples/simpleblog/templates/default.html b/examples/simpleblog/templates/default.html index c411976..01ed20e 100644 --- a/examples/simpleblog/templates/default.html +++ b/examples/simpleblog/templates/default.html @@ -4,16 +4,16 @@ - SimpleBlog - $title - + SimpleBlog - $title$ + -

SimpleBlog - $title

+

SimpleBlog - $title$

- $body + $body$ diff --git a/examples/simpleblog/templates/index.html b/examples/simpleblog/templates/index.html new file mode 100644 index 0000000..4978949 --- /dev/null +++ b/examples/simpleblog/templates/index.html @@ -0,0 +1,14 @@ +
+

Recent posts

+ + All posts... +
+ +
+

About

+

+ This is a sample blog for educational purposes. +

+
diff --git a/examples/simpleblog/templates/post.html b/examples/simpleblog/templates/post.html index 46797a4..e2864ba 100644 --- a/examples/simpleblog/templates/post.html +++ b/examples/simpleblog/templates/post.html @@ -1,4 +1,4 @@ -

$title

-by $author on $date +

$title$

+by $author$ on $date$ -$body +$body$ diff --git a/examples/simpleblog/templates/postitem.html b/examples/simpleblog/templates/postitem.html index 0e62418..19c954b 100644 --- a/examples/simpleblog/templates/postitem.html +++ b/examples/simpleblog/templates/postitem.html @@ -1,4 +1,4 @@
  • - $title - - $date - by $author + $title$ + - $date$ - by $author$
  • diff --git a/examples/simpleblog/templates/posts.html b/examples/simpleblog/templates/posts.html new file mode 100644 index 0000000..2bec161 --- /dev/null +++ b/examples/simpleblog/templates/posts.html @@ -0,0 +1,4 @@ +

    All posts

    + -- cgit v1.2.3