aboutsummaryrefslogtreecommitdiff
path: root/src/Parser/Utils.hs
blob: 852777762535b2a9ffbe698d8ed236cb463d93ae (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
31
32
33
34
35
module Parser.Utils
  ( getTagsBetween
  , getTagAttribute
  , getTagTextAfter
  ) where

import Data.List (find, findIndex)
import Data.Maybe (listToMaybe)
import qualified Data.Text as T

import Text.HTML.TagSoup

getTagsBetween :: String -> String -> [Tag T.Text] -> [Tag T.Text]
getTagsBetween beginSelector endSelector =
  takeWhile (~/= endSelector)
  . drop 1
  . dropWhile (~/= beginSelector)

getTagAttribute :: String -> T.Text -> [Tag T.Text] -> Maybe T.Text
getTagAttribute selector attribute tags =
  find (~== selector) tags >>= maybeTagAttribute attribute

getTagTextAfter :: String -> [Tag T.Text] -> Maybe T.Text
getTagTextAfter selector tags =
  case findIndex (~== selector) tags of
    Just index -> fmap T.strip $ safeGetAt (index + 1) tags >>= maybeTagText
    Nothing -> Nothing

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

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