summaryrefslogtreecommitdiff
path: root/src/Text/Hakyll/Paginate.hs
diff options
context:
space:
mode:
authorJasper Van der Jeugt <jaspervdj@gmail.com>2010-03-01 18:17:12 +0100
committerJasper Van der Jeugt <jaspervdj@gmail.com>2010-03-01 18:17:12 +0100
commiteaf0c230fb776a9884bd999be7c3b9d6f79fa239 (patch)
tree3c691b07a8987a13db56e5ea500ea7ac8468e7d0 /src/Text/Hakyll/Paginate.hs
parent9576700a77eb97c409ef9628b4a04fc275945dd6 (diff)
downloadhakyll-eaf0c230fb776a9884bd999be7c3b9d6f79fa239.tar.gz
Added simple pagination (unstable).
Diffstat (limited to 'src/Text/Hakyll/Paginate.hs')
-rw-r--r--src/Text/Hakyll/Paginate.hs74
1 files changed, 74 insertions, 0 deletions
diff --git a/src/Text/Hakyll/Paginate.hs b/src/Text/Hakyll/Paginate.hs
new file mode 100644
index 0000000..0f35854
--- /dev/null
+++ b/src/Text/Hakyll/Paginate.hs
@@ -0,0 +1,74 @@
+-- | Module aimed to paginate web pages.
+module Text.Hakyll.Paginate
+ ( PaginateConfiguration (..)
+ , defaultPaginateConfiguration
+ , paginate
+ ) where
+
+import Text.Hakyll.Renderables
+import Text.Hakyll.Renderable (Renderable, getUrl)
+import Text.Hakyll.Util (link)
+
+-- | A configuration for a pagination.
+data PaginateConfiguration = PaginateConfiguration
+ { -- | Label for the link to the previous page.
+ previousLabel :: String
+ , -- | Label for the link to the next page.
+ nextLabel :: String
+ , -- | Label for the link to the first page.
+ firstLabel :: String
+ , -- | Label for the link to the last page.
+ lastLabel :: String
+ }
+
+-- | A simple default configuration for pagination.
+defaultPaginateConfiguration :: PaginateConfiguration
+defaultPaginateConfiguration = PaginateConfiguration
+ { previousLabel = "Previous"
+ , nextLabel = "Next"
+ , firstLabel = "First"
+ , lastLabel = "Last"
+ }
+
+-- | The most important function for pagination. This function operates on a
+-- list of renderables (the pages), and basically just adds fields to them
+-- by combining them with a custom page.
+--
+-- The following metadata fields will be added:
+--
+-- - @$previous@: A link to the previous page.
+-- - @$next@: A link to the next page.
+-- - @$first@: A link to the first page.
+-- - @$last@: A link to the last page.
+-- - @$index@: 1-based index of the current page.
+-- - @$length@: Total number of pages.
+--
+paginate :: (Renderable a)
+ => PaginateConfiguration
+ -> [a]
+ -> [CombinedRenderable a CustomPage]
+paginate configuration renderables = paginate' Nothing renderables (1 :: Int)
+ where
+ linkWithLabel f r = link (f configuration) `fmap` getUrl r
+
+ first = linkWithLabel firstLabel (head renderables)
+ last' = linkWithLabel lastLabel (last renderables)
+ length' = length 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
+ customPage = createCustomPage "" []
+ [ ("previous", Right previous)
+ , ("next", Right next)
+ , ("first", Right first)
+ , ("last", Right last')
+ , ("index", Left $ show index)
+ , ("length", Left $ show length')
+ ]
+ in (x `combine` customPage) : paginate' (Just x) xs (index + 1)