module Rules.Blog.Paginate.MonthlyPosts (
    build
) where

import           Control.Monad.Reader      (asks)
import           Control.Monad.Trans       (MonadTrans (..))
import           Hakyll
import           System.FilePath           ((</>))

import           Archives                  (MonthlyArchives, archivesRules)
import           Config.Blog               (BlogConfig (..))
import           Rules.Blog.ListPage       (ListPageOpts, listPage)
import           Rules.Blog.Paginate.Utils
import           Rules.Blog.Type
import qualified Vendor.FontAwesome        as FA

build :: FA.FontAwesomeIcons
    -> Tags
    -> ListPageOpts
    -> BlogConfReader Rules Rules MonthlyArchives
build :: FontAwesomeIcons
-> Tags
-> ListPageOpts
-> BlogConfReader Rules Rules MonthlyArchives
build FontAwesomeIcons
faIcons Tags
tags ListPageOpts
listPageOpts = do
    MonthlyArchives
monthlyArchives <- (BlogConfig Rules -> Rules MonthlyArchives)
-> ReaderT (BlogConfig Rules) Rules (Rules MonthlyArchives)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks BlogConfig Rules -> Rules MonthlyArchives
forall (m :: * -> *). BlogConfig m -> m MonthlyArchives
blogMonthlyArchivesBuilder ReaderT (BlogConfig Rules) Rules (Rules MonthlyArchives)
-> (Rules MonthlyArchives
    -> BlogConfReader Rules Rules MonthlyArchives)
-> BlogConfReader Rules Rules MonthlyArchives
forall a b.
ReaderT (BlogConfig Rules) Rules a
-> (a -> ReaderT (BlogConfig Rules) Rules b)
-> ReaderT (BlogConfig Rules) Rules b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Rules MonthlyArchives -> BlogConfReader Rules Rules MonthlyArchives
forall (m :: * -> *) a.
Monad m =>
m a -> ReaderT (BlogConfig Rules) m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift
    [Identifier] -> Rules [[Identifier]]
grouper' <- BlogConfReader Rules Rules ([Identifier] -> Rules [[Identifier]])
forall (m :: * -> *) (f :: * -> *) (n :: * -> *).
(Monad m, MonadMetadata f, MonadFail f) =>
BlogConfReader n m ([Identifier] -> f [[Identifier]])
grouper
    (FilePath, FilePath) -> PageNumber -> Identifier
makeId' <- (BlogConfig Rules -> (FilePath, FilePath) -> FilePath)
-> BlogConfReader
     Rules Rules ((FilePath, FilePath) -> PageNumber -> Identifier)
forall (n :: * -> *) (m :: * -> *) a.
Monad n =>
(BlogConfig m -> a -> FilePath)
-> BlogConfReader m n (a -> PageNumber -> Identifier)
makeId BlogConfig Rules -> (FilePath, FilePath) -> FilePath
forall (m :: * -> *).
BlogConfig m -> (FilePath, FilePath) -> FilePath
blogMonthlyPagePath
    Rules () -> ReaderT (BlogConfig Rules) Rules ()
forall (m :: * -> *) a.
Monad m =>
m a -> ReaderT (BlogConfig Rules) m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Rules () -> ReaderT (BlogConfig Rules) Rules ())
-> Rules () -> ReaderT (BlogConfig Rules) Rules ()
forall a b. (a -> b) -> a -> b
$ MonthlyArchives
-> ((FilePath, FilePath) -> Pattern -> Rules ()) -> Rules ()
forall a. Archives a -> (a -> Pattern -> Rules ()) -> Rules ()
archivesRules MonthlyArchives
monthlyArchives (((FilePath, FilePath) -> Pattern -> Rules ()) -> Rules ())
-> ((FilePath, FilePath) -> Pattern -> Rules ()) -> Rules ()
forall a b. (a -> b) -> a -> b
$ \key :: (FilePath, FilePath)
key@(FilePath
year, FilePath
month) Pattern
pat ->
        let title' :: FilePath
title' = FilePath -> FilePath -> FilePath
title FilePath
"Monthly" (FilePath -> FilePath) -> FilePath -> FilePath
forall a b. (a -> b) -> a -> b
$ FilePath
year FilePath -> FilePath -> FilePath
</> FilePath
month
        in ([Identifier] -> Rules [[Identifier]])
-> Pattern -> (PageNumber -> Identifier) -> Rules Paginate
forall (m :: * -> *).
MonadMetadata m =>
([Identifier] -> m [[Identifier]])
-> Pattern -> (PageNumber -> Identifier) -> m Paginate
buildPaginateWith [Identifier] -> Rules [[Identifier]]
grouper' Pattern
pat ((FilePath, FilePath) -> PageNumber -> Identifier
makeId' (FilePath, FilePath)
key)
            Rules Paginate -> (Paginate -> Rules ()) -> Rules ()
forall a b. Rules a -> (a -> Rules b) -> Rules b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Maybe FilePath
-> FontAwesomeIcons -> Tags -> ListPageOpts -> Paginate -> Rules ()
listPage (FilePath -> Maybe FilePath
forall a. a -> Maybe a
Just FilePath
title') FontAwesomeIcons
faIcons Tags
tags ListPageOpts
listPageOpts
    MonthlyArchives -> BlogConfReader Rules Rules MonthlyArchives
forall a. a -> ReaderT (BlogConfig Rules) Rules a
forall (f :: * -> *) a. Applicative f => a -> f a
pure MonthlyArchives
monthlyArchives