<commit_before>{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
---------------------------------------------------------
--
-- Module        : Yesod.Helpers.AtomFeed
-- Copyright     : <NAME>
-- License       : BSD3
--
-- Maintainer    : <NAME> <<EMAIL>>
-- Stability     : Stable
-- Portability   : portable
--
-- Generating atom news feeds.
--
---------------------------------------------------------

module Yesod.Helpers.AtomFeed
    ( AtomFeed (..)
    , AtomFeedEntry (..)
    , AtomFeedResponse (..)
    , atomFeed
    ) where

import Yesod
import Data.Time.Clock (UTCTime)
-- FIXME import Web.Encodings (formatW3)
import Data.Convertible.Text

data AtomFeedResponse = AtomFeedResponse AtomFeed Approot

atomFeed :: Yesod y => AtomFeed -> Handler y AtomFeedResponse
atomFeed f = do
    y <- getYesod
    return $ AtomFeedResponse f $ approot y

data AtomFeed = AtomFeed
    { atomTitle :: String
    , atomLinkSelf :: Location
    , atomLinkHome :: Location
    , atomUpdated :: UTCTime
    , atomEntries :: [AtomFeedEntry]
    }
instance HasReps AtomFeedResponse where
    chooseRep = defChooseRep
        [ (TypeAtom, return . cs)
        ]

data AtomFeedEntry = AtomFeedEntry
    { atomEntryLink :: Location
    , atomEntryUpdated :: UTCTime
    , atomEntryTitle :: String
    , atomEntryContent :: HtmlContent
    }

instance ConvertSuccess AtomFeedResponse Content where
    convertSuccess = error "FIXME" -- cs . (cs :: Html -> XmlDoc) . cs
{- FIXME
instance ConvertSuccess AtomFeedResponse Html where
    convertSuccess (AtomFeedResponse f ar) =
        Tag "feed" [("xmlns", "http://www.w3.org/2005/Atom")] $ HtmlList
          [ Tag "title" [] $ cs $ atomTitle f
          , EmptyTag "link" [ ("rel", "self")
                            , ("href", showLocation ar $ atomLinkSelf f)
                            ]
          , EmptyTag "link" [ ("href", showLocation ar $ atomLinkHome f)
                            ]
          , Tag "updated" [] $ cs $ formatW3 $ atomUpdated f
          , Tag "id" [] $ cs $ showLocation ar $ atomLinkHome f
          , HtmlList $ map cs $ zip (atomEntries f) $ repeat ar
        ]

instance ConvertSuccess (AtomFeedEntry, Approot) Html where
    convertSuccess (e, ar) = Tag "entry" [] $ HtmlList
        [ Tag "id" [] $ cs $ showLocation ar $ atomEntryLink e
        , EmptyTag "link" [("href", showLocation ar $ atomEntryLink e)]
        , Tag "updated" [] $ cs $ formatW3 $ atomEntryUpdated e
        , Tag "title" [] $ cs $ atomEntryTitle e
        , Tag "content" [("type", "html")] $ cdata $ atomEntryContent e
        ]
-}<commit_msg>Began converting AtomFeed to hamlet<commit_after>{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE QuasiQuotes #-}
---------------------------------------------------------
--
-- Module        : Yesod.Helpers.AtomFeed
-- Copyright     : <NAME>
-- License       : BSD3
--
-- Maintainer    : <NAME> <<EMAIL>>
-- Stability     : Stable
-- Portability   : portable
--
-- Generating atom news feeds.
--
---------------------------------------------------------

module Yesod.Helpers.AtomFeed
    ( AtomFeed (..)
    , AtomFeedEntry (..)
    --, atomFeed
    , template -- FIXME
    ) where

import Yesod
import Data.Time.Clock (UTCTime)
import Web.Encodings (formatW3)
import Text.Hamlet.Monad

{-
atomFeed :: Yesod y => AtomFeed -> Handler y AtomFeedResponse
atomFeed f = do
    y <- getYesod
    return $ AtomFeedResponse f $ approot y
-}

data AtomFeed url = AtomFeed
    { atomTitle :: String
    , atomLinkSelf :: url
    , atomLinkHome :: url
    , atomUpdated :: UTCTime
    , atomEntries :: [AtomFeedEntry url]
    }
{- FIXME
instance HasReps (AtomFeed url) where
    chooseRep = defChooseRep
        [ (TypeAtom, return . cs)
        ]
-}

data AtomFeedEntry url = AtomFeedEntry
    { atomEntryLink :: url
    , atomEntryUpdated :: UTCTime
    , atomEntryTitle :: String
    , atomEntryContent :: HtmlContent
    }

xmlns :: a -> HtmlContent
xmlns _ = cs "http://www.w3.org/2005/Atom"

template :: AtomFeed url -> Hamlet url IO ()
template = [$hamlet|
%feed!xmlns=$xmlns$
    %title $atomTitle.cs$
    %link!rel=self!href=@atomLinkSelf@
    %link!href=@atomLinkHome@
    %updated $atomUpdated.formatW3.cs$
    %id @atomLinkHome@
    $forall atomEntries entry
        ^entry.entryTemplate^
|]

entryTemplate :: AtomFeedEntry url -> Hamlet url IO ()
entryTemplate = [$hamlet|
%entry
    %id @atomEntryLink@
    %link!href=@atomEntryLink@
    %updated $atomEntryUpdated.formatW3.cs$
    %title $atomEntryTitle.cs$
    %content!type=html $atomEntryContent.cdata$
|]
{'fraction_non_alphanumeric': 0.16626975535830266, 'fraction_numerical': 0.0038969473912102187, 'mean_word_length': 2.1972318339100347, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 5}, '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': '5905616', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1427, 'n_words': 355}