module GalleryPage (make) where import Control.Exception import Data.Function (on) import Data.List (sortBy) import Data.Ord (comparing) import qualified Data.Text.Lazy as Lazy import Data.Text.Lazy.Builder (Builder, toLazyText) import System.FilePath ((), takeDirectory) import BuilderQQ import Depend (thumbFile) import Info newtype NoThumb = NoThumb FilePath deriving stock Eq deriving anyclass Exception instance Show NoThumb where show (NoThumb dir) = "no thumbnail for " ++ dir make :: Text -> [(FilePath, Info)] -> Lazy.Text make title infos = toLazyText $ make' title infos make' :: Text -> [(FilePath, Info)] -> Builder make' title infos' = [b|@0 $*title

$*title

|] where items = map (uncurry makeItem) infos infos = sortBy (cmpInfo `on` snd) infos' cmpInfo = flip (comparing #date) <> comparing #title makeItem :: FilePath -> Info -> Builder makeItem file info = [b|@4
  • $title
    |] where dir = takeDirectory file thumb = maybe (throw $ NoThumb dir) (\t -> dir thumbFile t) $ #thumb info title = maybe mempty mkTitle $ #title info mkTitle t = [b|@8
    $*t
    |]