{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleContexts #-}
module ArticlesList (
ArticlesList(..)
, description
, getArticles
, otherURL
, rssLinkTexts
) where
import Article (Article)
import Blog (Blog(..), Renderer, Skin(..), template)
import Collection (Collection(..))
import Control.Monad.Reader (MonadReader, asks)
import Data.Text (Text, pack)
import Files (absoluteLink)
import Pretty ((.$))
import System.FilePath.Posix ((</>))
data ArticlesList = ArticlesList {
ArticlesList -> Bool
full :: Bool
, ArticlesList -> Collection
collection :: Collection
}
getArticles :: MonadReader Blog m => ArticlesList -> m [Article]
getArticles :: ArticlesList -> m [Article]
getArticles (ArticlesList {Bool
full :: Bool
full :: ArticlesList -> Bool
full, collection :: ArticlesList -> Collection
collection = Collection {[Article]
featured :: Collection -> [Article]
featured :: [Article]
featured}}) = do
[Article] -> [Article]
limit <- Int -> [Article] -> [Article]
forall a. Int -> [a] -> [a]
take (Int -> [Article] -> [Article])
-> m Int -> m ([Article] -> [Article])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Blog -> Int) -> m Int
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks ((Blog -> Int) -> m Int) -> (Blog -> Int) -> m Int
forall a b. (a -> b) -> a -> b
$Blog -> Skin
skin(Blog -> Skin) -> (Skin -> Int) -> Blog -> Int
forall a b c. (a -> b) -> (b -> c) -> a -> c
.$Skin -> Int
previewArticlesCount)
[Article] -> m [Article]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Article] -> m [Article]) -> [Article] -> m [Article]
forall a b. (a -> b) -> a -> b
$ if Bool
full then [Article]
featured else [Article] -> [Article]
limit [Article]
featured
otherURL :: ArticlesList -> String
otherURL :: ArticlesList -> String
otherURL (ArticlesList {Bool
full :: Bool
full :: ArticlesList -> Bool
full, Collection
collection :: Collection
collection :: ArticlesList -> Collection
collection}) = String -> String
absoluteLink (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$
(if Bool
full then String -> String
forall a. a -> a
id else (String -> String -> String
</> String
"all.html")) (String -> String)
-> (Maybe String -> String) -> Maybe String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> (String -> String) -> Maybe String -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" String -> String
forall a. a -> a
id (Maybe String -> String) -> Maybe String -> String
forall a b. (a -> b) -> a -> b
$ Collection -> Maybe String
tag Collection
collection
description :: Renderer m => ArticlesList -> m Text
description :: ArticlesList -> m Text
description (ArticlesList {Bool
full :: Bool
full :: ArticlesList -> Bool
full, Collection
collection :: Collection
collection :: ArticlesList -> Collection
collection}) =
String -> Environment -> m Text
forall (m :: * -> *). Renderer m => String -> Environment -> m Text
template String
page (Environment -> m Text)
-> (Maybe String -> Environment) -> Maybe String -> m Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe String -> Environment
environment (Maybe String -> m Text) -> Maybe String -> m Text
forall a b. (a -> b) -> a -> b
$ Collection -> Maybe String
tag Collection
collection
where
page :: String
page = if Bool
full then String
"allPage" else String
"latestPage"
environment :: Maybe String -> Environment
environment = Environment
-> (String -> Environment) -> Maybe String -> Environment
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] ((String -> Environment) -> Maybe String -> Environment)
-> (String -> Environment) -> Maybe String -> Environment
forall a b. (a -> b) -> a -> b
$ \String
value -> [(Text
"tag", String -> Text
pack String
value)]
rssLinkTexts :: Renderer m => ArticlesList -> m (Text, Text)
(ArticlesList {Collection
collection :: Collection
collection :: ArticlesList -> Collection
collection}) = do
Text
text <- String -> Environment -> m Text
forall (m :: * -> *). Renderer m => String -> Environment -> m Text
template String
"rssLink" []
Text
title <- String -> Environment -> m Text
forall (m :: * -> *). Renderer m => String -> Environment -> m Text
template String
"rssTitle" Environment
environment
(Text, Text) -> m (Text, Text)
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
text, Text
title)
where
environment :: Environment
environment = Environment
-> (String -> Environment) -> Maybe String -> Environment
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (\String
v -> [(Text
"tag", String -> Text
pack String
v)]) (Maybe String -> Environment) -> Maybe String -> Environment
forall a b. (a -> b) -> a -> b
$ Collection -> Maybe String
tag Collection
collection