Location datatype
This commit is contained in:
parent
29e6567c65
commit
dc355edf7d
4
TODO
4
TODO
@ -1,9 +1,5 @@
|
|||||||
HTML sitemap generation
|
|
||||||
Cleanup Data.Object.Translate
|
Cleanup Data.Object.Translate
|
||||||
Cleanup Parameter stuff. Own module? Interface with formlets?
|
Cleanup Parameter stuff. Own module? Interface with formlets?
|
||||||
Authentication via e-mail address built in. (eaut.org)
|
Authentication via e-mail address built in. (eaut.org)
|
||||||
OpenID 2 stuff (for direct Google login).
|
OpenID 2 stuff (for direct Google login).
|
||||||
Is there a mimetype package on hackage for Yesod.Helpers.Static?
|
|
||||||
Native support for HStringTemplate groups.
|
Native support for HStringTemplate groups.
|
||||||
AtomFeed uses RelLoc and AbsLoc like Sitemap
|
|
||||||
Fix type of sitemap
|
|
||||||
|
|||||||
@ -19,6 +19,8 @@ module Yesod.Definitions
|
|||||||
, Resource
|
, Resource
|
||||||
, Approot (..)
|
, Approot (..)
|
||||||
, Language
|
, Language
|
||||||
|
, Location (..)
|
||||||
|
, showLocation
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import qualified Hack
|
import qualified Hack
|
||||||
@ -55,3 +57,12 @@ type Resource = [String]
|
|||||||
newtype Approot = Approot { unApproot :: String }
|
newtype Approot = Approot { unApproot :: String }
|
||||||
|
|
||||||
type Language = String
|
type Language = String
|
||||||
|
|
||||||
|
-- | A location string. Can either be given absolutely or as a suffix for the
|
||||||
|
-- 'Approot'.
|
||||||
|
data Location = AbsLoc String | RelLoc String
|
||||||
|
|
||||||
|
-- | Display a 'Location' in absolute form.
|
||||||
|
showLocation :: Approot -> Location -> String
|
||||||
|
showLocation _ (AbsLoc s) = s
|
||||||
|
showLocation (Approot ar) (RelLoc s) = ar ++ s
|
||||||
|
|||||||
@ -17,6 +17,8 @@
|
|||||||
module Yesod.Helpers.AtomFeed
|
module Yesod.Helpers.AtomFeed
|
||||||
( AtomFeed (..)
|
( AtomFeed (..)
|
||||||
, AtomFeedEntry (..)
|
, AtomFeedEntry (..)
|
||||||
|
, AtomFeedResponse (..)
|
||||||
|
, atomFeed
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Yesod
|
import Yesod
|
||||||
@ -26,58 +28,65 @@ import qualified Data.Text.Lazy as TL
|
|||||||
import Data.Time.Clock
|
import Data.Time.Clock
|
||||||
import Web.Encodings
|
import Web.Encodings
|
||||||
|
|
||||||
|
data AtomFeedResponse = AtomFeedResponse AtomFeed Approot
|
||||||
|
|
||||||
|
atomFeed :: YesodApproot y => AtomFeed -> Handler y AtomFeedResponse
|
||||||
|
atomFeed f = do
|
||||||
|
y <- getYesod
|
||||||
|
return $ AtomFeedResponse f $ approot y
|
||||||
|
|
||||||
data AtomFeed = AtomFeed
|
data AtomFeed = AtomFeed
|
||||||
{ atomTitle :: String
|
{ atomTitle :: String
|
||||||
, atomLinkSelf :: String
|
, atomLinkSelf :: Location
|
||||||
, atomLinkHome :: String
|
, atomLinkHome :: Location
|
||||||
, atomUpdated :: UTCTime
|
, atomUpdated :: UTCTime
|
||||||
, atomEntries :: [AtomFeedEntry]
|
, atomEntries :: [AtomFeedEntry]
|
||||||
}
|
}
|
||||||
instance HasReps AtomFeed where
|
instance HasReps AtomFeedResponse where
|
||||||
reps =
|
reps =
|
||||||
[ (TypeAtom, return . cs)
|
[ (TypeAtom, return . cs)
|
||||||
]
|
]
|
||||||
|
|
||||||
data AtomFeedEntry = AtomFeedEntry
|
data AtomFeedEntry = AtomFeedEntry
|
||||||
{ atomEntryLink :: String
|
{ atomEntryLink :: Location
|
||||||
, atomEntryUpdated :: UTCTime
|
, atomEntryUpdated :: UTCTime
|
||||||
, atomEntryTitle :: String
|
, atomEntryTitle :: String
|
||||||
, atomEntryContent :: Html
|
, atomEntryContent :: Html
|
||||||
}
|
}
|
||||||
|
|
||||||
instance ConvertSuccess AtomFeed Content where
|
instance ConvertSuccess AtomFeedResponse Content where
|
||||||
convertSuccess = cs . (cs :: AtomFeed -> Text)
|
convertSuccess = (cs :: Text -> Content) . cs
|
||||||
instance ConvertSuccess AtomFeed Text where
|
instance ConvertSuccess AtomFeedResponse Text where
|
||||||
convertSuccess f = TL.concat
|
convertSuccess (AtomFeedResponse f ar) = TL.concat
|
||||||
[ cs "<?xml version='1.0' encoding='utf-8' ?>\n"
|
[ cs "<?xml version='1.0' encoding='utf-8' ?>\n"
|
||||||
, cs "<feed xmlns='http://www.w3.org/2005/Atom'>"
|
, cs "<feed xmlns='http://www.w3.org/2005/Atom'>"
|
||||||
, cs "<title>"
|
, cs "<title>"
|
||||||
, encodeHtml $ cs $ atomTitle f
|
, encodeHtml $ cs $ atomTitle f
|
||||||
, cs "</title>"
|
, cs "</title>"
|
||||||
, cs "<link rel='self' href='"
|
, cs "<link rel='self' href='"
|
||||||
, encodeHtml $ cs $ atomLinkSelf f
|
, encodeHtml $ cs $ showLocation ar $ atomLinkSelf f
|
||||||
, cs "'/>"
|
, cs "'/>"
|
||||||
, cs "<link href='"
|
, cs "<link href='"
|
||||||
, encodeHtml $ cs $ atomLinkHome f
|
, encodeHtml $ cs $ showLocation ar $ atomLinkHome f
|
||||||
, cs "'/>"
|
, cs "'/>"
|
||||||
, cs "<updated>"
|
, cs "<updated>"
|
||||||
, cs $ formatW3 $ atomUpdated f
|
, cs $ formatW3 $ atomUpdated f
|
||||||
, cs "</updated>"
|
, cs "</updated>"
|
||||||
, cs "<id>"
|
, cs "<id>"
|
||||||
, encodeHtml $ cs $ atomLinkHome f
|
, encodeHtml $ cs $ showLocation ar $ atomLinkHome f
|
||||||
, cs "</id>"
|
, cs "</id>"
|
||||||
, TL.concat $ map cs $ atomEntries f
|
, TL.concat $ map cs $ zip (atomEntries f) $ repeat ar
|
||||||
, cs "</feed>"
|
, cs "</feed>"
|
||||||
]
|
]
|
||||||
|
|
||||||
instance ConvertSuccess AtomFeedEntry Text where
|
instance ConvertSuccess (AtomFeedEntry, Approot) Text where
|
||||||
convertSuccess e = TL.concat
|
convertSuccess (e, ar) = TL.concat
|
||||||
[ cs "<entry>"
|
[ cs "<entry>"
|
||||||
, cs "<id>"
|
, cs "<id>"
|
||||||
, encodeHtml $ cs $ atomEntryLink e
|
, encodeHtml $ cs $ showLocation ar $ atomEntryLink e
|
||||||
, cs "</id>"
|
, cs "</id>"
|
||||||
, cs "<link href='"
|
, cs "<link href='"
|
||||||
, encodeHtml $ cs $ atomEntryLink e
|
, encodeHtml $ cs $ showLocation ar $ atomEntryLink e
|
||||||
, cs "' />"
|
, cs "' />"
|
||||||
, cs "<updated>"
|
, cs "<updated>"
|
||||||
, cs $ formatW3 $ atomEntryUpdated e
|
, cs $ formatW3 $ atomEntryUpdated e
|
||||||
|
|||||||
@ -19,7 +19,6 @@ module Yesod.Helpers.Sitemap
|
|||||||
( sitemap
|
( sitemap
|
||||||
, robots
|
, robots
|
||||||
, SitemapUrl (..)
|
, SitemapUrl (..)
|
||||||
, SitemapLoc (..)
|
|
||||||
, SitemapChangeFreq (..)
|
, SitemapChangeFreq (..)
|
||||||
, SitemapResponse (..)
|
, SitemapResponse (..)
|
||||||
) where
|
) where
|
||||||
@ -34,7 +33,6 @@ import Data.Text.Lazy (Text)
|
|||||||
import qualified Data.Text.Lazy as TL
|
import qualified Data.Text.Lazy as TL
|
||||||
import Yesod.Yesod
|
import Yesod.Yesod
|
||||||
|
|
||||||
data SitemapLoc = AbsLoc String | RelLoc String
|
|
||||||
data SitemapChangeFreq = Always
|
data SitemapChangeFreq = Always
|
||||||
| Hourly
|
| Hourly
|
||||||
| Daily
|
| Daily
|
||||||
@ -52,7 +50,7 @@ instance ConvertSuccess SitemapChangeFreq String where
|
|||||||
convertSuccess Never = "never"
|
convertSuccess Never = "never"
|
||||||
|
|
||||||
data SitemapUrl = SitemapUrl
|
data SitemapUrl = SitemapUrl
|
||||||
{ sitemapLoc :: SitemapLoc
|
{ sitemapLoc :: Location
|
||||||
, sitemapLastMod :: UTCTime
|
, sitemapLastMod :: UTCTime
|
||||||
, sitemapChangeFreq :: SitemapChangeFreq
|
, sitemapChangeFreq :: SitemapChangeFreq
|
||||||
, priority :: Double
|
, priority :: Double
|
||||||
@ -61,7 +59,7 @@ data SitemapResponse = SitemapResponse [SitemapUrl] Approot
|
|||||||
instance ConvertSuccess SitemapResponse Content where
|
instance ConvertSuccess SitemapResponse Content where
|
||||||
convertSuccess = cs . (cs :: SitemapResponse -> Text)
|
convertSuccess = cs . (cs :: SitemapResponse -> Text)
|
||||||
instance ConvertSuccess SitemapResponse Text where
|
instance ConvertSuccess SitemapResponse Text where
|
||||||
convertSuccess (SitemapResponse urls (Approot ar)) = TL.concat
|
convertSuccess (SitemapResponse urls ar) = TL.concat
|
||||||
[ cs "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
|
[ cs "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
|
||||||
, cs "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">"
|
, cs "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">"
|
||||||
, TL.concat $ map helper urls
|
, TL.concat $ map helper urls
|
||||||
@ -69,8 +67,9 @@ instance ConvertSuccess SitemapResponse Text where
|
|||||||
]
|
]
|
||||||
where
|
where
|
||||||
helper (SitemapUrl loc modTime freq pri) = cs $ concat
|
helper (SitemapUrl loc modTime freq pri) = cs $ concat
|
||||||
|
-- FIXME use HTML?
|
||||||
[ "<url><loc>"
|
[ "<url><loc>"
|
||||||
, encodeHtml $ showLoc loc
|
, encodeHtml $ showLocation ar loc
|
||||||
, "</loc><lastmod>"
|
, "</loc><lastmod>"
|
||||||
, formatW3 modTime
|
, formatW3 modTime
|
||||||
, "</lastmod><changefreq>"
|
, "</lastmod><changefreq>"
|
||||||
@ -79,24 +78,20 @@ instance ConvertSuccess SitemapResponse Text where
|
|||||||
, show pri
|
, show pri
|
||||||
, "</priority></url>"
|
, "</priority></url>"
|
||||||
]
|
]
|
||||||
showLoc (AbsLoc s) = s
|
|
||||||
showLoc (RelLoc s) = ar ++ s
|
|
||||||
|
|
||||||
instance HasReps SitemapResponse where
|
instance HasReps SitemapResponse where
|
||||||
reps =
|
reps =
|
||||||
[ (TypeXml, return . cs)
|
[ (TypeXml, return . cs)
|
||||||
]
|
]
|
||||||
|
|
||||||
sitemap :: YesodApproot yesod
|
sitemap :: YesodApproot y => [SitemapUrl] -> Handler y SitemapResponse
|
||||||
=> IO [SitemapUrl]
|
sitemap urls = do
|
||||||
-> Handler yesod SitemapResponse
|
|
||||||
sitemap urls' = do
|
|
||||||
yesod <- getYesod
|
yesod <- getYesod
|
||||||
urls <- liftIO urls'
|
|
||||||
return $ SitemapResponse urls $ approot yesod
|
return $ SitemapResponse urls $ approot yesod
|
||||||
|
|
||||||
robots :: YesodApproot yesod => Handler yesod Plain
|
robots :: YesodApproot yesod => Handler yesod Plain
|
||||||
robots = do
|
robots = do
|
||||||
yesod <- getYesod
|
yesod <- getYesod
|
||||||
return $ plain $ "Sitemap: " ++ unApproot (approot yesod)
|
return $ plain $ "Sitemap: " ++ showLocation
|
||||||
++ "sitemap.xml"
|
(approot yesod)
|
||||||
|
(RelLoc "sitemap.xml")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user