on master help sign in
Provide functions for sorting of tags (closes #22)
db03816Lubomír Sedlář
src/Hakyll/Web/Tags.hs+17−1
@@ -35,13 +35,17 @@ module Hakyll.Web.Tags
35 , renderTagList
35 , renderTagList
36 , renderTagsField
36 , renderTagsField
37 , renderCategoryField
37 , renderCategoryField
38+ , sortTagsBy
39+ , caseInsensitiveTags
38 ) where
40 ) where
39
41
40import Prelude hiding (id)
42import Prelude hiding (id)
41import Control.Category (id)
43import Control.Category (id)
42import Control.Applicative ((<$>))
44import Control.Applicative ((<$>))
45+import Data.Char (toLower)
46+import Data.Ord (comparing)
43import qualified Data.Map as M
47import qualified Data.Map as M
44−import Data.List (intersperse, intercalate)
48+import Data.List (intersperse, intercalate, sortBy)
45import Control.Arrow (arr, (&&&), (>>>), (***), (<<^), returnA)
49import Control.Arrow (arr, (&&&), (>>>), (***), (<<^), returnA)
46import Data.Maybe (catMaybes, fromMaybe)
50import Data.Maybe (catMaybes, fromMaybe)
47import Data.Monoid (mconcat)
51import Data.Monoid (mconcat)
@@ -206,3 +210,15 @@ renderCategoryField :: String -- ^ Destination key
206 -> (String -> Identifier a) -- ^ Create a category link
210 -> (String -> Identifier a) -- ^ Create a category link
207 -> Compiler (Page a) (Page a) -- ^ Resulting compiler
211 -> Compiler (Page a) (Page a) -- ^ Resulting compiler
208renderCategoryField = renderTagsFieldWith getCategory
212renderCategoryField = 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