diff options
author | Joris | 2018-06-17 23:24:47 +0200 |
---|---|---|
committer | Joris | 2018-06-18 11:13:55 +0200 |
commit | 0a4d3c8f12dc5797a919a00b6bcaf759947687cc (patch) | |
tree | bcb89781e22c2314bf0c064ebb37cb7f8a362f5c /src/parser/haskell/Parser/Utils.hs | |
parent | e2a5c7c5c596d057b6fa9c08a8204ce1429cfdc4 (diff) | |
download | ad-listener-0a4d3c8f12dc5797a919a00b6bcaf759947687cc.tar.gz ad-listener-0a4d3c8f12dc5797a919a00b6bcaf759947687cc.tar.bz2 ad-listener-0a4d3c8f12dc5797a919a00b6bcaf759947687cc.zip |
Add ouest france parser
Diffstat (limited to 'src/parser/haskell/Parser/Utils.hs')
-rw-r--r-- | src/parser/haskell/Parser/Utils.hs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/parser/haskell/Parser/Utils.hs b/src/parser/haskell/Parser/Utils.hs new file mode 100644 index 0000000..7c433c6 --- /dev/null +++ b/src/parser/haskell/Parser/Utils.hs @@ -0,0 +1,49 @@ +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 :: String -> [Tag Text] -> [Tag Text] +getTagsBefore selector = takeWhile (~/= selector) + +getTagsAfter :: String -> [Tag Text] -> [Tag Text] +getTagsAfter selector = drop 1 . dropWhile (~/= selector) + +getTagsBetween :: String -> String -> [Tag Text] -> [Tag Text] +getTagsBetween begin end = getTagsBefore end . getTagsAfter begin + +getTagAttributes :: String -> Text -> [Tag Text] -> [Text] +getTagAttributes selector attribute = + catMaybes + . fmap (maybeTagAttribute attribute) + . filter (~== selector) + +getTagAttribute :: String -> Text -> [Tag Text] -> Maybe Text +getTagAttribute selector attribute = + listToMaybe + . getTagAttributes selector 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 _ _ = Nothing + +safeGetAt :: Int -> [a] -> Maybe a +safeGetAt index = listToMaybe . drop index |