{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TupleSections #-}

module Scrappy.Elem.SimpleElemParser where

import Scrappy.Elem.Types

import Scrappy.Elem.ElemHeadParse (parseOpeningTag, parseOpeningTagWhere)
import Scrappy.Links (LastUrl)

import Scrappy.Types -- for witherable instance

import Control.Monad (when)  
import Control.Applicative (Alternative, liftA2, some, (<|>))
--import Witherable (mapMaybe)
--import Text.Megaparsec as MParsec (eitherP, some, manyTill)
import Text.Parsec (ParsecT, Stream, string, try, parserZero, anyChar, char, optional, anyToken, parserFail
                   , many, space, manyTill)
import Text.URI (URI, render)
import Data.Text (Text, unpack)
import Data.Map (Map, toList)
import Data.Maybe (fromMaybe)

import Control.Monad.IO.Class

-- TODO(galen): antiElemParser --- inner matches must be 0 ... maybe doesnt match any parameter 


eitherP :: Alternative m => m a -> m b -> m (Either a b)
eitherP :: forall (m :: * -> *) a b.
Alternative m =>
m a -> m b -> m (Either a b)
eitherP m a
a m b
b = (a -> Either a b
forall a b. a -> Either a b
Left (a -> Either a b) -> m a -> m (Either a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a
a) m (Either a b) -> m (Either a b) -> m (Either a b)
forall a. m a -> m a -> m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (b -> Either a b
forall a b. b -> Either a b
Right (b -> Either a b) -> m b -> m (Either a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m b
b)

manyTill_ :: ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m ([a], end)
manyTill_ :: forall s u (m :: * -> *) a end.
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m ([a], end)
manyTill_ ParsecT s u m a
p ParsecT s u m end
end = ParsecT s u m ([a], end)
go
  where
    go :: ParsecT s u m ([a], end)
go = (([],) (end -> ([a], end))
-> ParsecT s u m end -> ParsecT s u m ([a], end)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT s u m end
end) ParsecT s u m ([a], end)
-> ParsecT s u m ([a], end) -> ParsecT s u m ([a], end)
forall a. ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (a -> ([a], end) -> ([a], end))
-> ParsecT s u m a
-> ParsecT s u m ([a], end)
-> ParsecT s u m ([a], end)
forall a b c.
(a -> b -> c)
-> ParsecT s u m a -> ParsecT s u m b -> ParsecT s u m c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (\a
x ([a]
xs, end
y) -> (a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
xs, end
y)) ParsecT s u m a
p ParsecT s u m ([a], end)
go


-- | Try to cut out Megaparsec for now - get direct export from Control.Applicative

-- | Note: could make class HtmlP where { el :: a -> Elem, attrs :: a -> Attrs, innerText :: a -> Text } 

-- | A use-case/problem is popping up as I code:
 -- if elem 'a' contains elem 'a'
    -- then do what?
    -- 1) Restrict to identifying in parent only if not in some inner same element
    -- 2) Get all in parent element regardless
    -- 3) Consider being inside of same element a fail -> then get inner-same element
      -- like 1) but seeks to carry minimal data around it / more honed in



