<commit_before>{-# LANGUAGE TemplateHaskell #-}
-------------------------------------------------------------------------------
-- |
-- Module      :  Handlers
-- Copyright   :  (c) <NAME> 2010 
-- License     :  as-is
--
-- Maintainer  :  <EMAIL>
-- Stability   :  unstable
-- Portability :  unportable
--
-- Handler functions for each of the site's routes.
--
-------------------------------------------------------------------------------
module Handlers
    ( getRootR
    , getAboutR
    , getPostsR
    , getPostR
    , getTagsR
    , getTagR
    , getFeedR
    , getFaviconR
    , getRobotsR
    ) where

import Yesod
import DevSite
import Posts
import Layouts
import Templates
import qualified Settings as S
import Helpers.RssFeed
 
import Data.Maybe       (catMaybes)
import Data.Time.Clock  (UTCTime)
import Data.Time.Format (parseTime)
import System.Locale    (defaultTimeLocale)

-- | Home page
getRootR :: Handler RepHtml
getRootR = defaultLayout $ do
    let posts = take 10 allPosts
    setTitle $ string "pbrisbin - Home"
    addBody $(S.hamletFile "index")
     
-- | About page
getAboutR :: Handler RepHtml
getAboutR = pageLayout $ do
    setTitle $ string "pbrisbin - About"
    addBody $(S.hamletFile "about")

-- | All posts
getPostsR :: Handler RepHtml
getPostsR = pageLayout $ do
    setTitle $ string "pbrisbin - All Posts"
    addBody  $ allPostsTemplate allPosts "All Posts"

-- | A post
getPostR :: String -> Handler RepHtml
getPostR slug = do
    case loadPost slug of
        []       -> notFound
        (post:_) -> postLayout post

-- | All tags
getTagsR :: Handler RepHtml
getTagsR = pageLayout $ do
    setTitle $ string "pbrisbin - All Tags"
    addBody  $ allPostsTemplate allPosts "All Tags"

-- | A tag
getTagR :: String -> Handler RepHtml
getTagR tag = do
    case getPostsByTag tag of
        []    -> notFound
        posts -> pageLayout $ do
            setTitle $ string $ "pbrisbin - Tag: " ++ tag
            addBody  $ allPostsTemplate posts ("Tag: " ++ tag)

-- | Rss feed
getFeedR :: Handler RepRss
getFeedR = do
    rssFeed RssFeed
        { rssTitle       = "pbrisbin dot com"
        , rssDescription = "New posts on pbrisbin dot com"
        , rssLanguage    = "en-us"
        , rssLinkSelf    = FeedR
        , rssLinkHome    = RootR
        , rssUpdated     = mostRecent
        , rssEntries     = take 10 . catMaybes $ map readRssEntry allPosts
        }
        where
            -- todo: head of empty list
            mostRecent = rssEntryUpdated . head . catMaybes $ map readRssEntry allPosts

-- | Maybe read a single post into an RssEntry depending if the date
--   string can be parsed correctly
readRssEntry :: Post -> Maybe (RssFeedEntry DevSiteRoute)
readRssEntry post = case (readUTCTime $ postDate post) of
    Just date -> Just $ RssFeedEntry
        { rssEntryLink    = PostR $ postSlug post
        , rssEntryUpdated = date
        , rssEntryTitle   = postTitle post
        , rssEntryContent = string $ postDescr post
        }
    Nothing -> Nothing

-- | Read the output of `date -R` into a UTCTime<commit_msg>catMaybes . map is just mapMaybe<commit_after>{-# LANGUAGE TemplateHaskell #-}
-------------------------------------------------------------------------------
-- |
-- Module      :  Handlers
-- Copyright   :  (c) <NAME> 2010 
-- License     :  as-is
--
-- Maintainer  :  <EMAIL>
-- Stability   :  unstable
-- Portability :  unportable
--
-- Handler functions for each of the site's routes.
--
-------------------------------------------------------------------------------
module Handlers
    ( getRootR
    , getAboutR
    , getPostsR
    , getPostR
    , getTagsR
    , getTagR
    , getFeedR
    , getFaviconR
    , getRobotsR
    ) where

import Yesod
import DevSite
import Posts
import Layouts
import Templates
import qualified Settings as S
import Helpers.RssFeed
 
import Data.Maybe       (mapMaybe)
import Data.Time.Clock  (UTCTime)
import Data.Time.Format (parseTime)
import System.Locale    (defaultTimeLocale)

-- | Home page
getRootR :: Handler RepHtml
getRootR = defaultLayout $ do
    let posts = take 10 allPosts
    setTitle $ string "pbrisbin - Home"
    addBody $(S.hamletFile "index")
     
-- | About page
getAboutR :: Handler RepHtml
getAboutR = pageLayout $ do
    setTitle $ string "pbrisbin - About"
    addBody $(S.hamletFile "about")

-- | All posts
getPostsR :: Handler RepHtml
getPostsR = pageLayout $ do
    setTitle $ string "pbrisbin - All Posts"
    addBody  $ allPostsTemplate allPosts "All Posts"

-- | A post
getPostR :: String -> Handler RepHtml
getPostR slug = do
    case loadPost slug of
        []       -> notFound
        (post:_) -> postLayout post

-- | All tags
getTagsR :: Handler RepHtml
getTagsR = pageLayout $ do
    setTitle $ string "pbrisbin - All Tags"
    addBody  $ allPostsTemplate allPosts "All Tags"

-- | A tag
getTagR :: String -> Handler RepHtml
getTagR tag = do
    case getPostsByTag tag of
        []    -> notFound
        posts -> pageLayout $ do
            setTitle $ string $ "pbrisbin - Tag: " ++ tag
            addBody  $ allPostsTemplate posts ("Tag: " ++ tag)

-- | Rss feed
getFeedR :: Handler RepRss
getFeedR = do
    rssFeed RssFeed
        { rssTitle       = "pbrisbin dot com"
        , rssDescription = "New posts on pbrisbin dot com"
        , rssLanguage    = "en-us"
        , rssLinkSelf    = FeedR
        , rssLinkHome    = RootR
        , rssUpdated     = mostRecent
        , rssEntries     = take 10 $ mapMaybe readRssEntry allPosts
        }
        where
            -- todo: head of empty list
            mostRecent = rssEntryUpdated . head $ mapMaybe readRssEntry allPosts

-- | Maybe read a single post into an RssEntry depending if the date
--   string can be parsed correctly
readRssEntry :: Post -> Maybe (RssFeedEntry DevSiteRoute)
readRssEntry post = case (readUTCTime $ postDate post) of
    Just date -> Just $ RssFeedEntry
        { rssEntryLink    = PostR $ postSlug post
        , rssEntryUpdated = date
        , rssEntryTitle   = postTitle post
        , rssEntryContent = string $ postDescr post
        }
    Nothing -> Nothing

-- | Read the output of `date -R` into a UTCTime
{'fraction_non_alphanumeric': 0.13440337224383916, 'fraction_numerical': 0.0025940337224383916, 'mean_word_length': 1.98451862602806, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3889815', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1821, 'n_words': 573}