module Panda.View.Widget.RSS where

-- env
import Panda.Helper.Env -- hiding (Language)
import Prelude hiding ((.), (/), (^), id)
import qualified Panda.Config.Global as G
import qualified Panda.Type.Pager as Pager
import Text.RSS
import qualified Text.RSS as RSS

-- model
import Panda.Model.Post as Post

-- RSS
channel_rss_template = [ RSS.Language "en-us" ]

render_rss x  | G.summary_for_rss.is True = x.markup_summary.show
              | otherwise                 = x.markup.show

item_rss_template x = 
  [ Title $ x.Post.title
  , Description $ x.render_rss
  , Author $ G.author_email
  , Link $ x.item_uri
  , PubDate $ x.date
  ]

instance Default URI where
  def = nullURI

item_uri x = def { uriScheme = "http://", uriPath = host_link (x.uri) }
rss_uri x  = def { uriScheme = "http://", uriPath = host_link x }

rss categary s xs = RSS title  (rss_uri link) title channel_rss_template (xs.take 20 .map item_rss_template)
  .rssToXML.showXML
  where
    link = categary / s
    title = if s.empty then G.blog_title else G.blog_title ++ " / " ++ s

host_link s = G.host_name ++ (G.root / s)