-- | Simplest interface to building element patterns 
el :: Stream s m Char => Elem -> [(String, String)] -> ParsecT s u m (Elem' String)
el :: forall s (m :: * -> *) u.
Stream s m Char =>
Elem -> [(Elem, Elem)] -> ParsecT s u m (Elem' Elem)
el Elem
element [(Elem, Elem)]
attrss = Maybe [Elem]
-> Maybe (ParsecT s u m Elem)
-> [(Elem, Maybe Elem)]
-> ParsecT s u m (Elem' Elem)
forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
Maybe [Elem]
-> Maybe (ParsecT s u m a)
-> [(Elem, Maybe Elem)]
-> ParsecT s u m (Elem' a)
elemParser ([Elem] -> Maybe [Elem]
forall a. a -> Maybe a
Just (Elem
elementElem -> [Elem] -> [Elem]
forall a. a -> [a] -> [a]
:[])) Maybe (ParsecT s u m Elem)
forall a. Maybe a
Nothing ((((Elem, Elem) -> (Elem, Maybe Elem))
-> [(Elem, Elem)] -> [(Elem, Maybe Elem)]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (((Elem, Elem) -> (Elem, Maybe Elem))
 -> [(Elem, Elem)] -> [(Elem, Maybe Elem)])
-> ((Elem -> Maybe Elem) -> (Elem, Elem) -> (Elem, Maybe Elem))
-> (Elem -> Maybe Elem)
-> [(Elem, Elem)]
-> [(Elem, Maybe Elem)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Elem -> Maybe Elem) -> (Elem, Elem) -> (Elem, Maybe Elem)
forall a b. (a -> b) -> (Elem, a) -> (Elem, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) Elem -> Maybe Elem
forall a. a -> Maybe a
Just [(Elem, Elem)]
attrss)



-- | Generic interface for building Html element patterns where we do not differentiate based on whats inside
-- | for control of allowable inner html patterns, see ChainHTML and/or TreeElemParser  
elemParser :: (ShowHTML a, Stream s m Char) =>
              Maybe [Elem]
           -> Maybe (ParsecT s u m a)
           -> [(String, Maybe String)]
           -> ParsecT s u m (Elem' a)
elemParser :: forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
Maybe [Elem]
-> Maybe (ParsecT s u m a)
-> [(Elem, Maybe Elem)]
-> ParsecT s u m (Elem' a)
elemParser Maybe [Elem]
elemList Maybe (ParsecT s u m a)
innerSpec [(Elem, Maybe Elem)]
attrs = do
  -- liftIO $ print "hey"
  (Elem
elem', Attrs
attrs') <- Maybe [Elem] -> [(Elem, Maybe Elem)] -> ParsecT s u m (Elem, Attrs)
forall s (m :: * -> *) u.
Stream s m Char =>
Maybe [Elem] -> [(Elem, Maybe Elem)] -> ParsecT s u m (Elem, Attrs)
parseOpeningTag Maybe [Elem]
elemList [(Elem, Maybe Elem)]
attrs 
  -- we should now read the elem' to see if in list of self-closing tags
  -- TODO(galen): What about when the self closing tag actually doesnt?
  case Elem -> [Elem] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem Elem
elem' [Elem]
selfClosing of
    Bool
True -> do
      (ParsecT s u m Elem -> ParsecT s u m Elem
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (Elem -> ParsecT s u m Elem
forall s (m :: * -> *) u.
Stream s m Char =>
Elem -> ParsecT s u m Elem
string Elem
">") ParsecT s u m Elem -> ParsecT s u m Elem -> ParsecT s u m Elem
forall a. ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Elem -> ParsecT s u m Elem
forall s (m :: * -> *) u.
Stream s m Char =>
Elem -> ParsecT s u m Elem
string Elem
"/>")
      case Maybe (ParsecT s u m a)
innerSpec of
        Maybe (ParsecT s u m a)
Nothing -> Elem' a -> ParsecT s u m (Elem' a)
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Elem' a -> ParsecT s u m (Elem' a))
-> Elem' a -> ParsecT s u m (Elem' a)
forall a b. (a -> b) -> a -> b
$ Elem -> Attrs -> [a] -> Elem -> Elem' a
forall a. Elem -> Attrs -> [a] -> Elem -> Elem' a
Elem' Elem
elem' Attrs
attrs' [a]
forall a. Monoid a => a
mempty Elem
forall a. Monoid a => a
mempty 
        Just ParsecT s u m a
_ -> ParsecT s u m (Elem' a)
forall s u (m :: * -> *) a. ParsecT s u m a
parserZero 
    Bool
False -> do
      (Elem
asString, [a]
matches) <- ([HTMLMatcher Elem' a] -> (Elem, [a]))
-> ParsecT s u m [HTMLMatcher Elem' a] -> ParsecT s u m (Elem, [a])
forall a b. (a -> b) -> ParsecT s u m a -> ParsecT s u m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((HTMLMatcher Elem' a -> (Elem, [a]) -> (Elem, [a]))
-> (Elem, [a]) -> [HTMLMatcher Elem' a] -> (Elem, [a])
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr HTMLMatcher Elem' a -> (Elem, [a]) -> (Elem, [a])
forall (e :: * -> *) a.
(ShowHTML (e a), ShowHTML a, ElementRep e) =>
HTMLMatcher e a -> (Elem, [a]) -> (Elem, [a])
foldFuncTup (Elem, [a])
forall a. Monoid a => a
mempty)  -- this cant be where we do "/>" if we parse ">" in parseOpeningTag
        (ParsecT s u m [HTMLMatcher Elem' a] -> ParsecT s u m (Elem, [a]))
-> ParsecT s u m [HTMLMatcher Elem' a] -> ParsecT s u m (Elem, [a])
forall a b. (a -> b) -> a -> b
$ (ParsecT s u m Elem -> ParsecT s u m Elem
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (Elem -> ParsecT s u m Elem
forall s (m :: * -> *) u.
Stream s m Char =>
Elem -> ParsecT s u m Elem
string Elem
"/>") ParsecT s u m Elem
-> ParsecT s u m [HTMLMatcher Elem' a]
-> ParsecT s u m [HTMLMatcher Elem' a]
forall a b. ParsecT s u m a -> ParsecT s u m b -> ParsecT s u m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [HTMLMatcher Elem' a] -> ParsecT s u m [HTMLMatcher Elem' a]
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return [])
        ParsecT s u m [HTMLMatcher Elem' a]
-> ParsecT s u m [HTMLMatcher Elem' a]
-> ParsecT s u m [HTMLMatcher Elem' a]
forall a. ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (ParsecT s u m [HTMLMatcher Elem' a]
-> ParsecT s u m [HTMLMatcher Elem' a]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT s u m [HTMLMatcher Elem' a]
 -> ParsecT s u m [HTMLMatcher Elem' a])
-> ParsecT s u m [HTMLMatcher Elem' a]
-> ParsecT s u m [HTMLMatcher Elem' a]
forall a b. (a -> b) -> a -> b
$ Elem
-> Maybe (ParsecT s u m a) -> ParsecT s u m [HTMLMatcher Elem' a]
forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
Elem
-> Maybe (ParsecT s u m a) -> ParsecT s u m [HTMLMatcher Elem' a]
innerElemParser Elem
elem' Maybe (ParsecT s u m a)
innerSpec) -- need to be sure that we have exhausted looking for an end tag, then we can do the following safely
        ParsecT s u m [HTMLMatcher Elem' a]
-> ParsecT s u m [HTMLMatcher Elem' a]
-> ParsecT s u m [HTMLMatcher Elem' a]
forall a. ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Maybe (ParsecT s u m a) -> ParsecT s u m [HTMLMatcher Elem' a]
forall a s (m :: * -> *) u (e :: * -> *).
(ShowHTML a, Stream s m Char) =>
Maybe (ParsecT s u m a) -> ParsecT s u m [HTMLMatcher e a]
selfClosingTextful Maybe (ParsecT s u m a)
innerSpec)
      Elem' a -> ParsecT s u m (Elem' a)
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Elem' a -> ParsecT s u m (Elem' a))
-> Elem' a -> ParsecT s u m (Elem' a)
forall a b. (a -> b) -> a -> b
$ Elem -> Attrs -> [a] -> Elem -> Elem' a
forall a. Elem -> Attrs -> [a] -> Elem -> Elem' a
Elem' Elem
elem' Attrs
attrs' [a]
matches Elem
asString


-- | Generic interface for building Html element patterns where we do not differentiate based on whats inside
-- | for control of allowable inner html patterns, see ChainHTML and/or TreeElemParser  
elemParserWhere :: (ShowHTML a, Stream s m Char) =>
                   Maybe [Elem]
                -> Maybe (ParsecT s u m a)
                -> String -> (String -> Bool) -- GOAL: -> [(String, String -> Bool)]
                -- ^ An attr and a predicate
                -> ParsecT s u m (Elem' a)
elemParserWhere :: forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
Maybe [Elem]
-> Maybe (ParsecT s u m a)
-> Elem
-> (Elem -> Bool)
-> ParsecT s u m (Elem' a)
elemParserWhere Maybe [Elem]
elemList Maybe (ParsecT s u m a)
innerSpec Elem
attr Elem -> Bool
pred = do
  (Elem
elem', Attrs
attrs') <- Maybe [Elem]
-> Elem -> (Elem -> Bool) -> ParsecT s u m (Elem, Attrs)
forall s (m :: * -> *) u.
Stream s m Char =>
Maybe [Elem]
-> Elem -> (Elem -> Bool) -> ParsecT s u m (Elem, Attrs)
parseOpeningTagWhere Maybe [Elem]
elemList Elem
attr Elem -> Bool
pred
  -- we should now read the elem' to see if in list of self-closing tags
  case Elem -> [Elem] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem Elem
elem' [Elem]
selfClosing of
    Bool
True -> do
      (ParsecT s u m Elem -> ParsecT s u m Elem
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (Elem -> ParsecT s u m Elem
forall s (m :: * -> *) u.
Stream s m Char =>
Elem -> ParsecT s u m Elem
string Elem
">") ParsecT s u m Elem -> ParsecT s u m Elem -> ParsecT s u m Elem
forall a. ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Elem -> ParsecT s u m Elem
forall s (m :: * -> *) u.
Stream s m Char =>
Elem -> ParsecT s u m Elem
string Elem
"/>")
      case Maybe (ParsecT s u m a)
innerSpec of
        Maybe (ParsecT s u m a)
Nothing -> Elem' a -> ParsecT s u m (Elem' a)
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Elem' a -> ParsecT s u m (Elem' a))
-> Elem' a -> ParsecT s u m (Elem' a)
forall a b. (a -> b) -> a -> b
$ Elem -> Attrs -> [a] -> Elem -> Elem' a
forall a. Elem -> Attrs -> [a] -> Elem -> Elem' a
Elem' Elem
elem' Attrs
attrs' [a]
forall a. Monoid a => a
mempty Elem
forall a. Monoid a => a
mempty 
        Just ParsecT s u m a
_ -> ParsecT s u m (Elem' a)
forall s u (m :: * -> *) a. ParsecT s u m a
parserZero 
    Bool
False -> do
      (Elem
asString, [a]
matches) <- ([HTMLMatcher Elem' a] -> (Elem, [a]))
-> ParsecT s u m [HTMLMatcher Elem' a] -> ParsecT s u m (Elem, [a])
forall a b. (a -> b) -> ParsecT s u m a -> ParsecT s u m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((HTMLMatcher Elem' a -> (Elem, [a]) -> (Elem, [a]))
-> (Elem, [a]) -> [HTMLMatcher Elem' a] -> (Elem, [a])
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr HTMLMatcher Elem' a -> (Elem, [a]) -> (Elem, [a])
forall (e :: * -> *) a.
(ShowHTML (e a), ShowHTML a, ElementRep e) =>
HTMLMatcher e a -> (Elem, [a]) -> (Elem, [a])
foldFuncTup (Elem, [a])
forall a. Monoid a => a
mempty)  -- this cant be where we do "/>" if we parse ">" in parseOpeningTag
        (ParsecT s u m [HTMLMatcher Elem' a] -> ParsecT s u m (Elem, [a]))
-> ParsecT s u m [HTMLMatcher Elem' a] -> ParsecT s u m (Elem, [a])
forall a b. (a -> b) -> a -> b
$ (ParsecT s u m Elem -> ParsecT s u m Elem
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (Elem -> ParsecT s u m Elem
forall s (m :: * -> *) u.
Stream s m Char =>
Elem -> ParsecT s u m Elem
string Elem
"/>") ParsecT s u m Elem
-> ParsecT s u m [HTMLMatcher Elem' a]
-> ParsecT s u m [HTMLMatcher Elem' a]
forall a b. ParsecT s u m a -> ParsecT s u m b -> ParsecT s u m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [HTMLMatcher Elem' a] -> ParsecT s u m [HTMLMatcher Elem' a]
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return [])
        ParsecT s u m [HTMLMatcher Elem' a]
-> ParsecT s u m [HTMLMatcher Elem' a]
-> ParsecT s u m [HTMLMatcher Elem' a]
forall a. ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (ParsecT s u m [HTMLMatcher Elem' a]
-> ParsecT s u m [HTMLMatcher Elem' a]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT s u m [HTMLMatcher Elem' a]
 -> ParsecT s u m [HTMLMatcher Elem' a])
-> ParsecT s u m [HTMLMatcher Elem' a]
-> ParsecT s u m [HTMLMatcher Elem' a]
forall a b. (a -> b) -> a -> b
$ Elem
-> Maybe (ParsecT s u m a) -> ParsecT s u m [HTMLMatcher Elem' a]
forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
Elem
-> Maybe (ParsecT s u m a) -> ParsecT s u m [HTMLMatcher Elem' a]
innerElemParser Elem
elem' Maybe (ParsecT s u m a)
innerSpec) -- need to be sure that we have exhausted looking for an end tag, then we can do the following safely
        ParsecT s u m [HTMLMatcher Elem' a]
-> ParsecT s u m [HTMLMatcher Elem' a]
-> ParsecT s u m [HTMLMatcher Elem' a]
forall a. ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Maybe (ParsecT s u m a) -> ParsecT s u m [HTMLMatcher Elem' a]
forall a s (m :: * -> *) u (e :: * -> *).
(ShowHTML a, Stream s m Char) =>
Maybe (ParsecT s u m a) -> ParsecT s u m [HTMLMatcher e a]
selfClosingTextful Maybe (ParsecT s u m a)
innerSpec)
      Elem' a -> ParsecT s u m (Elem' a)
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Elem' a -> ParsecT s u m (Elem' a))
-> Elem' a -> ParsecT s u m (Elem' a)
forall a b. (a -> b) -> a -> b
$ Elem -> Attrs -> [a] -> Elem -> Elem' a
forall a. Elem -> Attrs -> [a] -> Elem -> Elem' a
Elem' Elem
elem' Attrs
attrs' [a]
matches Elem
asString




clickableHref :: Stream s m Char => Bool -> LastUrl -> ParsecT s u m Clickable
clickableHref :: forall s (m :: * -> *) u.
Stream s m Char =>
Bool -> LastUrl -> ParsecT s u m Clickable
clickableHref Bool
booly LastUrl
cUrl = do
  (Elem, Attrs)
elA <- Maybe [Elem] -> [(Elem, Maybe Elem)] -> ParsecT s u m (Elem, Attrs)
forall s (m :: * -> *) u.
Stream s m Char =>
Maybe [Elem] -> [(Elem, Maybe Elem)] -> ParsecT s u m (Elem, Attrs)
parseOpeningTag Maybe [Elem]
forall a. Maybe a
Nothing [(Elem
"href", Maybe Elem
forall a. Maybe a
Nothing)]
  LastUrl
href <- (Attrs -> Maybe LastUrl)
-> ParsecT s u m Attrs -> ParsecT s u m LastUrl
forall a b s u (m :: * -> *).
(a -> Maybe b) -> ParsecT s u m a -> ParsecT s u m b
mapMaybe (Bool -> LastUrl -> Attrs -> Maybe LastUrl
getHrefAttrs Bool
booly LastUrl
cUrl) (Attrs -> ParsecT s u m Attrs
forall a. a -> ParsecT s u m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Attrs -> ParsecT s u m Attrs) -> Attrs -> ParsecT s u m Attrs
forall a b. (a -> b) -> a -> b
$ (Elem, Attrs) -> Attrs
forall a b. (a, b) -> b
snd (Elem, Attrs)
elA) 
  Clickable -> ParsecT s u m Clickable
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Clickable -> ParsecT s u m Clickable)
-> Clickable -> ParsecT s u m Clickable
forall a b. (a -> b) -> a -> b
$ (Elem, Attrs) -> LastUrl -> Clickable
Clickable (Elem, Attrs)
elA LastUrl
href


-- eg clickable' (string "download") 
clickableHref' :: (Stream s m Char, ShowHTML a) =>
                  ParsecT s u m a
               -> Bool
               -> LastUrl
               -> ParsecT s u m Clickable 
clickableHref' :: forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
ParsecT s u m a -> Bool -> LastUrl -> ParsecT s u m Clickable
clickableHref' ParsecT s u m a
innerPat Bool
booly LastUrl
cUrl = do
  Elem' a
e <- Maybe [Elem]
-> Maybe (ParsecT s u m a)
-> [(Elem, Maybe Elem)]
-> ParsecT s u m (Elem' a)
forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
Maybe [Elem]
-> Maybe (ParsecT s u m a)
-> [(Elem, Maybe Elem)]
-> ParsecT s u m (Elem' a)
elemParser Maybe [Elem]
forall a. Maybe a
Nothing (ParsecT s u m a -> Maybe (ParsecT s u m a)
forall a. a -> Maybe a
Just (ParsecT s u m a -> Maybe (ParsecT s u m a))
-> ParsecT s u m a -> Maybe (ParsecT s u m a)
forall a b. (a -> b) -> a -> b
$ ParsecT s u m a
innerPat) [(Elem
"href", Maybe Elem
forall a. Maybe a
Nothing)]
  LastUrl
href <- (Elem' a -> Maybe LastUrl)
-> ParsecT s u m (Elem' a) -> ParsecT s u m LastUrl
forall a b s u (m :: * -> *).
(a -> Maybe b) -> ParsecT s u m a -> ParsecT s u m b
mapMaybe (Bool -> LastUrl -> Elem' a -> Maybe LastUrl
forall (e :: * -> *) a.
ElementRep e =>
Bool -> LastUrl -> e a -> Maybe LastUrl
getHrefEl Bool
booly LastUrl
cUrl) (Elem' a -> ParsecT s u m (Elem' a)
forall a. a -> ParsecT s u m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Elem' a
e)
  Clickable -> ParsecT s u m Clickable
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Clickable -> ParsecT s u m Clickable)
-> Clickable -> ParsecT s u m Clickable
forall a b. (a -> b) -> a -> b
$ (Elem, Attrs) -> LastUrl -> Clickable
Clickable (Elem' a -> Elem
forall b. Elem' b -> Elem
forall (a :: * -> *) b. ElementRep a => a b -> Elem
elTag Elem' a
e, Elem' a -> Attrs
forall b. Elem' b -> Attrs
forall (a :: * -> *) b. ElementRep a => a b -> Attrs
attrs Elem' a
e) LastUrl
href

  
-- instance Monad Elem' where 

sameElTag :: (ShowHTML a, Stream s m Char) => Elem -> Maybe (ParsecT s u m a) -> ParsecT s u m (Elem' a)
sameElTag :: forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
Elem -> Maybe (ParsecT s u m a) -> ParsecT s u m (Elem' a)
sameElTag Elem
elem Maybe (ParsecT s u m a)
parser = Maybe [Elem]
-> Maybe (ParsecT s u m a)
-> [(Elem, Maybe Elem)]
-> ParsecT s u m (Elem' a)
forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
Maybe [Elem]
-> Maybe (ParsecT s u m a)
-> [(Elem, Maybe Elem)]
-> ParsecT s u m (Elem' a)
elemParser ([Elem] -> Maybe [Elem]
forall a. a -> Maybe a
Just [Elem
elem]) Maybe (ParsecT s u m a)
parser []
  
  -- innerMatches el 
  -- return $ (elemToStr el, Match $ innerMatches el)  -- allowed to return a String or Match a

-- future concern for foldFuncMatchlist where Elem ~~ [] ; both of kind * -> *
matchesInSameElTag :: (ShowHTML a, Stream s m Char) => Elem -> Maybe (ParsecT s u m a) -> ParsecT s u m [a]
matchesInSameElTag :: forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
Elem -> Maybe (ParsecT s u m a) -> ParsecT s u m [a]
matchesInSameElTag Elem
elem Maybe (ParsecT s u m a)
parser = do
  Elem' a
el <- Maybe [Elem]
-> Maybe (ParsecT s u m a)
-> [(Elem, Maybe Elem)]
-> ParsecT s u m (Elem' a)
forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
Maybe [Elem]
-> Maybe (ParsecT s u m a)
-> [(Elem, Maybe Elem)]
-> ParsecT s u m (Elem' a)
elemParser ([Elem] -> Maybe [Elem]
forall a. a -> Maybe a
Just [Elem
elem]) Maybe (ParsecT s u m a)
parser [] 
  [a] -> ParsecT s u m [a]
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return ([a] -> ParsecT s u m [a]) -> [a] -> ParsecT s u m [a]
forall a b. (a -> b) -> a -> b
$ (Elem' a -> [a]
forall b. Elem' b -> [b]
forall (a :: * -> *) b. ElementRep a => a b -> [b]
matches' Elem' a
el)  -- allowed to return a String or Match a

-- | Might be worth it to do again with findNextMatch func
  -- this would open up ability to return multiple matches inside of a given element
  -- would need to retain ability to handle 3Cases{ self-closing(2 { /> or > .. eof}) | match | no match
  -- in case of no match { self-closing || simply, no match } -> needs to throw parserZero 

-- | findNextMatch already handles case of Eof
  -- would be re-definition of baseParser in `let`


-- the below class functions give rise to concept:
-- findInRecursive :: IsHTMLRep a => (Parser1, Parser2, Parser3, ...) -> Element a

-- where Element a is meant to represent anything that fits IsHTMLRep and has some number of summarized data
-- from the html

-- and is meant to work with a MessyTree ~ String in a context-enabled manner

-- this could in theory be as abstract as possible

-- | Case 1:

-- (ParserLevel1 a, ParserLevel2 b) ~ up to 2 levels specified ~ up to 2 patterns
  --  Given find functions, this can start at any arbitrary next index

-- | Case 2:

-- [ParserSomeLevel] ~ up to n levels specified, 1 possible pattern'

-- | Case 3: The most general

-- [Show a => forall a. a] ~ up to n levels specified, n possible patterns 


-- class MonoidFold a where
  -- foldMon :: [a] -> b
  -- newEmpty :: b

-- parseHtmlMatcher -> foldtoITR + opening tag -> elem 


-- | Maybe elemParser can be abstracted to be a class function

-- | Does this work with parser meant to take up whole inner?
  -- I suppose it would but this would allow other stuff
  -- that case is handled by treeElemParserSpecific

-- {-# DEPRECATED elemParser "needs logic for anyTagInner for text-ful, self-closing tags"  #-}
-- elemParser :: (InnerHTMLRep Elem' InnerTextResult a, ShowHTML a, Stream s m Char) =>
--               Maybe [Elem]
--            -> Maybe (ParsecT s u m a)
--            -> [(String, Maybe String)]
--            -> ParsecT s u m (Elem' a)
-- elemParser elemList innerSpec attrs = do
--   (elem', attrs') <- parseOpeningTag elemList attrs
--   let
--     parser = char '>'
--              >> manyTill (Match <$> (fromMaybe parserZero innerSpec)
--                           <|> Element <$> sameElTag elem' innerSpec
--                           <|> ((IText . (:[])) <$> anyChar)) (endTag elem')
                          

--   innerH <- (try (string "/>") >> return []) <|> (try parser) <|> (selfClosingTextful innerSpec)
--   let itr = foldHtmlMatcher innerH 
--   case length $ matches itr of
--     0 ->
--       case innerSpec of
--         Nothing -> return $ Elem' elem' attrs' (matches itr) (innerText itr)
--         _ -> parserZero
--     _ -> return $ Elem' elem' attrs' (matches itr) (innerText itr)

-- if innerSpec == Nothing
      -- then return $ Elem' elem' attrs' (matches itr) (innerText itr)
      -- else parserZero
  
    {- endTag is currently in TreeElemParser -}
    
-- selfClosingTextful :: ParsecT s u m a
-- selfClosingTextful = do
--   -

-- foldHtmlMatcherToTrup :: [HTMLMatcher e a] -> ([a], 
 
-- {-# DEPRECATED elemParser "needs logic for anyTagInner for text-ful, self-closing tags"  #-}

-- elemParser :: (ShowHTML a, Stream s m Char) =>
--               Maybe [Elem]
--            -> Maybe (ParsecT s u m a)
--            -> [(String, Maybe String)]
--            -> ParsecT s u m (Elem' a)
-- elemParser elemList innerSpec attrs = do
--   let
--     parser eTag = char '>'
--                    >> manyTill (Match <$> (fromMaybe parserZero innerSpec)
--                                 <|> Element <$> sameElTag eTag innerSpec
--                                 <|> ((IText . (:[])) <$> anyChar)) (endTag eTag)
--     required = case innerSpec of
--                  { Nothing -> 0; _ -> 1 }
--     enoughMatches e a (asString, matches) = 
--       if required > (length matches)
--       then return $ Elem' e a matches asString
--       else parserZero
                    
--   (elem', attrs') <- parseOpeningTag elemList attrs
--   innerH <- fmap (foldr foldFuncTup mempty) 
--             $ (try (string "/>") >> return [])
--             <|> (try $ parser elem')
--             <|> (selfClosingTextful innerSpec)
--   enoughMatches elem' attrs' innerH

selfClosing :: [String]
selfClosing :: [Elem]
selfClosing = [Elem
"area", Elem
"base", Elem
"br", Elem
"col", Elem
"embed", Elem
"hr", Elem
"img", Elem
"input", Elem
"link", Elem
"meta", Elem
"param", Elem
"source", Elem
"track", Elem
"wbr"]

elSelfC :: Stream s m Char => Maybe [Elem] -> [(String, Maybe String)] -> ParsecT s u m (Elem' a)
elSelfC :: forall s (m :: * -> *) u a.
Stream s m Char =>
Maybe [Elem] -> [(Elem, Maybe Elem)] -> ParsecT s u m (Elem' a)
elSelfC Maybe [Elem]
elemOpts [(Elem, Maybe Elem)]
attrsSubset = do
  (Elem
tag, Attrs
attrs) <- Maybe [Elem] -> [(Elem, Maybe Elem)] -> ParsecT s u m (Elem, Attrs)
forall s (m :: * -> *) u.
Stream s m Char =>
Maybe [Elem] -> [(Elem, Maybe Elem)] -> ParsecT s u m (Elem, Attrs)
parseOpeningTag Maybe [Elem]
elemOpts [(Elem, Maybe Elem)]
attrsSubset
  Elem' a -> ParsecT s u m (Elem' a)
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Elem' a -> ParsecT s u m (Elem' a))
-> Elem' a -> ParsecT s u m (Elem' a)
forall a b. (a -> b) -> a -> b
$ Elem -> Attrs -> [a] -> Elem -> Elem' a
forall a. Elem -> Attrs -> [a] -> Elem -> Elem' a
Elem' Elem
tag Attrs
attrs [a]
forall a. Monoid a => a
mempty Elem
forall a. Monoid a => a
mempty 

elSelfClosing :: Stream s m Char => Maybe [Elem] -> Maybe (ParsecT s u m a) -> [(String, Maybe String)] -> ParsecT s u m (Elem' a)
elSelfClosing :: forall s (m :: * -> *) u a.
Stream s m Char =>
Maybe [Elem]
-> Maybe (ParsecT s u m a)
-> [(Elem, Maybe Elem)]
-> ParsecT s u m (Elem' a)
elSelfClosing Maybe [Elem]
elemOpts Maybe (ParsecT s u m a)
innerSpec [(Elem, Maybe Elem)]
attrsSubset = do
  (Elem
tag, Attrs
attrs) <- Maybe [Elem] -> [(Elem, Maybe Elem)] -> ParsecT s u m (Elem, Attrs)
forall s (m :: * -> *) u.
Stream s m Char =>
Maybe [Elem] -> [(Elem, Maybe Elem)] -> ParsecT s u m (Elem, Attrs)
parseOpeningTag Maybe [Elem]
elemOpts [(Elem, Maybe Elem)]
attrsSubset
  case Maybe (ParsecT s u m a)
innerSpec of
    Just ParsecT s u m a
_ -> ParsecT s u m (Elem' a)
forall s u (m :: * -> *) a. ParsecT s u m a
parserZero
    Maybe (ParsecT s u m a)
Nothing -> Elem' a -> ParsecT s u m (Elem' a)
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Elem' a -> ParsecT s u m (Elem' a))
-> Elem' a -> ParsecT s u m (Elem' a)
forall a b. (a -> b) -> a -> b
$ Elem -> Attrs -> [a] -> Elem -> Elem' a
forall a. Elem -> Attrs -> [a] -> Elem -> Elem' a
Elem' Elem
tag Attrs
attrs [a]
forall a. Monoid a => a
mempty Elem
forall a. Monoid a => a
mempty 

elemWithBody :: (ShowHTML a, Stream s m Char) =>
              Maybe [Elem]
           -> Maybe (ParsecT s u m a)
           -> [(String, Maybe String)]
           -> ParsecT s u m (Elem' a)
elemWithBody :: forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
Maybe [Elem]
-> Maybe (ParsecT s u m a)
-> [(Elem, Maybe Elem)]
-> ParsecT s u m (Elem' a)
elemWithBody Maybe [Elem]
elemList Maybe (ParsecT s u m a)
innerSpec [(Elem, Maybe Elem)]
attrs = do
  Elem' a
e <- Maybe [Elem]
-> Maybe (ParsecT s u m a)
-> [(Elem, Maybe Elem)]
-> ParsecT s u m (Elem' a)
forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
Maybe [Elem]
-> Maybe (ParsecT s u m a)
-> [(Elem, Maybe Elem)]
-> ParsecT s u m (Elem' a)
elemParserInternal Maybe [Elem]
elemList Maybe (ParsecT s u m a)
innerSpec [(Elem, Maybe Elem)]
attrs
  Bool -> ParsecT s u m () -> ParsecT s u m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (Elem' a -> [a]
forall b. Elem' b -> [b]
forall (a :: * -> *) b. ElementRep a => a b -> [b]
matches' Elem' a
e) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< (case Maybe (ParsecT s u m a)
innerSpec of { Maybe (ParsecT s u m a)
Nothing -> Int
0; Maybe (ParsecT s u m a)
_ -> Int
1 })) (Elem -> ParsecT s u m ()
forall s u (m :: * -> *) a. Elem -> ParsecT s u m a
parserFail Elem
"not enough matches")
  Elem' a -> ParsecT s u m (Elem' a)
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return Elem' a
e
  
elemParserInternal :: (ShowHTML a, Stream s m Char) =>
              Maybe [Elem]
           -> Maybe (ParsecT s u m a)
           -> [(String, Maybe String)]
           -> ParsecT s u m (Elem' a)
elemParserInternal :: forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
Maybe [Elem]
-> Maybe (ParsecT s u m a)
-> [(Elem, Maybe Elem)]
-> ParsecT s u m (Elem' a)
elemParserInternal Maybe [Elem]
elemList Maybe (ParsecT s u m a)
innerSpec [(Elem, Maybe Elem)]
attrs = do
  (Elem
elem', Attrs
attrs') <- Maybe [Elem] -> [(Elem, Maybe Elem)] -> ParsecT s u m (Elem, Attrs)
forall s (m :: * -> *) u.
Stream s m Char =>
Maybe [Elem] -> [(Elem, Maybe Elem)] -> ParsecT s u m (Elem, Attrs)
parseOpeningTag Maybe [Elem]
elemList [(Elem, Maybe Elem)]
attrs
  -- we should now read the elem' to see if in list of self-closing tags
  -- case elem elem' selfClosing of
    -- True -> return Elem' elem' attrs' 
  (Elem
asString, [a]
matches) <- ([HTMLMatcher Elem' a] -> (Elem, [a]))
-> ParsecT s u m [HTMLMatcher Elem' a] -> ParsecT s u m (Elem, [a])
forall a b. (a -> b) -> ParsecT s u m a -> ParsecT s u m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((HTMLMatcher Elem' a -> (Elem, [a]) -> (Elem, [a]))
-> (Elem, [a]) -> [HTMLMatcher Elem' a] -> (Elem, [a])
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr HTMLMatcher Elem' a -> (Elem, [a]) -> (Elem, [a])
forall (e :: * -> *) a.
(ShowHTML (e a), ShowHTML a, ElementRep e) =>
HTMLMatcher e a -> (Elem, [a]) -> (Elem, [a])
foldFuncTup (Elem, [a])
forall a. Monoid a => a
mempty)  -- this cant be where we do "/>" if we parse ">" in parseOpeningTag
    (ParsecT s u m [HTMLMatcher Elem' a] -> ParsecT s u m (Elem, [a]))
-> ParsecT s u m [HTMLMatcher Elem' a] -> ParsecT s u m (Elem, [a])
forall a b. (a -> b) -> a -> b
$ (ParsecT s u m Elem -> ParsecT s u m Elem
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (Elem -> ParsecT s u m Elem
forall s (m :: * -> *) u.
Stream s m Char =>
Elem -> ParsecT s u m Elem
string Elem
"/>") ParsecT s u m Elem
-> ParsecT s u m [HTMLMatcher Elem' a]
-> ParsecT s u m [HTMLMatcher Elem' a]
forall a b. ParsecT s u m a -> ParsecT s u m b -> ParsecT s u m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [HTMLMatcher Elem' a] -> ParsecT s u m [HTMLMatcher Elem' a]
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return [])
    ParsecT s u m [HTMLMatcher Elem' a]
-> ParsecT s u m [HTMLMatcher Elem' a]
-> ParsecT s u m [HTMLMatcher Elem' a]
forall a. ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (ParsecT s u m [HTMLMatcher Elem' a]
-> ParsecT s u m [HTMLMatcher Elem' a]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT s u m [HTMLMatcher Elem' a]
 -> ParsecT s u m [HTMLMatcher Elem' a])
-> ParsecT s u m [HTMLMatcher Elem' a]
-> ParsecT s u m [HTMLMatcher Elem' a]
forall a b. (a -> b) -> a -> b
$ Elem
-> Maybe (ParsecT s u m a) -> ParsecT s u m [HTMLMatcher Elem' a]
forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
Elem
-> Maybe (ParsecT s u m a) -> ParsecT s u m [HTMLMatcher Elem' a]
innerElemParser Elem
elem' Maybe (ParsecT s u m a)
innerSpec) -- need to be sure that we have exhausted looking for an end tag, then we can do the following safely
    ParsecT s u m [HTMLMatcher Elem' a]
-> ParsecT s u m [HTMLMatcher Elem' a]
-> ParsecT s u m [HTMLMatcher Elem' a]
forall a. ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Maybe (ParsecT s u m a) -> ParsecT s u m [HTMLMatcher Elem' a]
forall a s (m :: * -> *) u (e :: * -> *).
(ShowHTML a, Stream s m Char) =>
Maybe (ParsecT s u m a) -> ParsecT s u m [HTMLMatcher e a]
selfClosingTextful Maybe (ParsecT s u m a)
innerSpec)
  Elem' a -> ParsecT s u m (Elem' a)
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Elem' a -> ParsecT s u m (Elem' a))
-> Elem' a -> ParsecT s u m (Elem' a)
forall a b. (a -> b) -> a -> b
$ Elem -> Attrs -> [a] -> Elem -> Elem' a
forall a. Elem -> Attrs -> [a] -> Elem -> Elem' a
Elem' Elem
elem' Attrs
attrs' [a]
matches Elem
asString


-- elemParserInternalV2 :: (ShowHTML a, Stream s m Char) =>
--               Maybe [Elem]
--            -> Maybe (ParsecT s u m a)
--            -> [(String, Maybe String)]
--            -> ParsecT s u m (Elem' a)
-- elemParserInternalV2 elemList innerSpec attrs = do
--   (elem', attrs') <- parseOpeningTag elemList attrs
--   -- we should now read the elem' to see if in list of self-closing tags
--   case elem elem' selfClosing of
--     True -> (try string ">" <|> string "/>") >> return Elem' elem' attrs' mempty mempty 
--     False -> do
--       (asString, matches) <- fmap (foldr foldFuncTup mempty)  -- this cant be where we do "/>" if we parse ">" in parseOpeningTag
--         $ (try (string "/>") >> return [])
--         <|> (try $ innerElemParser elem' innerSpec) -- need to be sure that we have exhausted looking for an end tag, then we can do the following safely
--         <|> (selfClosingTextful innerSpec)
--       return $ Elem' elem' attrs' matches (reverse asString)


  
-- elemParser :: (ShowHTML a, Stream s m Char) =>
--               Maybe [Elem]
--            -> Maybe (ParsecT s u m a)
--            -> [(String, Maybe String)]
--            -> ParsecT s u m (Elem' a)
-- elemParser elemList innerSpec attrs = do
--   -- let required = case innerSpec of { Nothing -> 0; _ -> 1 }
--   (elem', attrs') <- parseOpeningTag elemList attrs
--   innerH <- fmap (foldr foldFuncTup mempty)
--             -- this cant be where we do "/>" if we parse ">" in parseOpeningTag
--             $ (try (string "/>") >> return [])
--             <|> (try $ innerElemParser elem' innerSpec)
--             -- need to be sure that we have exhausted looking for an end tag
--             -- then we can do the following safely
--             <|> (selfClosingTextful innerSpec)
--   enoughMatches 0 elem' attrs' innerH


innerElemParser :: (ShowHTML a, Stream s m Char) =>
                   String
                -> Maybe (ParsecT s u m a)
                -> ParsecT s u m [HTMLMatcher Elem' a]
innerElemParser :: forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
Elem
-> Maybe (ParsecT s u m a) -> ParsecT s u m [HTMLMatcher Elem' a]
innerElemParser Elem
eTag Maybe (ParsecT s u m a)
innerSpec = Char -> ParsecT s u m Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'>'
                                 ParsecT s u m Char
-> ParsecT s u m [HTMLMatcher Elem' a]
-> ParsecT s u m [HTMLMatcher Elem' a]
forall a b. ParsecT s u m a -> ParsecT s u m b -> ParsecT s u m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT s u m (HTMLMatcher Elem' a)
-> ParsecT s u m Elem -> ParsecT s u m [HTMLMatcher Elem' a]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill (ParsecT s u m (HTMLMatcher Elem' a)
-> ParsecT s u m (HTMLMatcher Elem' a)
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (a -> HTMLMatcher Elem' a
forall (a :: * -> *) b. b -> HTMLMatcher a b
Match (a -> HTMLMatcher Elem' a)
-> ParsecT s u m a -> ParsecT s u m (HTMLMatcher Elem' a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ParsecT s u m a -> Maybe (ParsecT s u m a) -> ParsecT s u m a
forall a. a -> Maybe a -> a
fromMaybe ParsecT s u m a
forall s u (m :: * -> *) a. ParsecT s u m a
parserZero Maybe (ParsecT s u m a)
innerSpec))
                                              ParsecT s u m (HTMLMatcher Elem' a)
-> ParsecT s u m (HTMLMatcher Elem' a)
-> ParsecT s u m (HTMLMatcher Elem' a)
forall a. ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (ParsecT s u m (HTMLMatcher Elem' a)
-> ParsecT s u m (HTMLMatcher Elem' a)
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (Elem -> HTMLMatcher Elem' a
forall (a :: * -> *) b. Elem -> HTMLMatcher a b
IText (Elem -> HTMLMatcher Elem' a)
-> ParsecT s u m Elem -> ParsecT s u m (HTMLMatcher Elem' a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT s u m Elem
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Elem
stylingElem)) -- this line is new/unstable
                                              ParsecT s u m (HTMLMatcher Elem' a)
-> ParsecT s u m (HTMLMatcher Elem' a)
-> ParsecT s u m (HTMLMatcher Elem' a)
forall a. ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT s u m (HTMLMatcher Elem' a)
-> ParsecT s u m (HTMLMatcher Elem' a)
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (Elem' a -> HTMLMatcher Elem' a
forall (a :: * -> *) b. a b -> HTMLMatcher a b
Element (Elem' a -> HTMLMatcher Elem' a)
-> ParsecT s u m (Elem' a) -> ParsecT s u m (HTMLMatcher Elem' a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Elem -> Maybe (ParsecT s u m a) -> ParsecT s u m (Elem' a)
forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
Elem -> Maybe (ParsecT s u m a) -> ParsecT s u m (Elem' a)
sameElTag Elem
eTag Maybe (ParsecT s u m a)
innerSpec)
                                              ParsecT s u m (HTMLMatcher Elem' a)
-> ParsecT s u m (HTMLMatcher Elem' a)
-> ParsecT s u m (HTMLMatcher Elem' a)
forall a. ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ((Elem -> HTMLMatcher Elem' a
forall (a :: * -> *) b. Elem -> HTMLMatcher a b
IText (Elem -> HTMLMatcher Elem' a)
-> (Char -> Elem) -> Char -> HTMLMatcher Elem' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Elem -> Elem
forall a. a -> [a] -> [a]
:[])) (Char -> HTMLMatcher Elem' a)
-> ParsecT s u m Char -> ParsecT s u m (HTMLMatcher Elem' a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT s u m Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
anyChar)) (Elem -> ParsecT s u m Elem
forall s (m :: * -> *) u.
Stream s m Char =>
Elem -> ParsecT s u m Elem
endTag Elem
eTag)
                                              

-- Doesnt change the structure of the page at all just how text is styled like MS word stuff
stylingTags :: [Elem]
stylingTags = [Elem
"abbr", Elem
"b", Elem
"big", Elem
"acronym", Elem
"dfn", Elem
"em", Elem
"font", Elem
"i", Elem
"mark", Elem
"q", Elem
"small", Elem
"strong"]

-- | Just gives the inners 
stylingElem :: Stream s m Char => ParsecT s u m String 
stylingElem :: forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Elem
stylingElem = do
  (Elem
e,Attrs
_) <- Maybe [Elem] -> [(Elem, Maybe Elem)] -> ParsecT s u m (Elem, Attrs)
forall s (m :: * -> *) u.
Stream s m Char =>
Maybe [Elem] -> [(Elem, Maybe Elem)] -> ParsecT s u m (Elem, Attrs)
parseOpeningTag ([Elem] -> Maybe [Elem]
forall a. a -> Maybe a
Just [Elem]
stylingTags) []
  Char -> ParsecT s u m Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'>'
  ((Elem, Elem) -> Elem)
-> ParsecT s u m (Elem, Elem) -> ParsecT s u m Elem
forall a b. (a -> b) -> ParsecT s u m a -> ParsecT s u m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Elem, Elem) -> Elem
forall a b. (a, b) -> a
fst (ParsecT s u m (Elem, Elem) -> ParsecT s u m Elem)
-> ParsecT s u m (Elem, Elem) -> ParsecT s u m Elem
forall a b. (a -> b) -> a -> b
$ ParsecT s u m Char
-> ParsecT s u m Elem -> ParsecT s u m (Elem, Elem)
forall s u (m :: * -> *) a end.
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m ([a], end)
manyTill_ ParsecT s u m Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
anyChar (Elem -> ParsecT s u m Elem
forall s (m :: * -> *) u.
Stream s m Char =>
Elem -> ParsecT s u m Elem
endTag Elem
e)
  
-- f :: ([a], [b], [c]) -> Elem' a
-- f (x,y,z) = f' x y z

-- f' el attrs = Elem' el attrs

    

--   case length $ matches itr of
--     0 ->
--       case innerSpec of
--         Nothing -> return $ Elem' elem' attrs' (matches itr) (innerText itr)
--         _ -> parserZero
--     _ -> return $ Elem' elem' attrs' (matches itr) (innerText itr)

   
-- -- this should be a success but does not allow 


-- baseInnerParser :: Stream s m Char =>
--                    Maybe (ParsecT s u m a)
--                 -> ParsecT s u m String
--                 -> ParsecT s u m (InnerTextResult a)
-- baseInnerParser innerPat endParse = do
--       _ <- char '>'

--       x :: [Inner a] <- manyTill_ (Match $ match <|> El <$> sameElTag <|> NonMatch anyChar) endParse

--       foldHtmlMatcher x
      
      
      -- (pre, patternFound) <- manyTill_ (try sameElTag <|> p) (fromMaybe anyChar innerPat)
      -- (post, _) <- manyTill_  (try sameElTag <|> p) endParse

      -- return $ InnerTextResult { match = patternFound
      --                          , fullInner = mconcat pre <> patternFound <> mconcat post }



-- -- | Gets own subsets (eg div if outer = div)
-- -- | Monoid may need to be implemented so that we can have mempty to help generalize
-- parseInnerHTMLAndEndTag2 :: (ToHTML a, Stream s m Char) => Elem -> Maybe (ParsecT s u m a) -> ParsecT s u m (InnerTextResult a)
-- parseInnerHTMLAndEndTag2 elem innerPattern = do
--   let
--     p :: Stream s m Char => ParsecT s u m (Inner a) 
--     p = do
--       a <- anyChar
--       return (NonMatch $ a : [])
      
--     -- what does anyTagInner do?
--     -- should it instead be 
--     anyTagInner :: Stream s m Char => Maybe (ParsecT s u m String) -> ParsecT s u m (InnerTextResult a)
--     anyTagInner innerP = baseParser innerP (try (char '<'
--                                                   >> (optional (char '/'))
--                                                   >> MParsec.some anyChar -- end tag 
--                                                   >> (string " " <|> string ">")))

--     normal :: Stream s m Char => Maybe (ParsecT s u m String) -> ParsecT s u m (InnerTextResult a)
--     normal innerP = baseParser innerP (try (string ("</" <> elem <> ">")))
      
--   x <- Match <$> normal <|> NonMatch <$> 

--   baseInnerParser
--   _ <- char '>'
--   (pre, patternFound) <- manyTill_ (try (sameElTag elem) <|> p) (fromMaybe anyChar innerPattern)
--   (post, _) <- manyTill_  (try sameElTag <|> p) endParse

--   return $ InnerTextResult { match = patternFound
--                            , fullInner = mconcat pre <> patternFound <> mconcat post }

  
  
--   x <- MParsec.eitherP (try (string "/>")) (normal innerPattern <|> anyTagInner innerPattern)
--   case x of
--      Left  a -> -- was "/>" 
--        case innerPattern of
--          Just a -> parserZero
--          Nothing ->
--            pure InnerTextResult { match = "", fullInner = "" }

--      --was not "/>"
--        -- but i believe could still be
--          -- > implicit self-closing tag
--          -- > fail 
         
--      Right b -> return b

-- | Does not get subsets, gets most inner (Elem <-> Match) combo
-- | Monoid may need to be implemented so that we can have mempty to help generalize
{-# DEPRECATED parseInnerHTMLAndEndTag "use new elem parser directly" #-}
parseInnerHTMLAndEndTag :: (Stream s m Char) =>
                           Elem
                        -> Maybe (ParsecT s u m String)
                        -> ParsecT s u m (InnerTextResult String)
parseInnerHTMLAndEndTag :: forall s (m :: * -> *) u.
Stream s m Char =>
Elem
-> Maybe (ParsecT s u m Elem)
-> ParsecT s u m (InnerTextResult Elem)
parseInnerHTMLAndEndTag Elem
elem Maybe (ParsecT s u m Elem)
innerPattern = do

  let f :: Stream s m Char => Maybe (ParsecT s u m String) -> ParsecT s u m String
      f :: forall s (m :: * -> *) u.
Stream s m Char =>
Maybe (ParsecT s u m Elem) -> ParsecT s u m Elem
f Maybe (ParsecT s u m Elem)
x = case Maybe (ParsecT s u m Elem)
x of
              Just ParsecT s u m Elem
pat -> ParsecT s u m Elem
pat
              Maybe (ParsecT s u m Elem)
Nothing -> Elem -> ParsecT s u m Elem
forall s (m :: * -> *) u.
Stream s m Char =>
Elem -> ParsecT s u m Elem
string Elem
""

      sameElTag :: Stream s m Char => ParsecT s u m String
      sameElTag :: forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Elem
sameElTag = do
        Elem' Elem
el <- Maybe [Elem]
-> Maybe (ParsecT s u m Elem)
-> [(Elem, Maybe Elem)]
-> ParsecT s u m (Elem' Elem)
forall s (m :: * -> *) u.
Stream s m Char =>
Maybe [Elem]
-> Maybe (ParsecT s u m Elem)
-> [(Elem, Maybe Elem)]
-> ParsecT s u m (Elem' Elem)
elemParserOld ([Elem] -> Maybe [Elem]
forall a. a -> Maybe a
Just [Elem
elem]) Maybe (ParsecT s u m Elem)
forall a. Maybe a
Nothing []
        Elem -> ParsecT s u m Elem
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Elem -> ParsecT s u m Elem) -> Elem -> ParsecT s u m Elem
forall a b. (a -> b) -> a -> b
$ Elem' Elem -> Elem
forall a. ShowHTML a => a -> Elem
showH Elem' Elem
el

      p :: Stream s m Char => ParsecT s u m String 
      p :: forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Elem
p = do
        Char
a <- ParsecT s u m Char
forall s (m :: * -> *) t u.
(Stream s m t, Show t) =>
ParsecT s u m t
anyToken
        Elem -> ParsecT s u m Elem
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Char
a Char -> Elem -> Elem
forall a. a -> [a] -> [a]
: [])

      baseParser :: Stream s m Char => Maybe (ParsecT s u m String) -> ParsecT s u m String -> ParsecT s u m (InnerTextResult String)
      baseParser :: forall s (m :: * -> *) u.
Stream s m Char =>
Maybe (ParsecT s u m Elem)
-> ParsecT s u m Elem -> ParsecT s u m (InnerTextResult Elem)
baseParser Maybe (ParsecT s u m Elem)
innerPat ParsecT s u m Elem
endParse = do
        Char
_ <- Char -> ParsecT s u m Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'>'
        ([Elem]
pre, Elem
patternFound) <- ParsecT s u m Elem
-> ParsecT s u m Elem -> ParsecT s u m ([Elem], Elem)
forall s u (m :: * -> *) a end.
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m ([a], end)
manyTill_ (ParsecT s u m Elem -> ParsecT s u m Elem
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT s u m Elem
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Elem
sameElTag ParsecT s u m Elem -> ParsecT s u m Elem -> ParsecT s u m Elem
forall a. ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT s u m Elem
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Elem
p) (Maybe (ParsecT s u m Elem) -> ParsecT s u m Elem
forall s (m :: * -> *) u.
Stream s m Char =>
Maybe (ParsecT s u m Elem) -> ParsecT s u m Elem
f Maybe (ParsecT s u m Elem)
innerPat)
        ([Elem]
post, Elem
_) <- ParsecT s u m Elem
-> ParsecT s u m Elem -> ParsecT s u m ([Elem], Elem)
forall s u (m :: * -> *) a end.
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m ([a], end)
manyTill_  (ParsecT s u m Elem -> ParsecT s u m Elem
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT s u m Elem
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Elem
sameElTag ParsecT s u m Elem -> ParsecT s u m Elem -> ParsecT s u m Elem
forall a. ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT s u m Elem
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Elem
p) ParsecT s u m Elem
endParse

        InnerTextResult Elem -> ParsecT s u m (InnerTextResult Elem)
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (InnerTextResult Elem -> ParsecT s u m (InnerTextResult Elem))
-> InnerTextResult Elem -> ParsecT s u m (InnerTextResult Elem)
forall a b. (a -> b) -> a -> b
$ InnerTextResult { _matchesITR :: [Elem]
_matchesITR = [Elem
patternFound]
                                 , _fullInner :: Elem
_fullInner = [Elem] -> Elem
forall a. Monoid a => [a] -> a
mconcat [Elem]
pre Elem -> Elem -> Elem
forall a. Semigroup a => a -> a -> a
<> Elem
patternFound Elem -> Elem -> Elem
forall a. Semigroup a => a -> a -> a
<> [Elem] -> Elem
forall a. Monoid a => [a] -> a
mconcat [Elem]
post }
  
      anyTagInner :: Stream s m Char => Maybe (ParsecT s u m String) -> ParsecT s u m (InnerTextResult String)
      anyTagInner :: forall s (m :: * -> *) u.
Stream s m Char =>
Maybe (ParsecT s u m Elem) -> ParsecT s u m (InnerTextResult Elem)
anyTagInner Maybe (ParsecT s u m Elem)
innerP = Maybe (ParsecT s u m Elem)
-> ParsecT s u m Elem -> ParsecT s u m (InnerTextResult Elem)
forall s (m :: * -> *) u.
Stream s m Char =>
Maybe (ParsecT s u m Elem)
-> ParsecT s u m Elem -> ParsecT s u m (InnerTextResult Elem)
baseParser Maybe (ParsecT s u m Elem)
innerP (ParsecT s u m Elem -> ParsecT s u m Elem
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (Char -> ParsecT s u m Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'<'
                                                   ParsecT s u m Char -> ParsecT s u m () -> ParsecT s u m ()
forall a b. ParsecT s u m a -> ParsecT s u m b -> ParsecT s u m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (ParsecT s u m Char -> ParsecT s u m ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
optional (Char -> ParsecT s u m Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'/'))
----------------------------------------------------DOES THIS ACTUALLY WORK BELOW?--------------------
                                                   ParsecT s u m () -> ParsecT s u m Elem -> ParsecT s u m Elem
forall a b. ParsecT s u m a -> ParsecT s u m b -> ParsecT s u m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT s u m Char -> ParsecT s u m Elem
forall a. ParsecT s u m a -> ParsecT s u m [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
some ParsecT s u m Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
anyChar -- end tag 
                                                   ParsecT s u m Elem -> ParsecT s u m Elem -> ParsecT s u m Elem
forall a b. ParsecT s u m a -> ParsecT s u m b -> ParsecT s u m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Elem -> ParsecT s u m Elem
forall s (m :: * -> *) u.
Stream s m Char =>
Elem -> ParsecT s u m Elem
string Elem
" " ParsecT s u m Elem -> ParsecT s u m Elem -> ParsecT s u m Elem
forall a. ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Elem -> ParsecT s u m Elem
forall s (m :: * -> *) u.
Stream s m Char =>
Elem -> ParsecT s u m Elem
string Elem
">")))

      normal :: Stream s m Char => Maybe (ParsecT s u m String) -> ParsecT s u m (InnerTextResult String)
      normal :: forall s (m :: * -> *) u.
Stream s m Char =>
Maybe (ParsecT s u m Elem) -> ParsecT s u m (InnerTextResult Elem)
normal Maybe (ParsecT s u m Elem)
innerP = Maybe (ParsecT s u m Elem)
-> ParsecT s u m Elem -> ParsecT s u m (InnerTextResult Elem)
forall s (m :: * -> *) u.
Stream s m Char =>
Maybe (ParsecT s u m Elem)
-> ParsecT s u m Elem -> ParsecT s u m (InnerTextResult Elem)
baseParser Maybe (ParsecT s u m Elem)
innerP (ParsecT s u m Elem -> ParsecT s u m Elem
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (Elem -> ParsecT s u m Elem
forall s (m :: * -> *) u.
Stream s m Char =>
Elem -> ParsecT s u m Elem
string (Elem
"</" Elem -> Elem -> Elem
forall a. Semigroup a => a -> a -> a
<> Elem
elem Elem -> Elem -> Elem
forall a. Semigroup a => a -> a -> a
<> Elem
">")))
      

  Either Elem (InnerTextResult Elem)
x <- ParsecT s u m Elem
-> ParsecT s u m (InnerTextResult Elem)
-> ParsecT s u m (Either Elem (InnerTextResult Elem))
forall (m :: * -> *) a b.
Alternative m =>
m a -> m b -> m (Either a b)
eitherP (ParsecT s u m Elem -> ParsecT s u m Elem
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (Elem -> ParsecT s u m Elem
forall s (m :: * -> *) u.
Stream s m Char =>
Elem -> ParsecT s u m Elem
string Elem
"/>")) (Maybe (ParsecT s u m Elem) -> ParsecT s u m (InnerTextResult Elem)
forall s (m :: * -> *) u.
Stream s m Char =>
Maybe (ParsecT s u m Elem) -> ParsecT s u m (InnerTextResult Elem)
normal Maybe (ParsecT s u m Elem)
innerPattern ParsecT s u m (InnerTextResult Elem)
-> ParsecT s u m (InnerTextResult Elem)
-> ParsecT s u m (InnerTextResult Elem)
forall a. ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe (ParsecT s u m Elem) -> ParsecT s u m (InnerTextResult Elem)
forall s (m :: * -> *) u.
Stream s m Char =>
Maybe (ParsecT s u m Elem) -> ParsecT s u m (InnerTextResult Elem)
anyTagInner Maybe (ParsecT s u m Elem)
innerPattern)
  case Either Elem (InnerTextResult Elem)
x of
     Left  Elem
a ->
       case Maybe (ParsecT s u m Elem)
innerPattern of
         Just ParsecT s u m Elem
a -> ParsecT s u m (InnerTextResult Elem)
forall s u (m :: * -> *) a. ParsecT s u m a
parserZero
         Maybe (ParsecT s u m Elem)
Nothing ->
           InnerTextResult Elem -> ParsecT s u m (InnerTextResult Elem)
forall a. a -> ParsecT s u m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure InnerTextResult { _matchesITR :: [Elem]
_matchesITR = [], _fullInner :: Elem
_fullInner = Elem
"" }
         
     Right InnerTextResult Elem
b -> InnerTextResult Elem -> ParsecT s u m (InnerTextResult Elem)
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return InnerTextResult Elem
b
     
  -- Note: we can parse these better with eitherP
  -- eitherP :: Alternative m => m a -> m b -> m (Either a b)

      -- then use case statement to deal with case (A: sub-elem | B: anychar)
        --if sub-elem -> put in list --then--> 
  
  -- (pre, patternFound) <- manyTill_ (try sameElTag <|> p) (f innerPattern)
  -- (post, _) <- manyTill_  (try sameElTag <|> p) (try (string ("</" <> elem <> ">")))

  -- return $ InnerTextResult { match = patternFound
  --                          , fullInner = mconcat pre <> patternFound <> mconcat post }


-- | Note: In case of Nothing for innerSpec, the parser should be : optional anyChar == () 

-- Note: this can be passed to findAll func in megaparsec as is
-- |          attrs (Attr | AnyAttr)   maybe discr elem
{-# DEPRECATED elemParserOld "use elemParser" #-}
elemParserOld :: (Stream s m Char) =>
              Maybe [Elem]
           -> Maybe (ParsecT s u m String)
           -> [(String, Maybe String)]
           -> ParsecT s u m (Elem' String)
elemParserOld :: forall s (m :: * -> *) u.
Stream s m Char =>
Maybe [Elem]
-> Maybe (ParsecT s u m Elem)
-> [(Elem, Maybe Elem)]
-> ParsecT s u m (Elem' Elem)
elemParserOld Maybe [Elem]
elemList Maybe (ParsecT s u m Elem)
innerSpec [(Elem, Maybe Elem)]
attrs = do
  (Elem
elem', Attrs
attrs') <- Maybe [Elem] -> [(Elem, Maybe Elem)] -> ParsecT s u m (Elem, Attrs)
forall s (m :: * -> *) u.
Stream s m Char =>
Maybe [Elem] -> [(Elem, Maybe Elem)] -> ParsecT s u m (Elem, Attrs)
parseOpeningTag Maybe [Elem]
elemList [(Elem, Maybe Elem)]
attrs
  --note that at this point, there is a set elem' to match  
  InnerTextResult Elem
inner <- Elem
-> Maybe (ParsecT s u m Elem)
-> ParsecT s u m (InnerTextResult Elem)
forall s (m :: * -> *) u.
Stream s m Char =>
Elem
-> Maybe (ParsecT s u m Elem)
-> ParsecT s u m (InnerTextResult Elem)
parseInnerHTMLAndEndTag Elem
elem' Maybe (ParsecT s u m Elem)
innerSpec
  Elem' Elem -> ParsecT s u m (Elem' Elem)
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Elem' Elem -> ParsecT s u m (Elem' Elem))
-> Elem' Elem -> ParsecT s u m (Elem' Elem)
forall a b. (a -> b) -> a -> b
$ Elem -> Attrs -> [Elem] -> Elem -> Elem' Elem
forall a. Elem -> Attrs -> [a] -> Elem -> Elem' a
Elem' Elem
elem' Attrs
attrs' (InnerTextResult Elem -> [Elem]
forall a. InnerTextResult a -> [a]
_matchesITR InnerTextResult Elem
inner) (InnerTextResult Elem -> Elem
forall a. InnerTextResult a -> Elem
_fullInner InnerTextResult Elem
inner)
-- | Attrs should really be returned as a map

-- | note that this could even be used for mining text on page
-- | eg. search with innerSpec as "the" or other common articles 

-- either begin parsing the element or href and find that it is what youre looking for
-- OR -> skip to start of next element
 
-- | Note: how do we get a computer to recognize, Boolean-ly if some representation is x?
-- | where x is some statement?

  --  As humans we do this visually : is this object red? yes or no

  --  A computer must do the same with "local vision" that theoretically must be as parsing algorithm
  --  aka Bool `followedby` Bool2 `followedBy` Bool3 ... Bool_n
  
    --  where some Bools are of set-wise funcs and some are singleton-options AKA / therfore some
    --        sequential combo of (Set 1 of Any <-> Set (totalCount) of Any)

    --  Therefore -> this logic generalizes processing to both humans and computers,; the full set of
    --  higher level processors