module Hakyll.Web.Template.Util ( functionField1 , slug , cat , identifierLink ) where import Prolog import Hakyll import Hakyll.Web.Template.Context import Data.String.Slugger import System.FilePath import Witherable functionField1 :: String -> (String -> Compiler String) -> Context a functionField1 n f = functionField n $ \case [x] -> \_ -> f x xs -> \_ -> fail $ "Expecting exactly one argument to " <> n <> ". Instead got " <> show (length xs) slug :: Context a slug = functionField "slug" $ \strs _ -> pure $ toSlug $ unwords strs cat :: Context a cat = functionField "cat" $ \strs _ -> pure $ concat strs identifierLink :: Context a identifierLink = functionField1 "identifier" $ \str -> do getPaths (fromString str) >>= \case [] -> noResult $ "No filepaths match the pattern " <> str x@(_:_:_) -> noResult $ "Multiple filepaths match the pattern " <> str <> ": " <> show x [x] -> pure x getPaths :: Pattern -> Compiler [FilePath] getPaths = getMatches >=> witherM getRoute