diff options
Diffstat (limited to 'src/Parser/Utils.hs')
-rw-r--r-- | src/Parser/Utils.hs | 31 |
1 files changed, 18 insertions, 13 deletions
diff --git a/src/Parser/Utils.hs b/src/Parser/Utils.hs index 4864e00..8527777 100644 --- a/src/Parser/Utils.hs +++ b/src/Parser/Utils.hs @@ -1,30 +1,35 @@ module Parser.Utils - ( getTagAttribute - , getTagText + ( getTagsBetween + , getTagAttribute + , getTagTextAfter ) where import Data.List (find, findIndex) import Data.Maybe (listToMaybe) +import qualified Data.Text as T import Text.HTML.TagSoup -getTagAttribute :: String -> String -> [Tag String] -> Maybe String -getTagAttribute selector attribute item = - find (~== selector) item >>= maybeTagAttribute attribute +getTagsBetween :: String -> String -> [Tag T.Text] -> [Tag T.Text] +getTagsBetween beginSelector endSelector = + takeWhile (~/= endSelector) + . drop 1 + . dropWhile (~/= beginSelector) -getTagText :: String -> [Tag String] -> Maybe String -getTagText selector item = - case findIndex (~== selector) item of - Just index -> fmap trim $ safeGetAt (index + 1) item >>= maybeTagText +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 :: String -> Tag String -> Maybe String +maybeTagAttribute :: T.Text -> Tag T.Text -> Maybe T.Text 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 |