-- |
-- Description: Rules for generating the site map.
-- Copyright: Copyright (C) 2023 Yoo Chung
-- License: All rights reserved
-- Maintainer: web@chungyc.org
--
-- Exports rules related to generating the sitemap for the web site.
module Web.Site.Rules.Sitemap (rules) where

import Hakyll
import Web.Site.Compilers

-- |
-- Given a pattern which matches all the pages on the web site,
-- returns the rules which generates the sitemap.
rules :: Pattern -> Rules ()
rules :: Pattern -> Rules ()
rules Pattern
items = do
  Pattern -> Rules () -> Rules ()
match Pattern
"sitemap.xml" (Rules () -> Rules ()) -> Rules () -> Rules ()
forall a b. (a -> b) -> a -> b
$ do
    Routes -> Rules ()
route Routes
idRoute
    Compiler (Item String) -> Rules ()
forall a.
(Binary a, Typeable a, Writable a) =>
Compiler (Item a) -> Rules ()
compile (Compiler (Item String) -> Rules ())
-> Compiler (Item String) -> Rules ()
forall a b. (a -> b) -> a -> b
$ do
      [Item String]
itemList <- Pattern -> Compiler [Item String]
forall a. (Binary a, Typeable a) => Pattern -> Compiler [Item a]
loadAll Pattern
items
      let sitemapContext :: Context String
sitemapContext =
            String
-> Context String -> Compiler [Item String] -> Context String
forall a b. String -> Context a -> Compiler [Item a] -> Context b
listField String
"items" Context String
siteContext ([Item String] -> Compiler [Item String]
forall a. a -> Compiler a
forall (m :: * -> *) a. Monad m => a -> m a
return [Item String]
itemList)
              Context String -> Context String -> Context String
forall a. Semigroup a => a -> a -> a
<> Context String
siteContext
      Compiler (Item String)
getResourceBody Compiler (Item String)
-> (Item String -> Compiler (Item String))
-> Compiler (Item String)
forall a b. Compiler a -> (a -> Compiler b) -> Compiler b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Context String -> Item String -> Compiler (Item String)
applyAsTemplate Context String
sitemapContext