Permalink
Browse files

Rename Rss plugin to EmbedFeed and make it work with RSS as well as Atom

  • Loading branch information...
1 parent febda60 commit f683a969b883f875d43b88f860126c0623bc85b8 @batterseapower committed Feb 10, 2010
Showing with 53 additions and 45 deletions.
  1. +1 −1 my.conf
  2. +52 −0 plugins/EmbedFeed.hs
  3. +0 −44 plugins/Rss.hs
View
@@ -105,7 +105,7 @@ table-of-contents: yes
# specifies whether to print a tables of contents (with links to
# sections) on each wiki page.
-plugins: plugins/Rss.hs
+plugins: plugins/EmbedFeed.hs
# specifies a list of plugins to load. Plugins may be specified
# either by their path or by their module name. If the plugin name
# starts with Gitit.Plugin., gitit will assume that the plugin is
View
@@ -0,0 +1,52 @@
+{-# LANGUAGE ViewPatterns, PatternGuards #-}
+module EmbedFeed (plugin) where
+
+import Data.Maybe
+import Data.Ord
+import Data.List
+
+import Network.Gitit.Interface
+import Network.Browser
+import Network.HTTP
+
+import qualified Text.Atom.Feed as Atom
+import qualified Text.RSS.Syntax as RSS
+import Text.Feed.Types
+import Text.Feed.Import
+
+
+plugin :: Plugin
+plugin = mkPageTransformM transformBlock
+
+transformBlock :: Block -> PluginM Block
+transformBlock (CodeBlock (_, classes, namevals) contents)
+ | "feed" `elem` classes , Just url <- lookup "url" namevals = do
+ feed_data <- liftIO $ browse $ do
+ setAllowRedirects True
+ fmap (rspBody . snd) $ request $ getRequest url
+
+ --liftIO $ print $ parseFeedString feed_data
+
+ return $ case parseFeedString feed_data of
+ Just (feedToEntries -> Just entries)
+ -> BulletList [[Plain [Link [title] (uri, linktitle)]] | (title, (uri, linktitle)) <- entries]
+ _ -> Para [Emph [Str "Error parsing feed from", Str url]]
+transformBlock x = return x
+
+feedToEntries :: Feed -> Maybe [(Inline, (String, String))]
+feedToEntries (AtomFeed f)
+ = Just [ (title, (Atom.linkHref link, fromMaybe (show title) (Atom.linkTitle link)))
+ | Atom.Entry { Atom.entryTitle = (textContentToInline -> title), Atom.entryLinks = links } <- Atom.feedEntries f
+ , (link:_) <- [sortBy (comparing (maybe True (\t -> "omment" `isInfixOf` t) . Atom.linkTitle))
+ [link | link@(Atom.Link { Atom.linkType = Just "text/html" }) <- links]]
+ ]
+feedToEntries (RSSFeed r)
+ = Just [ (Str title, (url, title))
+ | item@(RSS.RSSItem { RSS.rssItemLink = Just url }) <- RSS.rssItems (RSS.rssChannel r)
+ , let title = fromMaybe (fromJust $ RSS.rssItemDescription item) (RSS.rssItemTitle item)
+ ]
+feedToEntries _ = Nothing
+
+textContentToInline (Atom.TextString s) = Str s
+textContentToInline (Atom.HTMLString s) = HtmlInline s
+textContentToInline (Atom.XHTMLString x) = HtmlInline (show x)
View
@@ -1,44 +0,0 @@
-{-# LANGUAGE ViewPatterns, PatternGuards #-}
-module Rss (plugin) where
-
-import Data.Maybe
-import Data.Ord
-import Data.List
-
-import Debug.Trace
-
-import Network.Gitit.Interface
-import Network.Browser
-import Network.HTTP
-
-import qualified Text.Atom.Feed as Atom
-import Text.Feed.Types
-import Text.Feed.Import
-
-
-plugin :: Plugin
-plugin = mkPageTransformM transformBlock
-
-transformBlock :: Block -> PluginM Block
-transformBlock (CodeBlock (_, classes, namevals) contents)
- | "rss" `elem` classes , Just url <- lookup "url" namevals = do
- feed_data <- liftIO $ browse $ do
- setAllowRedirects True
- fmap (rspBody . snd) $ request $ getRequest url
-
- --liftIO $ print $ parseFeedString feed_data
-
- return $ case parseFeedString feed_data of
- Just (AtomFeed f) ->
- BulletList [ [Para [Link [title] (Atom.linkHref link, fromMaybe (show title) (Atom.linkTitle link))]]
- | Atom.Entry { Atom.entryTitle = (textContentToInline -> title), Atom.entryLinks = links } <- Atom.feedEntries f
- , trace (show $ [link | link@(Atom.Link { Atom.linkType = Just "text/html" }) <- links]) True
- , (link:_) <- [sortBy (comparing (maybe True (\t -> "omment" `isInfixOf` t) . Atom.linkTitle))
- [link | link@(Atom.Link { Atom.linkType = Just "text/html" }) <- links]]
- ]
- _ -> Para [Emph [Str "Error parsing feed from", Str url]]
-transformBlock x = return x
-
-textContentToInline (Atom.TextString s) = Str s
-textContentToInline (Atom.HTMLString s) = HtmlInline s
-textContentToInline (Atom.XHTMLString x) = HtmlInline (show x)

0 comments on commit f683a96

Please sign in to comment.