diff --git a/examples/example-from-documentation/Main.hs b/examples/example-from-documentation/Main.hs index 57067d6..e82353d 100644 --- a/examples/example-from-documentation/Main.hs +++ b/examples/example-from-documentation/Main.hs @@ -1,28 +1,37 @@ {-# LANGUAGE OverloadedStrings #-} +import Control.Applicative ((<|>)) import Text.HTML.Scalpel -import Control.Applicative - - -exampleHtml :: String -exampleHtml = "\ -\ \ -\
\ -\
\ -\ Sally\ -\
Woo hoo!
\ -\
\ -\
\ -\ Bill\ -\ \ -\
\ -\
\ -\ Susan\ -\
WTF!?!
\ -\
\ -\
\ -\ \ -\" + +htmlString :: String +htmlString = + "\ + \ \ + \
\ + \
\ + \ Sally\ + \
Woo hoo!
\ + \
\ + \
\ + \ Bill\ + \ \ + \
\ + \
\ + \ \ + \" + +main :: IO () +main = do + -- We can either scrape a raw html of any StringLike type (fetched before by other means): + let scrapedCommentsFromString = scrapeStringLike htmlString comments + -- prints: Just [TextComment "Sally" "Woo hoo!",ImageComment "Bill" "http://example.com/cat.gif"] + print scrapedCommentsFromString + + -- or let Scalpel fetch and scrape an HTML page for us for convenience : + scrapedCommentsFromUrl <- scrapeURL "http://example.org/article.html" comments + -- example.org doesn't have the HTML above + -- prints: Just [] + print scrapedCommentsFromUrl type Author = String @@ -31,23 +40,20 @@ data Comment | ImageComment Author URL deriving (Show, Eq) -main :: IO () -main = print $ scrapeStringLike exampleHtml comments - where - comments :: Scraper String [Comment] - comments = chroots ("div" @: [hasClass "container"]) comment - +comments :: Scraper String [Comment] +comments = chroots ("div" @: [hasClass "container"]) comment + where comment :: Scraper String Comment comment = textComment <|> imageComment textComment :: Scraper String Comment textComment = do - author <- text $ "span" @: [hasClass "author"] - commentText <- text $ "div" @: [hasClass "text"] + author <- text $ "span" @: [hasClass "author"] + commentText <- text $ "div" @: [hasClass "text"] return $ TextComment author commentText imageComment :: Scraper String Comment imageComment = do - author <- text $ "span" @: [hasClass "author"] - imageURL <- attr "src" $ "img" @: [hasClass "image"] + author <- text $ "span" @: [hasClass "author"] + imageURL <- attr "src" $ "img" @: [hasClass "image"] return $ ImageComment author imageURL