From 389d979eb3eaa18beb8a6da9f4a03bdb6acc1722 Mon Sep 17 00:00:00 2001 From: Joris Guyonvarch Date: Sat, 18 Apr 2015 21:59:22 +0200 Subject: Parsing utag_list from a detail page and showing the keys that are given in the configuration file --- src/View/Html/Ad.hs | 38 +++++++++++++++++++++++++++---------- src/View/Plain/Ad.hs | 53 +++++++++++++++++++++++++++++++++++----------------- 2 files changed, 64 insertions(+), 27 deletions(-) (limited to 'src/View') diff --git a/src/View/Html/Ad.hs b/src/View/Html/Ad.hs index fce164e..f3d9ece 100644 --- a/src/View/Html/Ad.hs +++ b/src/View/Html/Ad.hs @@ -7,9 +7,11 @@ module View.Html.Ad import Data.Text (Text) import qualified Data.Text as T import Data.Text.Lazy (toStrict) -import Data.Maybe (fromMaybe) +import Data.Maybe (fromMaybe, catMaybes) import Data.String (fromString) import Data.List (intersperse) +import Data.Map (Map) +import qualified Data.Map as M import Text.Blaze.Html import Text.Blaze.Html5 (Html) @@ -28,20 +30,21 @@ import Model.Detail (Detail) import qualified Model.Detail as D import Model.URL +import Model.Config -renderAds :: [Ad] -> Text -renderAds = toStrict . renderHtml . adsHtml +renderAds :: Config -> [Ad] -> Text +renderAds config = toStrict . renderHtml . (adsHtml config) -adsHtml :: [Ad] -> Html -adsHtml ads = H.div (mapM_ adHtml ads) +adsHtml :: Config -> [Ad] -> Html +adsHtml config ads = H.div (mapM_ (adHtml config) ads) -adHtml :: Ad -> Html -adHtml ad = +adHtml :: Config -> Ad -> Html +adHtml config ad = let resume = A.resume ad detail = A.detail ad in do resumeHtml resume - detailHtml detail + detailHtml config detail resumeHtml :: Resume -> Html resumeHtml resume = @@ -55,8 +58,9 @@ resumeHtml resume = H.h1 (toHtml title) linkHtml url -detailHtml :: Detail -> Html -detailHtml detail = do +detailHtml :: Config -> Detail -> Html +detailHtml config detail = do + propertiesHtml (properties config) (D.properties detail) case D.description detail of Just description -> descriptionHtml description @@ -64,6 +68,20 @@ detailHtml detail = do H.div "" mapM_ imageLinkHtml (D.images detail) +propertiesHtml :: [Text] -> Map Text Text -> Html +propertiesHtml keys properties = + H.dl $ + sequence_ $ catMaybes $ map (propertyHtml properties) keys + +propertyHtml :: Map Text Text -> Text -> Maybe Html +propertyHtml properties key = + fmap + (\value -> do + H.dt (toHtml key) + H.dd (toHtml value) + ) + (M.lookup key properties) + descriptionHtml :: Text -> Html descriptionHtml = H.p . sequence_ . intersperse H.br . fmap toHtml . T.lines diff --git a/src/View/Plain/Ad.hs b/src/View/Plain/Ad.hs index 9dc5289..5120226 100644 --- a/src/View/Plain/Ad.hs +++ b/src/View/Plain/Ad.hs @@ -6,25 +6,28 @@ module View.Plain.Ad ) where import Data.List (intersperse) -import Data.Maybe (fromMaybe) +import Data.Maybe (fromMaybe, catMaybes) +import Data.Map (Map) +import qualified Data.Map as M import Data.Text (Text) import qualified Data.Text as T -import Model.Ad (Ad(..)) +import Model.Ad (Ad) import qualified Model.Ad as Ad -import Model.Resume (Resume(..)) +import Model.Resume (Resume) import qualified Model.Resume as Resume -import Model.Detail (Detail(..)) +import Model.Detail (Detail) import qualified Model.Detail as Detail import Model.URL +import Model.Config -renderConsoleAds :: Text -> [Ad] -> Text -renderConsoleAds time ads = - let (title, message) = renderAds ads +renderConsoleAds :: Config -> Text -> [Ad] -> Text +renderConsoleAds config time ads = + let (title, message) = renderAds config ads titleWithTime = T.concat [ "\n[" @@ -41,10 +44,10 @@ renderConsoleAds time ads = , message ] -renderAds :: [Ad] -> (Text, Text) -renderAds ads = +renderAds :: Config -> [Ad] -> (Text, Text) +renderAds config ads = let titleMessage = renderTitle $ length ads - adsMessage = T.intercalate "\n\n" . map renderAd $ ads + adsMessage = T.intercalate "\n\n" . map (renderAd config) $ ads in (titleMessage, adsMessage) renderTitle :: Int -> Text @@ -60,12 +63,12 @@ renderTitle count = , if count > 1 then "s" else "" ] -renderAd :: Ad -> Text -renderAd ad = +renderAd :: Config -> Ad -> Text +renderAd config ad = T.concat [ renderResume (Ad.resume ad) - , "\n\n" - , renderDetail (Ad.detail ad) + , "\n" + , renderDetail config (Ad.detail ad) ] renderResume :: Resume -> Text @@ -75,13 +78,29 @@ renderResume resume = titleLine = T.concat [Resume.name resume, price] in T.intercalate "\n" [titleLine, Resume.url resume] -renderDetail :: Detail -> Text -renderDetail detail = +renderDetail :: Config -> Detail -> Text +renderDetail config detail = T.concat - [ fromMaybe "−" (Detail.description detail) + [ renderProperties (properties config) (Detail.properties detail) + , fromMaybe "−" (Detail.description detail) , renderURLs "\n\nImages:" (Detail.images detail) ] +renderProperties :: [Text] -> Map Text Text -> Text +renderProperties [] properties = "" +renderProperties keys properties = + T.concat + [ "\n" + , T.concat (catMaybes $ map (renderProperty properties) keys) + , "\n" + ] + +renderProperty :: Map Text Text -> Text -> Maybe Text +renderProperty properties key = + fmap + (\value -> T.concat [key, ": ", value, "\n"]) + (M.lookup key properties) + renderURLs :: Text -> [URL] -> Text renderURLs title [] = "" renderURLs title urls = -- cgit v1.2.3