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

import Hakyll
import Web.Site.Compilers
import Web.Site.Routes
import Web.Site.Rules.Article qualified as Article
import Web.Site.Rules.Diagram qualified as Diagram
import Web.Site.Rules.File qualified as File
import Web.Site.Rules.Link qualified as Link
import Web.Site.Rules.Publication qualified as Publication
import Web.Site.Rules.Server qualified as Server
import Web.Site.Rules.Sitemap qualified as Sitemap
import Web.Site.Rules.Stylesheet qualified as Stylesheet
import Web.Site.Rules.Update qualified as Update

-- |
-- Rules for Hakyll to generate the web site.
--
-- This encompasses all of the rules exported by all of the modules defining rules.
rules :: Rules ()
rules :: Rules ()
rules = do
  Rules ()
Server.rules
  Rules ()
Stylesheet.rules
  Rules ()
Diagram.rules
  Rules ()
File.rules
  Rules ()
Update.rules
  Rules ()
Article.rules
  Rules ()
Publication.rules
  Rules ()
Link.rules

  Pattern -> Rules () -> Rules ()
match Pattern
"templates/*" (Rules () -> Rules ()) -> Rules () -> Rules ()
forall a b. (a -> b) -> a -> b
$ Compiler (Item Template) -> Rules ()
forall a.
(Binary a, Typeable a, Writable a) =>
Compiler (Item a) -> Rules ()
compile Compiler (Item Template)
templateBodyCompiler

  Pattern -> Rules () -> Rules ()
match Pattern
"about.html" (Rules () -> Rules ()) -> Rules () -> Rules ()
forall a b. (a -> b) -> a -> b
$ do
    Routes -> Rules ()
route Routes
dropExtensions
    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
$
      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
>>= Identifier
-> Context String -> Item String -> Compiler (Item String)
forall a.
Identifier -> Context a -> Item a -> Compiler (Item String)
loadAndApplyTemplate Identifier
"templates/default.html" Context String
siteContext

  Pattern -> Rules () -> Rules ()
match Pattern
"index.html" (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
$ (Context String -> Compiler (Item String))
-> Compiler (Item String)
Update.withLatest ((Context String -> Compiler (Item String))
 -> Compiler (Item String))
-> (Context String -> Compiler (Item String))
-> Compiler (Item String)
forall a b. (a -> b) -> a -> b
$ \Context String
indexContext -> do
      let context :: Context String
context = String -> (Item String -> Bool) -> Context String
forall a. String -> (Item a -> Bool) -> Context a
boolField String
"front" (Bool -> Item String -> Bool
forall a b. a -> b -> a
const Bool
True) Context String -> Context String -> Context String
forall a. Semigroup a => a -> a -> a
<> Context String
indexContext
      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
context
        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
>>= Identifier
-> Context String -> Item String -> Compiler (Item String)
forall a.
Identifier -> Context a -> Item a -> Compiler (Item String)
loadAndApplyTemplate Identifier
"templates/default.html" Context String
context

  Pattern -> Rules ()
Sitemap.rules (Pattern -> Rules ()) -> Pattern -> Rules ()
forall a b. (a -> b) -> a -> b
$
    Pattern
"about.html"
      Pattern -> Pattern -> Pattern
.||. Pattern
"index.html"
      Pattern -> Pattern -> Pattern
.||. Pattern
Article.items
      Pattern -> Pattern -> Pattern
.||. Pattern
Link.items
      Pattern -> Pattern -> Pattern
.||. Pattern
Publication.items
      Pattern -> Pattern -> Pattern
.||. Pattern
Update.items