aboutsummaryrefslogtreecommitdiff
path: root/src/Parser/Utils.hs
blob: 4864e00f077870e37f67eb03655ca0b4eec8a574 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module Parser.Utils
  ( getTagAttribute
  , getTagText
  ) where

import Data.List (find, findIndex)
import Data.Maybe (listToMaybe)

import Text.HTML.TagSoup

getTagAttribute :: String -> String -> [Tag String] -> Maybe String
getTagAttribute selector attribute item =
  find (~== selector) item >>= maybeTagAttribute attribute

getTagText :: String -> [Tag String] -> Maybe String
getTagText selector item =
  case findIndex (~== selector) item of
    Just index -> fmap trim $ safeGetAt (index + 1) item >>= maybeTagText
    Nothing -> Nothing

maybeTagAttribute :: String -> Tag String -> Maybe String
maybeTagAttribute name (TagOpen _ xs) =
  fmap snd . find (\(x, _) -> x == name) $ xs
maybeTagAttribute attribute _ = Nothing

trim :: String -> String
trim = unwords . words

safeGetAt :: Int -> [a] -> Maybe a
safeGetAt index = listToMaybe . drop index