{-# LANGUAGE OverloadedStrings #-} module Util.Hakyll.Routes where import Hakyll import Data.Maybe (fromMaybe) import Data.Map as Map import qualified Data.Text as T import qualified Data.Text.Slugger as Slugger import Debug.Trace import Util.Hash (FileHashes) prefixRoute :: FilePath -> Routes prefixRoute p = customRoute $ \id' -> p ++ toFilePath id' titleRouteWithDefault :: String -> Metadata -> Routes titleRouteWithDefault defaultValue = constRoute . fileNameFromTitle where getTitleFromMeta :: Metadata -> String getTitleFromMeta = fromMaybe defaultValue . lookupString "title" fileNameFromTitle :: Metadata -> FilePath fileNameFromTitle = T.unpack . (`T.append` ".html") . Slugger.toSlug . T.pack . getTitleFromMeta titleRoute :: Metadata -> Maybe Routes titleRoute metadata = constRoute <$> fileNameFromTitle metadata where slug :: String -> String slug = T.unpack . (`T.append` ".html") . Slugger.toSlug . T.pack ignore :: Eq a => a -> a -> Maybe a ignore shouldBeIgnored value | shouldBeIgnored == value = Nothing | otherwise = Just value fileNameFromTitle :: Metadata -> Maybe FilePath fileNameFromTitle metadata = slug <$> (ignore "" =<< lookupString "title" metadata) hashRoute :: FileHashes -> Routes hashRoute hashes = customRoute hash where hash :: Identifier -> String hash = flip (++) ".html" . fromMaybe "error" . flip Map.lookup hashes