diff options
author | Joris | 2018-06-19 22:49:16 +0200 |
---|---|---|
committer | Joris | 2018-06-19 22:49:16 +0200 |
commit | 149a0470b73781022e584aaeaa7ce871d6f4173b (patch) | |
tree | c1cc762e105ae19e7f3daaa3e9279a467dbaa3dc /src/lib/haskell/Parser/Utils.hs | |
parent | 5d921c9a2b0a7a8f1a1bb5642cbefa516cbbe4cc (diff) | |
download | ad-listener-149a0470b73781022e584aaeaa7ce871d6f4173b.tar.gz ad-listener-149a0470b73781022e584aaeaa7ce871d6f4173b.tar.bz2 ad-listener-149a0470b73781022e584aaeaa7ce871d6f4173b.zip |
Add automatic tests on remote pages
Diffstat (limited to 'src/lib/haskell/Parser/Utils.hs')
-rw-r--r-- | src/lib/haskell/Parser/Utils.hs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/lib/haskell/Parser/Utils.hs b/src/lib/haskell/Parser/Utils.hs new file mode 100644 index 0000000..4768327 --- /dev/null +++ b/src/lib/haskell/Parser/Utils.hs @@ -0,0 +1,48 @@ +module Parser.Utils + ( getTagsBefore + , getTagsAfter + , getTagsBetween + , getTagAttributes + , getTagAttribute + , getTagTextAfter + ) where + +import Data.List (find, findIndex) +import Data.Maybe (catMaybes, listToMaybe) +import Data.Text (Text) +import qualified Data.Text as T +import Text.HTML.TagSoup + +getTagsBefore :: Text -> [Tag Text] -> [Tag Text] +getTagsBefore selector = takeWhile (~/= (T.unpack selector)) + +getTagsAfter :: Text -> [Tag Text] -> [Tag Text] +getTagsAfter selector = drop 1 . dropWhile (~/= (T.unpack selector)) + +getTagsBetween :: Text -> Text -> [Tag Text] -> [Tag Text] +getTagsBetween begin end = getTagsBefore end . getTagsAfter begin + +getTagAttributes :: Text -> Text -> [Tag Text] -> [Text] +getTagAttributes selector attribute = + catMaybes + . fmap (maybeTagAttribute attribute) + . filter (~== (T.unpack selector)) + +getTagAttribute :: Text -> Text -> [Tag Text] -> Maybe Text +getTagAttribute selector attribute = + listToMaybe + . getTagAttributes selector attribute + +getTagTextAfter :: Text -> [Tag Text] -> Maybe Text +getTagTextAfter selector tags = + case findIndex (~== (T.unpack 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 _ _ = Nothing + +safeGetAt :: Int -> [a] -> Maybe a +safeGetAt index = listToMaybe . drop index |