on master help sign in
Provide functions for sorting of tags (closes #22)
commitdb03816b5a582de931cf6187fb656c23f81fc621 authorLubomír Sedlář <lubomir.sedlar@gmail.com> dateTue 24 Jan 2012, 23:27 (15 years ago) parentd31e92c
1 file changed+17−1
src/Hakyll/Web/Tags.hs+17−1
src/Hakyll/Web/Tags.hs+17−1
@@ -35,13 +35,17 @@ module Hakyll.Web.Tags
3535 , renderTagList
3636 , renderTagsField
3737 , renderCategoryField
38+ , sortTagsBy
39+ , caseInsensitiveTags
3840 ) where
3941
4042import Prelude hiding (id)
4143import Control.Category (id)
4244import Control.Applicative ((<$>))
45+import Data.Char (toLower)
46+import Data.Ord (comparing)
4347import qualified Data.Map as M
44−import Data.List (intersperse, intercalate)
48+import Data.List (intersperse, intercalate, sortBy)
4549import Control.Arrow (arr, (&&&), (>>>), (***), (<<^), returnA)
4650import Data.Maybe (catMaybes, fromMaybe)
4751import Data.Monoid (mconcat)
@@ -206,3 +210,15 @@ renderCategoryField :: String -- ^ Destination key
206210 -> (String -> Identifier a) -- ^ Create a category link
207211 -> Compiler (Page a) (Page a) -- ^ Resulting compiler
208212renderCategoryField = renderTagsFieldWith getCategory
213+
214+-- | Sort tags using supplied function. First element of the tuple passed to
215+-- the comparing function is the actual tag name.
216+--
217+sortTagsBy :: ((String, [Page a]) -> (String, [Page a]) -> Ordering)
218+ -> Compiler (Tags a) (Tags a)
219+sortTagsBy f = arr $ Tags . sortBy f . tagsMap
220+
221+-- | Sample sorting function that compares tags case insensitively.
222+--
223+caseInsensitiveTags :: (String, [Page a]) -> (String, [Page a]) -> Ordering
224+caseInsensitiveTags = comparing $ map toLower . fst