on master help sign in
Updated documentation and examples.
d205f96Jasper Van der Jeugt
examples/hakyll/hakyll.hs+5−8
@@ -14,14 +14,11 @@ main = hakyll $ do
14 directory static "examples"
14 directory static "examples"
15 directory static "reference"
15 directory static "reference"
16
16
17− tutorials <- liftIO $ liftM (sort . filter (`matchesRegex` "tutorial[0-9]*.markdown")) $ getDirectoryContents "."
17+ tutorials <- liftIO $ liftM (sort . filter (`matchesRegex` "^tutorial[0-9]*.markdown$")) $ getDirectoryContents "."
18+ let tutorialPage = createListing "tutorials.html"
18− let tutorialList = renderAndConcat ["templates/tutorialitem.html"]
19+ "templates/tutorialitem.html"
19− (map createPagePath tutorials)
20+ (map createPagePath tutorials)
20− tutorialPage = createCustomPage "tutorials.html"
21− ("templates/tutorialitem.html" : tutorials)
22− [ ("title", Left "Tutorials")
21+ [("title", "Tutorials")]
23− , ("tutorials", Right tutorialList)
24− ]
25 renderChain ["templates/tutorials.html", "templates/default.html"] $ withSidebar tutorialPage
22 renderChain ["templates/tutorials.html", "templates/default.html"] $ withSidebar tutorialPage
26
23
27 mapM_ render' $ [ "about.markdown"
24 mapM_ render' $ [ "about.markdown"
examples/hakyll/templates/tutorials.html+1−1
@@ -3,5 +3,5 @@
3 Here is a list of tutorials I've written about hakyll:
3 Here is a list of tutorials I've written about hakyll:
4</p>
4</p>
5<ul>
5<ul>
6− $tutorials
6+ $body
7</ul>
7</ul>
examples/hakyll/tutorial3.markdown+37−29
@@ -72,7 +72,7 @@ we want to show a few recent posts on the index page.
72
72
73## Custom Pages
73## Custom Pages
74
74
75−Currently, there are 3 renderable datatypes in Hakyll:
75+Currently, there are 4 renderable datatypes in Hakyll:
76
76
77- `Page`: The result of any rendering action. It is generally not recommended
77- `Page`: The result of any rendering action. It is generally not recommended
78 to use pages a lot, because they cannot check dependencies (and therefore,
78 to use pages a lot, because they cannot check dependencies (and therefore,
@@ -82,6 +82,8 @@ Currently, there are 3 renderable datatypes in Hakyll:
82 and works on a higher level.
82 and works on a higher level.
83- `CustomPage`: Basically the name says it - the preferred way of creating
83- `CustomPage`: Basically the name says it - the preferred way of creating
84 custom pages in Hakyll.
84 custom pages in Hakyll.
85+- `CombinedRenderable`: A way of combining two other `Renderable`s - this is
86+ explained in tutorial 2.
85
87
86We will use a `CustomPage` here. Basically, all `Renderable` datatypes are in
88We will use a `CustomPage` here. Basically, all `Renderable` datatypes are in
87the end just `key: value` mappings. A CustomPage is created using the
89the end just `key: value` mappings. A CustomPage is created using the
@@ -107,8 +109,23 @@ which type to use for the value:
107 result in a String. However - this action _will not be executed_ when the file
109 result in a String. However - this action _will not be executed_ when the file
108 in `_site` is up-to-date.
110 in `_site` is up-to-date.
109
111
110−First, let us define this `Hakyll String` for our index page. We want to render
112+However, in this specific case - a list of posts - there is an easier, and more
113+high-level approach[^1]. Let's look at the signature of `createListing`:
114+
115+~~~~~{.haskell}
116+createListing :: (Renderable a)
117+ -> String
118+ -> FilePath
119+ -> [a]
120+ -> [(String, String)]
121+~~~~~
122+
123+[^1]: Since Hakyll-1.3 onwards.
124+
125+The first argument is the destination url. For our blog, this is of course
126+`index.html`. The second argument is a template to render _each renderable_
127+with. We use `templates/postitem.html` here. This is, as you can see, a simple
111−every post using a simple template:
128+template:
112
129
113~~~~~{.html}
130~~~~~{.html}
114<li>
131<li>
@@ -117,41 +134,32 @@ every post using a simple template:
117</li>
134</li>
118~~~~~
135~~~~~
119
136
120−When every post is rendered with this template, we then want to concatenate the
137+We then give a list of renderables. For our index, these are the 3 last posts.
121−result. Since rendering and concatenating is pretty common, Hakyll provides us
138+The last argument of the `createListing` functions lets you specify additional
122−with a high-level function to do this.
139+key-value pairs. We use this to set the title of our page. So, we create our
140+index page using:
123
141
124~~~~~{.haskell}
142~~~~~{.haskell}
143+let index = createListing "index.html"
125−let recentPosts = renderAndConcat ["templates/postitem.html"]
144+ "templates/postitem.html"
126− (take 3 renderablePosts)
145+ (take 3 renderablePosts)
146+ [("title", "Home")]
127~~~~~
147~~~~~
128
148
129−Now, creating our custom page is fairly straight-forward:
149+The result of this will be a `CustomPage`. The body of this page will contain
130−
150+a concatenation of all the renderables, rendered with the
131−~~~~~{.haskell}
132−createCustomPage "index.html"
133− ("templates/postitem.html" : take 3 postPaths)
151+`templates/postitem.html` template.
134− [ ("title", Left "All posts")
135− , ("posts", Right recentPosts)
136− ]
137−~~~~~
138
152
139−You can see our three arguments here. We're rendering `index.html`, then we tell
153+Now, we only have to render it: first using the `index.html` template, then
140−Hakyll on what files it depends - here the `templates/postitem.html` template
154+using the `templates/default.html` template.
141−and the latest 3 posts. Finally, we give a `title` value to substitute in the
142−template, and the result of our concatenation. Of course, we also need to render
143−this custom page:
144
155
145~~~~~{.haskell}
156~~~~~{.haskell}
146−renderChain ["index.html", "templates/default.html"] $
157+renderChain ["index.html", "templates/default.html"] index
147− createCustomPage "index.html"
148− ("templates/postitem.html" : take 3 postPaths)
149− [ ("title", Left "All posts")
150− , ("posts", Right recentPosts)
151− ]
152~~~~~
158~~~~~
153
159
154−Note that the `index.html` in the `renderChain` list is also a template.
160+Note that the `index.html` in the `renderChain` list is also a template. Now,
161+take your time to read the `index.html` template and the other files in the zip
162+so you understand what is going on here.
155
163
156## That's that
164## That's that
157
165
examples/simpleblog/hakyll.hs+3−7
@@ -4,7 +4,7 @@ import Text.Hakyll (hakyll)
4import Text.Hakyll.Render
4import Text.Hakyll.Render
5import Text.Hakyll.Context
5import Text.Hakyll.Context
6import Text.Hakyll.File (getRecursiveContents, directory)
6import Text.Hakyll.File (getRecursiveContents, directory)
7−import Text.Hakyll.Renderables (createPagePath, createCustomPage)
7+import Text.Hakyll.Renderables (createPagePath, createCustomPage, createListing)
8import Data.List (sort)
8import Data.List (sort)
9import Control.Monad (mapM_, liftM)
9import Control.Monad (mapM_, liftM)
10import Control.Monad.Reader (liftIO)
10import Control.Monad.Reader (liftIO)
@@ -19,16 +19,12 @@ main = hakyll $ do
19 let renderablePosts = map createPagePath postPaths
19 let renderablePosts = map createPagePath postPaths
20
20
21 -- Render index, including recent posts.
21 -- Render index, including recent posts.
22− let recentPosts = renderAndConcat ["templates/postitem.html"] $ take 3 renderablePosts
23 renderChain ["index.html", "templates/default.html"] $
22 renderChain ["index.html", "templates/default.html"] $
24− createCustomPage "index.html" ("templates/postitem.html" : take 3 postPaths)
23+ createListing "index.html" "templates/postitem.html" (take 3 renderablePosts) [("title", "Home")]
25− [("title", Left "Home"), ("posts", Right recentPosts)]
26
24
27 -- Render all posts list.
25 -- Render all posts list.
28− let postItems = renderAndConcat ["templates/postitem.html"] $ renderablePosts
29 renderChain ["posts.html", "templates/default.html"] $
26 renderChain ["posts.html", "templates/default.html"] $
30− createCustomPage "posts.html" ("templates/postitem.html" : postPaths)
27+ createListing "posts.html" "templates/postitem.html" renderablePosts [("title", "All posts")]
31− [("title", Left "All posts"), ("posts", Right postItems)]
32
28
33 -- Render all posts.
29 -- Render all posts.
34 liftIO $ putStrLn "Generating posts..."
30 liftIO $ putStrLn "Generating posts..."
examples/simpleblog/index.html+1−1
@@ -1,7 +1,7 @@
1<div id="posts">
1<div id="posts">
2 <h1>Recent posts</h1>
2 <h1>Recent posts</h1>
3 <ul>
3 <ul>
4− $posts
4+ $body
5 </ul>
5 </ul>
6 <a href="$root/posts.html">All posts...</a>
6 <a href="$root/posts.html">All posts...</a>
7</div>
7</div>
examples/simpleblog/posts.html+1−1
@@ -1,4 +1,4 @@
1<h1>All posts</h1>
1<h1>All posts</h1>
2<ul>
2<ul>
3− $posts
3+ $body
4</ul>
4</ul>