diff options
author | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2010-03-01 18:50:02 +0100 |
---|---|---|
committer | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2010-03-01 18:50:02 +0100 |
commit | e4f09b0a051092ff12726d491a464c4b9c67ddf5 (patch) | |
tree | 147442037744dfa02f68aaec1a48bb8d6faca02e /src/Text/Hakyll | |
parent | eaf0c230fb776a9884bd999be7c3b9d6f79fa239 (diff) | |
download | hakyll-e4f09b0a051092ff12726d491a464c4b9c67ddf5.tar.gz |
Clean up pagination, disable first/last link when on first/last page.
Diffstat (limited to 'src/Text/Hakyll')
-rw-r--r-- | src/Text/Hakyll/Paginate.hs | 28 |
1 files changed, 17 insertions, 11 deletions
diff --git a/src/Text/Hakyll/Paginate.hs b/src/Text/Hakyll/Paginate.hs index 0f35854..f8b3373 100644 --- a/src/Text/Hakyll/Paginate.hs +++ b/src/Text/Hakyll/Paginate.hs @@ -43,32 +43,38 @@ defaultPaginateConfiguration = PaginateConfiguration -- - @$index@: 1-based index of the current page. -- - @$length@: Total number of pages. -- +-- When @$previous@ or @$next@ are not available, they will be just a label +-- without a link. The same goes for when we are on the first or last page for +-- @$first@ and @$last@. paginate :: (Renderable a) => PaginateConfiguration -> [a] -> [CombinedRenderable a CustomPage] paginate configuration renderables = paginate' Nothing renderables (1 :: Int) where + -- Create a link with a given label, taken from the configuration. linkWithLabel f r = link (f configuration) `fmap` getUrl r - first = linkWithLabel firstLabel (head renderables) - last' = linkWithLabel lastLabel (last renderables) - length' = length renderables - + -- The main function that creates combined renderables by recursing over + -- the list of renderables. paginate' _ [] _ = [] paginate' maybePrev (x:xs) index = - let previous = case maybePrev of - (Just r) -> linkWithLabel previousLabel r - Nothing -> return $ previousLabel configuration - next = case xs of - (n:_) -> linkWithLabel nextLabel n - [] -> return $ nextLabel configuration + let (previous, first) = case maybePrev of + (Just r) -> ( linkWithLabel previousLabel r + , linkWithLabel firstLabel (head renderables) ) + Nothing -> ( return $ previousLabel configuration + , return $ firstLabel configuration ) + (next, last') = case xs of + (n:_) -> ( linkWithLabel nextLabel n + , linkWithLabel lastLabel (last renderables) ) + [] -> ( return $ nextLabel configuration + , return $ lastLabel configuration ) customPage = createCustomPage "" [] [ ("previous", Right previous) , ("next", Right next) , ("first", Right first) , ("last", Right last') , ("index", Left $ show index) - , ("length", Left $ show length') + , ("length", Left $ show $ length $ renderables) ] in (x `combine` customPage) : paginate' (Just x) xs (index + 1) |