module Readability.Helper
  ( innerText,
    content',
    elin,
    getElement,
    guarded,
  )
where

import Control.Applicative (Alternative (..))
import Data.Text as T (Text, concat, strip)
import Text.XML
import Text.XML.Cursor

innerText :: Cursor -> Text
innerText c = T.concat (c $// content')

content' :: Cursor -> [Text]
content' = content &| T.strip

getElement :: Node -> Maybe Element
getElement (NodeElement e) = Just e
getElement _ = Nothing

elin :: Element -> [Name] -> Bool
elin e [n] = elementName e == n
elin e ns = elementName e `elem` ns

guarded :: (Alternative f) => (a -> Bool) -> a -> f a
guarded p x = if p x then pure x else empty