module View where import Album import Config import Util import Network.NewCGI hiding (Html) import Text.XHtml import Control.Exception (Exception, handle) mkTitle :: Album -> Html mkTitle album = stringToHtml ("Album: " ++ albumTitle album) -- -- * Display index page -- -- | Make the HTML for displaying a thumbnail makeThumb :: Image -> Html makeThumb img = thediv ! [theclass "thumb"] << hotlink url [image ! [src thumb]] where url = "?image=" ++ imageName img ++ "&album=" ++ albumName (imageAlbum img) -- FIXME: URL encode thumb = thumbFile img makeIndex :: Album -> [Image] -> Html makeIndex album is = mkHtml hdr bdy where hdr = thetitle (mkTitle album) +++ cssLink (albumStyleSheet album) bdy = h1 << mkTitle album +++ map makeThumb is albumIndexPage :: Album -> CGI CGIResult albumIndexPage album = do is <- liftIO $ getImages album let is' = sortImages imageName is outputHtml $ makeIndex album is' -- -- * Display a single image -- makePage :: Album -> Image -> Html makePage album img = mkHtml hdr bdy where hdr = cssLink (albumStyleSheet album) bdy = hotlink url [image ! [src (smallFile img)]] url = imageFile img imagePage :: Album -> String -> CGI CGIResult imagePage album f = do img <- liftIO $ getImage album f outputHtml $ makePage album img -- -- * General album viewing stuff -- handleError :: Exception -> CGI CGIResult handleError ex = outputHtml $ mkHtml (thetitle << "Error") (stringToHtml (show ex)) runAlbum :: (Album -> CGI CGIResult) -> IO () runAlbum f = runConfig $ \conf -> do ma <- getInput "album" case ma of Nothing -> fail "No album given" Just a -> do album <- liftIO $ getAlbum conf a f album runConfig :: (Config -> CGI CGIResult) -> IO () runConfig f = do conf <- getConfig runCGI (f conf `handleExceptionCGI` handleError)