Skip to content

Commit

Permalink
Align examples folder example with Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
malteneuss authored Jul 20, 2024
1 parent b7e7008 commit f6e57bf
Showing 1 changed file with 38 additions and 32 deletions.
70 changes: 38 additions & 32 deletions examples/example-from-documentation/Main.hs
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
{-# LANGUAGE OverloadedStrings #-}

import Control.Applicative ((<|>))
import Text.HTML.Scalpel
import Control.Applicative


exampleHtml :: String
exampleHtml = "<html>\
\ <body>\
\ <div class='comments'>\
\ <div class='comment container'>\
\ <span class='comment author'>Sally</span>\
\ <div class='comment text'>Woo hoo!</div>\
\ </div>\
\ <div class='comment container'>\
\ <span class='comment author'>Bill</span>\
\ <img class='comment image' src='http://example.com/cat.gif' />\
\ </div>\
\ <div class='comment container'>\
\ <span class='comment author'>Susan</span>\
\ <div class='comment text'>WTF!?!</div>\
\ </div>\
\ </div>\
\ </body>\
\</html>"

htmlString :: String
htmlString =
"<html>\
\ <body>\
\ <div class='comments'>\
\ <div class='comment container'>\
\ <span class='comment author'>Sally</span>\
\ <div class='comment text'>Woo hoo!</div>\
\ </div>\
\ <div class='comment container'>\
\ <span class='comment author'>Bill</span>\
\ <img class='comment image' src='http://example.com/cat.gif' />\
\ </div>\
\ </div>\
\ </body>\
\</html>"

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

Expand All @@ -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

0 comments on commit f6e57bf

Please sign in to comment.