on master help sign in
Merge pull request #52 from lubomir/master

Make sorting tags easier

commite12e64233d8937af38c4511387eace008bb4fa6b authorJasper Van der Jeugt <jaspervdj@gmail.com> dateWed 25 Jan 2012, 01:58 (15 years ago) parentf90f29fdb03816
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