From 240374b12744013b7408e3cbfbb29ca10e13adbf Mon Sep 17 00:00:00 2001 From: Michael Snoyman Date: Sat, 25 Dec 2010 22:54:06 +0200 Subject: [PATCH] Imported code from yesod --- LICENSE | 25 ++++++++++ Yesod/Helpers/AtomFeed.hs | 99 +++++++++++++++++++++++++++++++++++++++ yesod-newsfeed.cabal | 24 ++++++++++ 3 files changed, 148 insertions(+) create mode 100644 LICENSE create mode 100644 Yesod/Helpers/AtomFeed.hs create mode 100644 yesod-newsfeed.cabal diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..8643e5d8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,25 @@ +The following license covers this documentation, and the source code, except +where otherwise indicated. + +Copyright 2010, Michael Snoyman. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, +OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Yesod/Helpers/AtomFeed.hs b/Yesod/Helpers/AtomFeed.hs new file mode 100644 index 00000000..c8136c65 --- /dev/null +++ b/Yesod/Helpers/AtomFeed.hs @@ -0,0 +1,99 @@ +{-# LANGUAGE QuasiQuotes #-} +{-# LANGUAGE CPP #-} +--------------------------------------------------------- +-- +-- Module : Yesod.Helpers.AtomFeed +-- Copyright : Michael Snoyman +-- License : BSD3 +-- +-- Maintainer : Michael Snoyman +-- Stability : Stable +-- Portability : portable +-- +-- Generating atom news feeds. +-- +--------------------------------------------------------- + +-- | Generation of Atom newsfeeds. See +-- . +module Yesod.Helpers.AtomFeed + ( AtomFeed (..) + , AtomFeedEntry (..) + , atomFeed + , atomLink + , RepAtom (..) + ) where + +import Yesod.Content +import Yesod.Handler +import Yesod.Widget +import Text.Hamlet +import Data.Time.Clock (UTCTime) + +newtype RepAtom = RepAtom Content +instance HasReps RepAtom where + chooseRep (RepAtom c) _ = return (typeAtom, c) + +atomFeed :: AtomFeed (Route master) -> GHandler sub master RepAtom +atomFeed = fmap RepAtom . hamletToContent . template + +data AtomFeed url = AtomFeed + { atomTitle :: String + , atomLinkSelf :: url + , atomLinkHome :: url + , atomUpdated :: UTCTime + , atomEntries :: [AtomFeedEntry url] + } + +data AtomFeedEntry url = AtomFeedEntry + { atomEntryLink :: url + , atomEntryUpdated :: UTCTime + , atomEntryTitle :: String + , atomEntryContent :: Html + } + +template :: AtomFeed url -> Hamlet url +template arg = +#if __GLASGOW_HASKELL__ >= 700 + [xhamlet| +#else + [$xhamlet| +#endif +\ +%feed!xmlns="http://www.w3.org/2005/Atom" + %title $atomTitle.arg$ + %link!rel=self!href=@atomLinkSelf.arg@ + %link!href=@atomLinkHome.arg@ + %updated $formatW3.atomUpdated.arg$ + %id @atomLinkHome.arg@ + $forall atomEntries.arg entry + ^entryTemplate.entry^ +|] + +entryTemplate :: AtomFeedEntry url -> Hamlet url +entryTemplate arg = +#if __GLASGOW_HASKELL__ >= 700 + [xhamlet| +#else + [$xhamlet| +#endif +%entry + %id @atomEntryLink.arg@ + %link!href=@atomEntryLink.arg@ + %updated $formatW3.atomEntryUpdated.arg$ + %title $atomEntryTitle.arg$ + %content!type=html $cdata.atomEntryContent.arg$ +|] + +-- | Generates a link tag in the head of a widget. +atomLink :: Route m + -> String -- ^ title + -> GWidget s m () +atomLink u title = addHamletHead +#if __GLASGOW_HASKELL__ >= 700 + [hamlet| +#else + [$hamlet| +#endif +%link!href=@u@!type="application/atom+xml"!rel="alternate"!title=$title$ +|] diff --git a/yesod-newsfeed.cabal b/yesod-newsfeed.cabal new file mode 100644 index 00000000..bd7eae2b --- /dev/null +++ b/yesod-newsfeed.cabal @@ -0,0 +1,24 @@ +name: yesod-newsfeed +version: 0.7.0 +license: BSD3 +license-file: LICENSE +author: Michael Snoyman +maintainer: Michael Snoyman +synopsis: Helper functions and data types for producing Atom feeds. +category: Web, Yesod +stability: Stable +cabal-version: >= 1.6 +build-type: Simple +homepage: http://docs.yesodweb.com/ + +library + build-depends: base >= 4 && < 5 + , yesod-core >= 0.7 && < 0.8 + , time >= 1.1.4 && < 1.3 + , hamlet >= 0.7 && < 0.8 + exposed-modules: Yesod.Helpers.AtomFeed + ghc-options: -Wall + +source-repository head + type: git + location: git://github.com/snoyberg/yesod-newsfeed.git