We want to use MonadMetadata
and getMetadataField
from Hakyll to read the coolness, then parse it to a number, and sort by that number.
Stealing heavily from The source for recentFirst
, you could write something like:
{-# LANGUAGE TupleSections #-}
import Hakyll
import Control.Monad (void, (>>=), liftM)
import Data.Ord (comparing)
import Data.List(sortOn, sortBy)
import Data.Maybe(fromMaybe)
import Text.Read(readMaybe)
import Data.Foldable (toList)
-- this parses the coolness out of an item
-- it defaults to 0 if it's missing, or can't be parsed as an Int
coolness :: MonadMetadata m => Item a -> m Int
coolness i = do
mStr <- getMetadataField (itemIdentifier i) "coolness"
return $ (fromMaybe 0 $ mStr >>= readMaybe)
byCoolness :: MonadMetadata m => [Item a] -> m [Item a]
byCoolness = sortByM coolness
where
sortByM :: (Monad m, Ord k) => (a -> m k) -> [a] -> m [a]
sortByM f xs = liftM (map fst . sortBy (comparing snd)) $
mapM (\x -> liftM (x,) (f x)) xs
This sorts on the extracted coolness value.
It sorts in increasing coolness, so if you want "coolest first" do:
posts <- reverse <$> (byCoolness =<< loadAll "posts/*")