module Parser.Utils ( getTagsBetween , getTagAttribute , getTagTextAfter ) where import Data.List (find, findIndex) import Data.Maybe (listToMaybe) import Data.Text (Text) import qualified Data.Text as T import Text.HTML.TagSoup getTagsBetween :: String -> String -> [Tag Text] -> [Tag Text] getTagsBetween beginSelector endSelector = takeWhile (~/= endSelector) . drop 1 . dropWhile (~/= beginSelector) getTagAttribute :: String -> Text -> [Tag Text] -> Maybe Text getTagAttribute selector attribute tags = find (~== selector) tags >>= maybeTagAttribute attribute getTagTextAfter :: String -> [Tag Text] -> Maybe Text getTagTextAfter selector tags = case findIndex (~== selector) tags of Just index -> fmap T.strip $ safeGetAt (index + 1) tags >>= maybeTagText Nothing -> Nothing maybeTagAttribute :: Text -> Tag Text -> Maybe 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