<commit_before>module Handler.Blog
    ( getBlogR
    , getBlogPostR
    , getFeedR
    ) where

import Import
import qualified Data.Map as Map
import qualified Data.ByteString as S
import qualified Filesystem.Path.CurrentOS as F
import Text.Blaze (unsafeByteString)
import Settings (blogRoot)
import Data.List (sortBy)
import Data.Ord (comparing)
import Yesod.Feed
import Data.IORef (readIORef)
import Text.Markdown (markdown, def, msXssProtect)
import Data.Text.Encoding (decodeUtf8)
import Data.Text.Lazy (fromStrict)

getBlogR :: Handler ()
getBlogR = getNewestBlog >>= redirect . fst

getBlogPostR :: Year -> Month -> Slug -> Handler RepHtml
getBlogPostR y m s = do
    iblog <- ywBlog <$> getYesod
    Blog blog <- liftIO $ readIORef iblog
    blog' <- maybe notFound return $ Map.lookup y blog
    blog'' <- maybe notFound return $ Map.lookup m blog'
    post <- maybe notFound return $ lookup s blog''
    contentRaw <- liftIO $ S.readFile $ F.encodeString $ blogRoot F.</> postFP post
    let content =
            if F.hasExtension (postFP post) "md"
                then markdown def { msXssProtect = False } $ fromStrict $ decodeUtf8 contentRaw
                else unsafeByteString contentRaw
    let currYear y' = y == y'
        currMonth y' m' = y == y' && m == m'
        currPost y' m' s' = y == y' && m == m' && s == s'
    defaultLayout $ do
        setTitle $ toHtml $ postTitle post
        let rev :: Ord k => Map.Map k v -> [(k, v)]
            rev = reverse . sortBy (comparing fst) . Map.toList
        $(widgetFile "blog")
  where
    pretty 1 = "January"
    pretty 2 = "February"
    pretty 3 = "March"
    pretty 4 = "April"
    pretty 5 = "May"
    pretty 6 = "June"
    pretty 7 = "July"
    pretty 8 = "August"
    pretty 9 = "September"
    pretty 10 = "October"
    pretty 11 = "November"
    pretty 12 = "December" :: Text
    pretty _ = "Some new month"

getFeedR :: Handler RepAtomRss
getFeedR = do
    posts <- getBlogList
    f <-
        case posts of
            [] -> notFound
            (_, f):_ -> return f
    entries <- mapM go $ take 5 posts
    newsFeed Feed
        { feedTitle = "Yesod Web Framework Blog"
        , feedLinkSelf = FeedR
        , feedLinkHome = RootR
        , feedDescription = "Development blog for the Yesod Web Framework"
        , feedLanguage = "en"
        , feedUpdated = postTime f
        , feedEntries = entries
        }
  where
    go (url, p) = do
        content <- liftIO $ S.readFile $ F.encodeString $ blogRoot F.</> postFP p
        return FeedEntry
            { feedEntryLink = url
            , feedEntryUpdated = postTime p
            , feedEntryTitle = postTitle p
            , feedEntryContent = unsafeByteString content
            }
<commit_msg>Fix feed for markdown posts<commit_after>module Handler.Blog
    ( getBlogR
    , getBlogPostR
    , getFeedR
    ) where

import Import
import qualified Data.Map as Map
import qualified Data.ByteString as S
import qualified Filesystem.Path.CurrentOS as F
import Text.Blaze (unsafeByteString)
import Settings (blogRoot)
import Data.List (sortBy)
import Data.Ord (comparing)
import Yesod.Feed
import Data.IORef (readIORef)
import Text.Markdown (markdown, def, msXssProtect)
import Data.Text.Encoding (decodeUtf8)
import Data.Text.Lazy (fromStrict)

getBlogR :: Handler ()
getBlogR = getNewestBlog >>= redirect . fst

getBlogPostR :: Year -> Month -> Slug -> Handler RepHtml
getBlogPostR y m s = do
    iblog <- ywBlog <$> getYesod
    Blog blog <- liftIO $ readIORef iblog
    blog' <- maybe notFound return $ Map.lookup y blog
    blog'' <- maybe notFound return $ Map.lookup m blog'
    post <- maybe notFound return $ lookup s blog''
    content <- liftIO $ getContent post
    let currYear y' = y == y'
        currMonth y' m' = y == y' && m == m'
        currPost y' m' s' = y == y' && m == m' && s == s'
    defaultLayout $ do
        setTitle $ toHtml $ postTitle post
        let rev :: Ord k => Map.Map k v -> [(k, v)]
            rev = reverse . sortBy (comparing fst) . Map.toList
        $(widgetFile "blog")
  where
    pretty 1 = "January"
    pretty 2 = "February"
    pretty 3 = "March"
    pretty 4 = "April"
    pretty 5 = "May"
    pretty 6 = "June"
    pretty 7 = "July"
    pretty 8 = "August"
    pretty 9 = "September"
    pretty 10 = "October"
    pretty 11 = "November"
    pretty 12 = "December" :: Text
    pretty _ = "Some new month"

getFeedR :: Handler RepAtomRss
getFeedR = do
    posts <- getBlogList
    f <-
        case posts of
            [] -> notFound
            (_, f):_ -> return f
    entries <- mapM go $ take 5 posts
    newsFeed Feed
        { feedTitle = "Yesod Web Framework Blog"
        , feedLinkSelf = FeedR
        , feedLinkHome = RootR
        , feedDescription = "Development blog for the Yesod Web Framework"
        , feedLanguage = "en"
        , feedUpdated = postTime f
        , feedEntries = entries
        }
  where
    go (url, p) = do
        content <- liftIO $ getContent p
        return FeedEntry
            { feedEntryLink = url
            , feedEntryUpdated = postTime p
            , feedEntryTitle = postTitle p
            , feedEntryContent = content
            }

getContent :: Post -> IO Html
getContent post = do
    contentRaw <- S.readFile $ F.encodeString $ blogRoot F.</> postFP post
    return $
        if F.hasExtension (postFP post) "md"
            then markdown def { msXssProtect = False } $ fromStrict $ decodeUtf8 contentRaw
            else unsafeByteString contentRaw

{'fraction_non_alphanumeric': 0.09785766158315178, 'fraction_numerical': 0.006535947712418301, 'mean_word_length': 2.279166666666667, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4495648', 'n_tokens_mistral': 1787, 'n_tokens_neox': 1653, 'n_words': 574}