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

module Scrappy.Elem.TreeElemParser where

import Control.Monad.IO.Class

import Scrappy.Elem.ElemHeadParse (parseOpeningTag, hrefParser', parseOpeningTagDesc, mkAttrsDesc, attrParser)
import Scrappy.Elem.Types (Elem, Attrs, ElemHead, TreeHTML(TreeHTML), HTMLMatcher (IText, Element, Match)
                  , InnerTextHTMLTree(InnerTextHTMLTree), innerTree, innerText, matches, GroupHtml
                  , Elem', TreeIndex, attrs, elTag, ShowHTML, showH, _innerTree', matches'
                  , ElementRep, mkGH, innerText', _innerText, _matches, foldFuncTrup
                  , UrlPagination(..), enoughMatchesTree, selfClosingTextful, endTag)

import Scrappy.Elem.ChainHTML (someHtml, manyHtml, nl)
import Scrappy.Elem.SimpleElemParser (elemParser)
import Scrappy.Find (findNaive)

import Control.Monad (when)
import Control.Applicative (Alternative, liftA2, many, (<|>), some)
import Text.Parsec (Stream, ParsecT, anyChar, try, parserZero, parserFail, string, parse
                   , char, noneOf, option, space, alphaNum, notFollowedBy, (<?>), optional, manyTill)
import qualified Data.Map as Map (Map, toList, fromList, adjust) 
import Data.Graph (Tree (Node), Forest)
import Data.Tree (rootLabel)
import Text.URI as URI
import Data.Char (digitToInt)
import Data.Maybe (fromMaybe, fromJust)
import Data.List
import Data.Text (Text, splitOn)


skipManyTill :: Alternative m => m a -> m end -> m end
skipManyTill :: forall (m :: * -> *) a end. Alternative m => m a -> m end -> m end
skipManyTill m a
p m end
end = m end
go
  where
    go :: m end
go = m end
end m end -> m end -> m end
forall a. m a -> m a -> m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (m a
p m a -> m end -> m end
forall a b. m a -> m b -> m b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> m end
go)
{-# INLINE skipManyTill #-}

-- | Note for research on Parsers/Scrapers + AI research -> if a scraper does provide only
-- | a slow method for processing (Picture -> *) that we might be able to solve this issue with
-- | either Quantum (same as current AI) or with attentive methodologies to "gamble" on what to first pay
-- | attention to , which could further be based on if Video like (frame -> frame) or single picture


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


data Many a = Many a | One a deriving Int -> Many a -> ShowS
[Many a] -> ShowS
Many a -> String
(Int -> Many a -> ShowS)
-> (Many a -> String) -> ([Many a] -> ShowS) -> Show (Many a)
forall a. Show a => Int -> Many a -> ShowS
forall a. Show a => [Many a] -> ShowS
forall a. Show a => Many a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Many a -> ShowS
showsPrec :: Int -> Many a -> ShowS
$cshow :: forall a. Show a => Many a -> String
show :: Many a -> String
$cshowList :: forall a. Show a => [Many a] -> ShowS
showList :: [Many a] -> ShowS
Show

treeLookupIdx :: TreeIndex -> Forest a -> a
treeLookupIdx :: forall a. TreeIndex -> Forest a -> a
treeLookupIdx = TreeIndex -> Forest a -> a
forall a. HasCallStack => a
undefined


-------------------------------------------------------------------------------------------------------------------
----------------------Top Level Functions-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------



  
-- | Like elemParser, this matches on an html element but also represents the innerHTML
-- | as a Tree ElemHead so that we can match this structure in elements further down in the DOM
-- | see groupHtml and treeElemParserSpecific 
treeElemParser :: (Stream s m Char, ShowHTML a) =>
                   Maybe [Elem]
                -> Maybe (ParsecT s u m a)
                -> [(String, Maybe String)]
                -> ParsecT s u m (TreeHTML a)
treeElemParser :: forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe [String]
-> Maybe (ParsecT s u m a)
-> [(String, Maybe String)]
-> ParsecT s u m (TreeHTML a)
treeElemParser Maybe [String]
elemOpts Maybe (ParsecT s u m a)
matchh [(String, Maybe String)]
attrsSubset = do
  TreeHTML a
e <- Maybe [String]
-> Maybe (ParsecT s u m a)
-> [(String, Maybe String)]
-> ParsecT s u m (TreeHTML a)
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe [String]
-> Maybe (ParsecT s u m a)
-> [(String, Maybe String)]
-> ParsecT s u m (TreeHTML a)
treeElemParser' Maybe [String]
elemOpts Maybe (ParsecT s u m a)
matchh [(String, Maybe String)]
attrsSubset
  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 (TreeHTML a -> [a]
forall b. TreeHTML b -> [b]
forall (a :: * -> *) b. ElementRep a => a b -> [b]
matches' TreeHTML a
e) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< (case Maybe (ParsecT s u m a)
matchh of { Maybe (ParsecT s u m a)
Nothing -> Int
0; Maybe (ParsecT s u m a)
_ -> Int
1 })) (String -> ParsecT s u m ()
forall s u (m :: * -> *) a. String -> ParsecT s u m a
parserFail String
"not enough matches")
  TreeHTML a -> ParsecT s u m (TreeHTML a)
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return TreeHTML a
e


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


-- | Used by treeElemParser, is not an interface, use treeElemParser
treeElemParser' :: (Stream s m Char, ShowHTML a) =>
                  Maybe [Elem]
               -> Maybe (ParsecT s u m a)
               -> [(String, Maybe String)]
               -> ParsecT s u m (TreeHTML a)
treeElemParser' :: forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe [String]
-> Maybe (ParsecT s u m a)
-> [(String, Maybe String)]
-> ParsecT s u m (TreeHTML a)
treeElemParser'  Maybe [String]
elemOpts Maybe (ParsecT s u m a)
matchh [(String, Maybe String)]
attrsSubset = do
 (String
elem', Attrs
attrs') <- Maybe [String]
-> [(String, Maybe String)] -> ParsecT s u m (String, Attrs)
forall s (m :: * -> *) u.
Stream s m Char =>
Maybe [String]
-> [(String, Maybe String)] -> ParsecT s u m (String, Attrs)
parseOpeningTag Maybe [String]
elemOpts [(String, Maybe String)]
attrsSubset
 case String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem String
elem' [String]
selfClosing of
   Bool
True -> do
      (ParsecT s u m String -> ParsecT s u m String
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (String -> ParsecT s u m String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
">") ParsecT s u m String
-> ParsecT s u m String -> ParsecT s u m String
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
<|> String -> ParsecT s u m String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"/>")
      case Maybe (ParsecT s u m a)
matchh of
        Maybe (ParsecT s u m a)
Nothing ->  TreeHTML a -> ParsecT s u m (TreeHTML a)
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (TreeHTML a -> ParsecT s u m (TreeHTML a))
-> TreeHTML a -> ParsecT s u m (TreeHTML a)
forall a b. (a -> b) -> a -> b
$ String
-> Attrs -> [a] -> String -> Forest (String, Attrs) -> TreeHTML a
forall a.
String
-> Attrs -> [a] -> String -> Forest (String, Attrs) -> TreeHTML a
TreeHTML String
elem' Attrs
attrs' [a]
forall a. Monoid a => a
mempty String
forall a. Monoid a => a
mempty Forest (String, Attrs)
forall a. Monoid a => a
mempty
        Just ParsecT s u m a
_ -> ParsecT s u m (TreeHTML a)
forall s u (m :: * -> *) a. ParsecT s u m a
parserZero 

     -- ((try string ">") <|> string "/>") >> return TreeHTML elem' attrs' mempty mempty mempty
   Bool
False -> do
     -- (inText, matchBook, treees) <- inerTreeElemParser 
     (String
inText, [a]
matchBook, Forest (String, Attrs)
treees) <- ([HTMLMatcher TreeHTML a] -> (String, [a], Forest (String, Attrs)))
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m (String, [a], Forest (String, Attrs))
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 TreeHTML a
 -> (String, [a], Forest (String, Attrs))
 -> (String, [a], Forest (String, Attrs)))
-> (String, [a], Forest (String, Attrs))
-> [HTMLMatcher TreeHTML a]
-> (String, [a], Forest (String, Attrs))
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr HTMLMatcher TreeHTML a
-> (String, [a], Forest (String, Attrs))
-> (String, [a], Forest (String, Attrs))
forall a.
ShowHTML a =>
HTMLMatcher TreeHTML a
-> (String, [a], Forest (String, Attrs))
-> (String, [a], Forest (String, Attrs))
foldFuncTrup (String, [a], Forest (String, Attrs))
forall a. Monoid a => a
mempty)
                                    (ParsecT s u m [HTMLMatcher TreeHTML a]
 -> ParsecT s u m (String, [a], Forest (String, Attrs)))
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m (String, [a], Forest (String, Attrs))
forall a b. (a -> b) -> a -> b
$ (ParsecT s u m String -> ParsecT s u m String
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (String -> ParsecT s u m String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"/>") ParsecT s u m String
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML 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 TreeHTML a] -> ParsecT s u m [HTMLMatcher TreeHTML a]
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return [])  
                                    ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML 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 TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT s u m [HTMLMatcher TreeHTML a]
 -> ParsecT s u m [HTMLMatcher TreeHTML a])
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall a b. (a -> b) -> a -> b
$ String
-> Maybe (ParsecT s u m a)
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
String
-> Maybe (ParsecT s u m a)
-> ParsecT s u m [HTMLMatcher TreeHTML a]
innerElemParser2 String
elem' Maybe (ParsecT s u m a)
matchh)
                                    ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML 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 TreeHTML 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)
matchh)
     TreeHTML a -> ParsecT s u m (TreeHTML a)
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (TreeHTML a -> ParsecT s u m (TreeHTML a))
-> TreeHTML a -> ParsecT s u m (TreeHTML a)
forall a b. (a -> b) -> a -> b
$ String
-> Attrs -> [a] -> String -> Forest (String, Attrs) -> TreeHTML a
forall a.
String
-> Attrs -> [a] -> String -> Forest (String, Attrs) -> TreeHTML a
TreeHTML String
elem' Attrs
attrs' [a]
matchBook String
inText Forest (String, Attrs)
treees

-------------------------------------------------------------------------------------------------------------------

-- | The real difference between (htmlGroup _ _ _) and specificRepetitiveForest is a matter of if we accept the next
-- | piece to be a new discovery to match on or if we are in that process of matching what we just found


-- digitEq "ere3" "ere4"
innerTreeElemParser :: (ShowHTML a, Stream s m Char) =>
                       Elem
                    -> Maybe (ParsecT s u m a)
                    -> ParsecT s u m (String, [a], [Tree ElemHead])
innerTreeElemParser :: forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
String
-> Maybe (ParsecT s u m a)
-> ParsecT s u m (String, [a], Forest (String, Attrs))
innerTreeElemParser String
elem' Maybe (ParsecT s u m a)
matchh = do
  ([HTMLMatcher TreeHTML a] -> (String, [a], Forest (String, Attrs)))
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m (String, [a], Forest (String, Attrs))
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 TreeHTML a
 -> (String, [a], Forest (String, Attrs))
 -> (String, [a], Forest (String, Attrs)))
-> (String, [a], Forest (String, Attrs))
-> [HTMLMatcher TreeHTML a]
-> (String, [a], Forest (String, Attrs))
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr HTMLMatcher TreeHTML a
-> (String, [a], Forest (String, Attrs))
-> (String, [a], Forest (String, Attrs))
forall a.
ShowHTML a =>
HTMLMatcher TreeHTML a
-> (String, [a], Forest (String, Attrs))
-> (String, [a], Forest (String, Attrs))
foldFuncTrup (String, [a], Forest (String, Attrs))
forall a. Monoid a => a
mempty)
    (ParsecT s u m [HTMLMatcher TreeHTML a]
 -> ParsecT s u m (String, [a], Forest (String, Attrs)))
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m (String, [a], Forest (String, Attrs))
forall a b. (a -> b) -> a -> b
$ (ParsecT s u m String -> ParsecT s u m String
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (String -> ParsecT s u m String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"/>") ParsecT s u m String
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML 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 TreeHTML a] -> ParsecT s u m [HTMLMatcher TreeHTML a]
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return [])
    ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML 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 TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT s u m [HTMLMatcher TreeHTML a]
 -> ParsecT s u m [HTMLMatcher TreeHTML a])
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall a b. (a -> b) -> a -> b
$ String
-> Maybe (ParsecT s u m a)
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
String
-> Maybe (ParsecT s u m a)
-> ParsecT s u m [HTMLMatcher TreeHTML a]
innerElemParser2 String
elem' Maybe (ParsecT s u m a)
matchh)
    ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML 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 TreeHTML 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)
matchh)
-- then i apply this to all leaves of the Map

-- !!!!!!!!!!!!!!!!!!!
-- | NOTE: In future: create function that simplifies all numbers that will be compared to their number of digits
-- 1426674 -> 1234567
-- 1834324 -> 1234567 (==) -> True 




-- htmlGroup --calls--> treeElemParser >>= (many treeSpecific --calls--> specificRepForest) 

-- | Ideal case is that we can do (Many a) struturing even if interspersed with text which would solve issues like
-- | many search terms highlighted, we then wouldnt need to know what the search term is
-- | AND!! we have already seen that it could for exmaple be: <a><b class="hiddenText">Hockey</b></a> 


-- treeElemParserSpecific' :: -> HTMLMatcher TreeHTML String
-- treeElemParserSpecific' = do
--   x <- treeElemParserSpecific
--   case innerText' x == searchTerm of
--     True -> IText (innerText' x)
--     False -> Element x

type SubTree a = [Tree a]
-- | Note: unlike other Element parsers, it does not call itself but innerParserSpecific instead loops with
-- | 
treeElemParserSpecific :: (Stream s m Char, ShowHTML a) =>
                          Maybe (ParsecT s u m a)
                       -> Elem
                       -> [(String, String)]
                       -> SubTree ElemHead
                       -> ParsecT s u m (TreeHTML a)
treeElemParserSpecific :: forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a)
-> String
-> [(String, String)]
-> Forest (String, Attrs)
-> ParsecT s u m (TreeHTML a)
treeElemParserSpecific Maybe (ParsecT s u m a)
match String
elem' [(String, String)]
attrs' Forest (String, Attrs)
subTree = do
  (String
tag, Attrs
attrsOut) <- Maybe [String]
-> [(String, String)] -> ParsecT s u m (String, Attrs)
forall s (m :: * -> *) u.
Stream s m Char =>
Maybe [String]
-> [(String, String)] -> ParsecT s u m (String, Attrs)
parseOpeningTagDesc ([String] -> Maybe [String]
forall a. a -> Maybe a
Just [String
elem']) [(String, String)]
attrs'
  Char -> ParsecT s u m Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'>'
  ([a]
matchBook, String
inText, Forest (String, Attrs)
treees) <- Maybe (ParsecT s u m a)
-> String
-> Forest (String, Attrs)
-> ParsecT s u m ([a], String, Forest (String, Attrs))
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a)
-> String
-> Forest (String, Attrs)
-> ParsecT s u m ([a], String, Forest (String, Attrs))
innerParserSpecific Maybe (ParsecT s u m a)
match String
tag Forest (String, Attrs)
subTree
  TreeHTML a -> ParsecT s u m (TreeHTML a)
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (TreeHTML a -> ParsecT s u m (TreeHTML a))
-> TreeHTML a -> ParsecT s u m (TreeHTML a)
forall a b. (a -> b) -> a -> b
$ String
-> Attrs -> [a] -> String -> Forest (String, Attrs) -> TreeHTML a
forall a.
String
-> Attrs -> [a] -> String -> Forest (String, Attrs) -> TreeHTML a
TreeHTML String
tag Attrs
attrsOut [a]
matchBook String
inText Forest (String, Attrs)
treees

validateGPR :: [Many (Tree ElemHead)] -> ParsecT s u m [HTMLMatcher TreeHTML a]
validateGPR :: forall s u (m :: * -> *) a.
[Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
validateGPR [Many (Tree (String, Attrs))]
manyElHeads =
  if [Many (Tree (String, Attrs))] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ((Many (Tree (String, Attrs)) -> Bool)
-> [Many (Tree (String, Attrs))] -> [Many (Tree (String, Attrs))]
forall a. (a -> Bool) -> [a] -> [a]
filter (\case {One Tree (String, Attrs)
a -> Bool
True; Many (Tree (String, Attrs))
_ -> Bool
False}) [Many (Tree (String, Attrs))]
manyElHeads) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 then [HTMLMatcher TreeHTML a] -> ParsecT s u m [HTMLMatcher TreeHTML a]
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return []
  else String -> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s u (m :: * -> *) a. String -> ParsecT s u m a
parserFail String
"promised elements not found"

-- We allow attrs to be any then check, but also avoid unnceccessarily parsing attrs
--  following needs to be inside many
-- innerParser will be from case elem tag selfClosing and on 
--  Is able to repeat / execute any pattern that returns multiple elements of same type
-- (see manyTreeElemHeadParser)


-- | Uses HTMLMatcher to collect cases of html while parsing inside of a certain element
htmlGenParserRepeat' :: (Stream s m Char, ShowHTML a) =>
                       String 
                    -> Maybe (ParsecT s u m a)
                    -> [Many (Tree ElemHead)]
                    -> ParsecT s u m [HTMLMatcher TreeHTML a]
htmlGenParserRepeat' :: forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
htmlGenParserRepeat' String
elemTag Maybe (ParsecT s u m a)
match [Many (Tree (String, Attrs))]
manyElHeads = {-parsesTreeHs-}
  ((String -> ParsecT s u m String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
endTag String
elemTag) ParsecT s u m String
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML 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
>> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s u (m :: * -> *) a.
[Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
validateGPR [Many (Tree (String, Attrs))]
manyElHeads)
  ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML 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
<|> (HTMLMatcher TreeHTML a
 -> [HTMLMatcher TreeHTML a] -> [HTMLMatcher TreeHTML a])
-> ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
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 -> HTMLMatcher TreeHTML a)
-> ParsecT s u m a -> ParsecT s u m (HTMLMatcher TreeHTML 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 a -> HTMLMatcher TreeHTML a
forall (a :: * -> *) b. b -> HTMLMatcher a b
Match (ParsecT s u m a -> ParsecT s u m (HTMLMatcher TreeHTML a))
-> ParsecT s u m a -> ParsecT s u m (HTMLMatcher TreeHTML a)
forall a b. (a -> b) -> a -> b
$ ParsecT s u m a -> ParsecT s u m a
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (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)
match)) (String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
htmlGenParserRepeat String
elemTag Maybe (ParsecT s u m a)
match [Many (Tree (String, Attrs))]
manyElHeads)
  ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML 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
<|> (HTMLMatcher TreeHTML a
 -> [HTMLMatcher TreeHTML a] -> [HTMLMatcher TreeHTML a])
-> ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
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 (:) ((String -> HTMLMatcher TreeHTML a)
-> ParsecT s u m String -> ParsecT s u m (HTMLMatcher TreeHTML 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 String -> HTMLMatcher TreeHTML a
forall (a :: * -> *) b. String -> HTMLMatcher a b
IText (ParsecT s u m String -> ParsecT s u m (HTMLMatcher TreeHTML a))
-> ParsecT s u m String -> ParsecT s u m (HTMLMatcher TreeHTML a)
forall a b. (a -> b) -> a -> b
$ ParsecT s u m String -> ParsecT s u m String
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT s u m String
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m String
stylingElem) (String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
htmlGenParserRepeat String
elemTag Maybe (ParsecT s u m a)
match [Many (Tree (String, Attrs))]
manyElHeads) 
  ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML 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 TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ((Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a)
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a)
treeElemParserSpecificContinuous Maybe (ParsecT s u m a)
match [Many (Tree (String, Attrs))]
manyElHeads)
       ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a)
-> (([Many (Tree (String, Attrs))], TreeHTML a)
    -> ParsecT s u m [HTMLMatcher TreeHTML a])
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall a b.
ParsecT s u m a -> (a -> ParsecT s u m b) -> ParsecT s u m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\([Many (Tree (String, Attrs))]
sM, TreeHTML a
a) -> ([HTMLMatcher TreeHTML a] -> [HTMLMatcher TreeHTML a])
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML 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 ((TreeHTML a -> HTMLMatcher TreeHTML a
forall (a :: * -> *) b. a b -> HTMLMatcher a b
Element TreeHTML a
a)HTMLMatcher TreeHTML a
-> [HTMLMatcher TreeHTML a] -> [HTMLMatcher TreeHTML a]
forall a. a -> [a] -> [a]
:) (String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
htmlGenParserRepeat String
elemTag Maybe (ParsecT s u m a)
match [Many (Tree (String, Attrs))]
sM)))
  ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML 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
<|> (HTMLMatcher TreeHTML a
 -> [HTMLMatcher TreeHTML a] -> [HTMLMatcher TreeHTML a])
-> ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
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 (:) ((String -> HTMLMatcher TreeHTML a
forall (a :: * -> *) b. String -> HTMLMatcher a b
IText (String -> HTMLMatcher TreeHTML a)
-> (Char -> String) -> Char -> HTMLMatcher TreeHTML a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> ShowS
forall a. a -> [a] -> [a]
:[])) (Char -> HTMLMatcher TreeHTML a)
-> ParsecT s u m Char -> ParsecT s u m (HTMLMatcher TreeHTML 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) (String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
htmlGenParserRepeat String
elemTag Maybe (ParsecT s u m a)
match [Many (Tree (String, Attrs))]
manyElHeads) 



-- Note: even tho this is done / works (below) we could allow for any element the entire time
-- | BUT! we need to check off our list of demanded elements so that when we parse the end tag, we can see if
-- | the elements (ordered and parsed only in order) were all found before the end tag
-- for htmlGenParserRepeat it can just change the passed state of [Many (Tree ElemHead)]


htmlGenParserRepeat :: (Stream s m Char, ShowHTML a) =>
                       String 
                    -> Maybe (ParsecT s u m a)
                    -> [Many (Tree ElemHead)]
                    -> ParsecT s u m [HTMLMatcher TreeHTML a]
htmlGenParserRepeat :: forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
htmlGenParserRepeat String
elemTag Maybe (ParsecT s u m a)
match [Many (Tree (String, Attrs))]
manyElHeadss = {-parsesTreeHs-}
  case [Many (Tree (String, Attrs))]
manyElHeadss of
    -- [] -> fmap ((flip (:) []) . IText . fst) (manyTill_ (htmlGenParserFlex match) (endTag elemTag))

    [] -> do
      ([HTMLMatcher TreeHTML a]
htMMers, String
_) <-  ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m String
-> ParsecT s u m ([HTMLMatcher TreeHTML a], String)
forall s u (m :: * -> *) a end.
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m ([a], end)
manyTill_ (Maybe (ParsecT s u m a) -> ParsecT s u m (HTMLMatcher TreeHTML a)
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a) -> ParsecT s u m (HTMLMatcher TreeHTML a)
htmlGenParserFlex Maybe (ParsecT s u m a)
match) (String -> ParsecT s u m String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
endTag String
elemTag)
      if [HTMLMatcher TreeHTML a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ((HTMLMatcher TreeHTML a -> Bool)
-> [HTMLMatcher TreeHTML a] -> [HTMLMatcher TreeHTML a]
forall a. (a -> Bool) -> [a] -> [a]
filter (\case {Element TreeHTML a
_ -> Bool
True; HTMLMatcher TreeHTML a
_ -> Bool
False}) [HTMLMatcher TreeHTML a]
htMMers) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 -- AND loose == False 
        then
        [HTMLMatcher TreeHTML a] -> ParsecT s u m [HTMLMatcher TreeHTML a]
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return [HTMLMatcher TreeHTML a]
htMMers
        else
        String -> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s u (m :: * -> *) a. String -> ParsecT s u m a
parserFail String
"extra elements"
    --  New idea: parse this structure up until the endTag and invalidate if number of elems exceeds 0 
    
      -- fmap ((flip (:) []) . IText . (:[])) specificITextParser
      -- fmap ((flip (:) []) . IText . fst)  (manyTill_ anyChar (endTag elemTag))
    [Many (Tree (String, Attrs))]
manyElHeads ->
      -- For strict matching: try expected elements first, fail on unexpected elements
      (HTMLMatcher TreeHTML a
 -> [HTMLMatcher TreeHTML a] -> [HTMLMatcher TreeHTML a])
-> ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
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 -> HTMLMatcher TreeHTML a)
-> ParsecT s u m a -> ParsecT s u m (HTMLMatcher TreeHTML 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 a -> HTMLMatcher TreeHTML a
forall (a :: * -> *) b. b -> HTMLMatcher a b
Match (ParsecT s u m a -> ParsecT s u m (HTMLMatcher TreeHTML a))
-> ParsecT s u m a -> ParsecT s u m (HTMLMatcher TreeHTML a)
forall a b. (a -> b) -> a -> b
$ ParsecT s u m a -> ParsecT s u m a
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (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)
match)) (String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
htmlGenParserRepeat String
elemTag Maybe (ParsecT s u m a)
match [Many (Tree (String, Attrs))]
manyElHeads)
      ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML 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 TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ((Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a)
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a)
treeElemParserSpecificContinuous Maybe (ParsecT s u m a)
match [Many (Tree (String, Attrs))]
manyElHeads)
               ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a)
-> (([Many (Tree (String, Attrs))], TreeHTML a)
    -> ParsecT s u m [HTMLMatcher TreeHTML a])
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall a b.
ParsecT s u m a -> (a -> ParsecT s u m b) -> ParsecT s u m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\([Many (Tree (String, Attrs))]
sM, TreeHTML a
a) -> ([HTMLMatcher TreeHTML a] -> [HTMLMatcher TreeHTML a])
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML 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 ((TreeHTML a -> HTMLMatcher TreeHTML a
forall (a :: * -> *) b. a b -> HTMLMatcher a b
Element TreeHTML a
a)HTMLMatcher TreeHTML a
-> [HTMLMatcher TreeHTML a] -> [HTMLMatcher TreeHTML a]
forall a. a -> [a] -> [a]
:) (String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
htmlGenParserRepeat String
elemTag Maybe (ParsecT s u m a)
match [Many (Tree (String, Attrs))]
sM)))
      -- Only allow text content that's not part of an opening tag
      ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML 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
<|> (HTMLMatcher TreeHTML a
 -> [HTMLMatcher TreeHTML a] -> [HTMLMatcher TreeHTML a])
-> ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
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 (:) ((Char -> HTMLMatcher TreeHTML a)
-> ParsecT s u m Char -> ParsecT s u m (HTMLMatcher TreeHTML 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 (String -> HTMLMatcher TreeHTML a
forall (a :: * -> *) b. String -> HTMLMatcher a b
IText (String -> HTMLMatcher TreeHTML a)
-> (Char -> String) -> Char -> HTMLMatcher TreeHTML a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> ShowS
forall a. a -> [a] -> [a]
:[])) (String -> ParsecT s u m Char
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
specificChar' String
elemTag)) (String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
htmlGenParserRepeat String
elemTag Maybe (ParsecT s u m a)
match [Many (Tree (String, Attrs))]
manyElHeads) 
      -- case parseOpeningTag 

      
      -- <|> liftA2 (:) ((IText . (:[])) <$> anyChar) (htmlGenParserRepeat elemTag match manyElHeads) 


-- | NEW IDEA!!

  -- Continue if first tree is present in second tree AND! allow for new branches
    -- NewBranch -> Optional (ParsecT s u m a) in data sig of (Many a)


specificChar :: Stream s m Char => ParsecT s u m Char
specificChar :: forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
specificChar = do
  ParsecT s u m Char -> ParsecT s u m ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy (Maybe [String]
-> [(String, Maybe String)] -> ParsecT s u m (String, Attrs)
forall s (m :: * -> *) u.
Stream s m Char =>
Maybe [String]
-> [(String, Maybe String)] -> ParsecT s u m (String, Attrs)
parseOpeningTag Maybe [String]
forall a. Maybe a
Nothing [] ParsecT s u m (String, Attrs)
-> ParsecT s u m Char -> ParsecT s u m Char
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
>> 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 () -> String -> ParsecT s u m ()
forall s u (m :: * -> *) a.
ParsecT s u m a -> String -> ParsecT s u m a
<?> String
"error on specificChar (tag found)"
  ParsecT s u m Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
anyChar 


  --  first <- anyChar
  -- if first == '<'
  --   then
  --   do 
  --     e <- option "false" (some alphaNum)
  --     attrs <- option "false" (some attrsParser)
  --     close <- option "false" (char '>')

  --     if e == "false" && e == attrs && attrs = close
  --       then return (first : [])  -- no elemHead return 
  --   -- attrs <- many attrsParser
  --   -- char '>'
  --   -- if length x == 0
  --   -- then
  --     -- return first
      
  --   -- do
  --     -- notFollowedBy $ (,) <$> many alphaNum <*> many attrParser <* char '>'
  --     -- return (first : [])
  --   else return (first : [])
    
  --   manyTill anyChar (char ' ') == 
  --   many alphaNum
  --   many attrsParser 
    
  --   elemTag : many attrsParser
  --   parserFail "
  --   else
    
  -- parseOpeningTag >> parserFail ""
  
  -- it can be any character except < if its not followedby  


-- (do { txt <- try stylingElem; return $ (IText txt):[] })

-- | treeElemParserSpecific is an interface to this (via innerParserSpecific)
-- | This inner function uses the Many datatype to differentiate between whether we should expect
-- | to parse a single element with the given specs or allow for multiple of the given element specs in a row
-- |
-- | 
treeElemParserSpecificContinuous :: (Stream s m Char, ShowHTML a) => Maybe (ParsecT s u m a)
                                 -> [Many (Tree ElemHead)] -- The total innerHtml structure the current element must have in order to match
                                 -> ParsecT s u m ([Many (Tree ElemHead)], TreeHTML a)
treeElemParserSpecificContinuous :: forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a)
treeElemParserSpecificContinuous Maybe (ParsecT s u m a)
match [Many (Tree (String, Attrs))]
manyElHeads = do
  let
    -- If we take up until the 1st (One a) then we know that it should succeed in all valid cases or fail
    elSet :: [Many (Tree ElemHead)]
    elSet :: [Many (Tree (String, Attrs))]
elSet = (Many (Tree (String, Attrs)) -> Bool)
-> [Many (Tree (String, Attrs))] -> [Many (Tree (String, Attrs))]
forall a. (a -> Bool) -> [a] -> [a]
takeTill (\case { One Tree (String, Attrs)
a -> Bool
True; Many (Tree (String, Attrs))
_ -> Bool
False}) [Many (Tree (String, Attrs))]
manyElHeads


    
  (String
e,Attrs
attrs) <- Maybe [String]
-> [(String, Maybe String)] -> ParsecT s u m (String, Attrs)
forall s (m :: * -> *) u.
Stream s m Char =>
Maybe [String]
-> [(String, Maybe String)] -> ParsecT s u m (String, Attrs)
parseOpeningTag ([String] -> Maybe [String]
forall a. a -> Maybe a
Just ([String] -> Maybe [String]) -> [String] -> Maybe [String]
forall a b. (a -> b) -> a -> b
$ (Many (Tree (String, Attrs)) -> String)
-> [Many (Tree (String, Attrs))] -> [String]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((String, Attrs) -> String
forall a b. (a, b) -> a
fst ((String, Attrs) -> String)
-> (Many (Tree (String, Attrs)) -> (String, Attrs))
-> Many (Tree (String, Attrs))
-> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tree (String, Attrs) -> (String, Attrs)
forall a. Tree a -> a
rootLabel (Tree (String, Attrs) -> (String, Attrs))
-> (Many (Tree (String, Attrs)) -> Tree (String, Attrs))
-> Many (Tree (String, Attrs))
-> (String, Attrs)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many (Tree (String, Attrs)) -> Tree (String, Attrs)
forall a. Many a -> a
fromMany) [Many (Tree (String, Attrs))]
elSet) []
      
  (Forest (String, Attrs)
innerForest, [Many (Tree (String, Attrs))]
outputStack) <- (String, Attrs)
-> [Many (Tree (String, Attrs))]
-> ParsecT
     s u m (Forest (String, Attrs), [Many (Tree (String, Attrs))])
forall s u (m :: * -> *).
(String, Attrs)
-> [Many (Tree (String, Attrs))]
-> ParsecT
     s u m (Forest (String, Attrs), [Many (Tree (String, Attrs))])
tryElHeads (String
e,Attrs
attrs) [Many (Tree (String, Attrs))]
elSet   
  --  at this point in the case where we have parsed an opening tag we have two possibilities
    --  manyElHeads = [] | x:xs
    --  [] -> then why do we have an opening tag? this should have been either an IText or the endTag
  
  ([a]
m, String
inTx, Forest (String, Attrs)
inTr) <- Maybe (ParsecT s u m a)
-> String
-> Forest (String, Attrs)
-> ParsecT s u m ([a], String, Forest (String, Attrs))
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a)
-> String
-> Forest (String, Attrs)
-> ParsecT s u m ([a], String, Forest (String, Attrs))
innerParserSpecific Maybe (ParsecT s u m a)
match String
e Forest (String, Attrs)
innerForest
  let
    manyElHeads' :: [Many (Tree ElemHead)]
    manyElHeads' :: [Many (Tree (String, Attrs))]
manyElHeads' = Int
-> [Many (Tree (String, Attrs))] -> [Many (Tree (String, Attrs))]
forall a. Int -> [a] -> [a]
drop (([Many (Tree (String, Attrs))] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Many (Tree (String, Attrs))]
manyElHeads) Int -> Int -> Int
forall a. Num a => a -> a -> a
- ([Many (Tree (String, Attrs))] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Many (Tree (String, Attrs))]
outputStack)) [Many (Tree (String, Attrs))]
manyElHeads
  ([Many (Tree (String, Attrs))], TreeHTML a)
-> ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a)
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (([Many (Tree (String, Attrs))], TreeHTML a)
 -> ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a))
-> ([Many (Tree (String, Attrs))], TreeHTML a)
-> ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a)
forall a b. (a -> b) -> a -> b
$ (,) [Many (Tree (String, Attrs))]
manyElHeads' (String
-> Attrs -> [a] -> String -> Forest (String, Attrs) -> TreeHTML a
forall a.
String
-> Attrs -> [a] -> String -> Forest (String, Attrs) -> TreeHTML a
TreeHTML String
e Attrs
attrs [a]
m String
inTx Forest (String, Attrs)
inTr)

--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------

-- | This is largely a subfunc of htmlGenParserContains 
-- | Accepts any element and if element is in the order of our checklist-of-elems, we give the tail of elems back
-- | if the tail reaches [] before we hit the end tag then we are successful 
treeElemParserContains :: (Stream s m Char, ShowHTML a) => Maybe (ParsecT s u m a)
                                 -> [Many (Tree ElemHead)]
                                 -> ParsecT s u m ([Many (Tree ElemHead)], TreeHTML a)
treeElemParserContains :: forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a)
treeElemParserContains Maybe (ParsecT s u m a)
match [Many (Tree (String, Attrs))]
manyElHeads = do
  -- (Just $ fmap (fst . rootLabel . fromMany) elSet) [] -- <|> (fmap Left $ parseOpeningTag Nothing [])
  (String
e,Attrs
ats) <- Maybe [String]
-> [(String, Maybe String)] -> ParsecT s u m (String, Attrs)
forall s (m :: * -> *) u.
Stream s m Char =>
Maybe [String]
-> [(String, Maybe String)] -> ParsecT s u m (String, Attrs)
parseOpeningTag Maybe [String]
forall a. Maybe a
Nothing []
  -- char '>'
  let
    elSet :: [Many (Tree (String, Attrs))]
elSet = (Many (Tree (String, Attrs)) -> Bool)
-> [Many (Tree (String, Attrs))] -> [Many (Tree (String, Attrs))]
forall a. (a -> Bool) -> [a] -> [a]
takeTill (\case { One Tree (String, Attrs)
a -> Bool
True; Many (Tree (String, Attrs))
_ -> Bool
False}) [Many (Tree (String, Attrs))]
manyElHeads
    -- forestNStack = tryElHeads' elAttrs elSet
  case (String, Attrs)
-> [Many (Tree (String, Attrs))]
-> Either
     String (Forest (String, Attrs), [Many (Tree (String, Attrs))])
tryElHeads' (String
e, Attrs
ats) [Many (Tree (String, Attrs))]
elSet of
    Right (Forest (String, Attrs)
innerForest, [Many (Tree (String, Attrs))]
outputStack) -> do
      let
        manyElHeads' :: [Many (Tree ElemHead)]
        manyElHeads' :: [Many (Tree (String, Attrs))]
manyElHeads' = Int
-> [Many (Tree (String, Attrs))] -> [Many (Tree (String, Attrs))]
forall a. Int -> [a] -> [a]
drop (([Many (Tree (String, Attrs))] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Many (Tree (String, Attrs))]
manyElHeads) Int -> Int -> Int
forall a. Num a => a -> a -> a
- ([Many (Tree (String, Attrs))] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Many (Tree (String, Attrs))]
outputStack)) [Many (Tree (String, Attrs))]
manyElHeads
      if Forest (String, Attrs)
innerForest Forest (String, Attrs) -> Forest (String, Attrs) -> Bool
forall a. Eq a => a -> a -> Bool
== []
        then -- this can be selfclosing
        do
          case String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem String
e [String]
selfClosing of
            Bool
True -> do
              (ParsecT s u m String -> ParsecT s u m String
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (String -> ParsecT s u m String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
">") ParsecT s u m String
-> ParsecT s u m String -> ParsecT s u m String
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
<|> String -> ParsecT s u m String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"/>")
              ([Many (Tree (String, Attrs))], TreeHTML a)
-> ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a)
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (([Many (Tree (String, Attrs))], TreeHTML a)
 -> ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a))
-> ([Many (Tree (String, Attrs))], TreeHTML a)
-> ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a)
forall a b. (a -> b) -> a -> b
$ (,) [Many (Tree (String, Attrs))]
manyElHeads' (String
-> Attrs -> [a] -> String -> Forest (String, Attrs) -> TreeHTML a
forall a.
String
-> Attrs -> [a] -> String -> Forest (String, Attrs) -> TreeHTML a
TreeHTML String
e Attrs
ats [a]
forall a. Monoid a => a
mempty String
forall a. Monoid a => a
mempty Forest (String, Attrs)
forall a. Monoid a => a
mempty)
            Bool
False -> do
              ([a]
m, String
inTx, Forest (String, Attrs)
inTr) <- Maybe (ParsecT s u m a)
-> String
-> Forest (String, Attrs)
-> ParsecT s u m ([a], String, Forest (String, Attrs))
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a)
-> String
-> Forest (String, Attrs)
-> ParsecT s u m ([a], String, Forest (String, Attrs))
innerParserContains Maybe (ParsecT s u m a)
match String
e Forest (String, Attrs)
innerForest
              ([Many (Tree (String, Attrs))], TreeHTML a)
-> ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a)
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (([Many (Tree (String, Attrs))], TreeHTML a)
 -> ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a))
-> ([Many (Tree (String, Attrs))], TreeHTML a)
-> ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a)
forall a b. (a -> b) -> a -> b
$ (,) [Many (Tree (String, Attrs))]
manyElHeads' (String
-> Attrs -> [a] -> String -> Forest (String, Attrs) -> TreeHTML a
forall a.
String
-> Attrs -> [a] -> String -> Forest (String, Attrs) -> TreeHTML a
TreeHTML String
e Attrs
ats [a]
m String
inTx Forest (String, Attrs)
inTr)
        else -- this cannot be selfClosing
        do 
          ([a]
m, String
inTx, Forest (String, Attrs)
inTr) <- Maybe (ParsecT s u m a)
-> String
-> Forest (String, Attrs)
-> ParsecT s u m ([a], String, Forest (String, Attrs))
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a)
-> String
-> Forest (String, Attrs)
-> ParsecT s u m ([a], String, Forest (String, Attrs))
innerParserContains Maybe (ParsecT s u m a)
match String
e Forest (String, Attrs)
innerForest
          ([Many (Tree (String, Attrs))], TreeHTML a)
-> ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a)
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (([Many (Tree (String, Attrs))], TreeHTML a)
 -> ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a))
-> ([Many (Tree (String, Attrs))], TreeHTML a)
-> ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a)
forall a b. (a -> b) -> a -> b
$ (,) [Many (Tree (String, Attrs))]
manyElHeads' (String
-> Attrs -> [a] -> String -> Forest (String, Attrs) -> TreeHTML a
forall a.
String
-> Attrs -> [a] -> String -> Forest (String, Attrs) -> TreeHTML a
TreeHTML String
e Attrs
ats [a]
m String
inTx Forest (String, Attrs)
inTr)

      -- string "/>" >> return (mempty, mempty, mempty) <|> string ">" >> innerParserContains 

      
    Left String
someError -> do
      (String
inText, [a]
matchBook, Forest (String, Attrs)
treees) <- String
-> Maybe (ParsecT s u m a)
-> ParsecT s u m (String, [a], Forest (String, Attrs))
forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
String
-> Maybe (ParsecT s u m a)
-> ParsecT s u m (String, [a], Forest (String, Attrs))
innerTreeElemParser String
e Maybe (ParsecT s u m a)
match
      ([Many (Tree (String, Attrs))], TreeHTML a)
-> ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a)
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (([Many (Tree (String, Attrs))], TreeHTML a)
 -> ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a))
-> ([Many (Tree (String, Attrs))], TreeHTML a)
-> ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a)
forall a b. (a -> b) -> a -> b
$ (,) [Many (Tree (String, Attrs))]
manyElHeads (String
-> Attrs -> [a] -> String -> Forest (String, Attrs) -> TreeHTML a
forall a.
String
-> Attrs -> [a] -> String -> Forest (String, Attrs) -> TreeHTML a
TreeHTML String
e Attrs
ats [a]
matchBook String
inText Forest (String, Attrs)
treees) 
    
          
   -- ParsecT s u m ([a], String, [Tree ElemHead]) 

htmlGenParserContains :: (Stream s m Char, ShowHTML a) =>
                       String 
                    -> Maybe (ParsecT s u m a)
                    -> [Many (Tree ElemHead)]
                    -> ParsecT s u m [HTMLMatcher TreeHTML a]
htmlGenParserContains :: forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
htmlGenParserContains String
elemTag Maybe (ParsecT s u m a)
match [Many (Tree (String, Attrs))]
manyElHeadss = {-parsesTreeHs-}
  case [Many (Tree (String, Attrs))]
manyElHeadss of
    [] -> do
      ([HTMLMatcher TreeHTML a]
htMMers, String
_) <-  ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m String
-> ParsecT s u m ([HTMLMatcher TreeHTML a], String)
forall s u (m :: * -> *) a end.
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m ([a], end)
manyTill_ (Maybe (ParsecT s u m a) -> ParsecT s u m (HTMLMatcher TreeHTML a)
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a) -> ParsecT s u m (HTMLMatcher TreeHTML a)
htmlGenParserFlex Maybe (ParsecT s u m a)
match) (String -> ParsecT s u m String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
endTag String
elemTag)
      [HTMLMatcher TreeHTML a] -> ParsecT s u m [HTMLMatcher TreeHTML a]
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return [HTMLMatcher TreeHTML a]
htMMers
    [Many (Tree (String, Attrs))]
manyElHeads -> 
      (HTMLMatcher TreeHTML a
 -> [HTMLMatcher TreeHTML a] -> [HTMLMatcher TreeHTML a])
-> ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
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 -> HTMLMatcher TreeHTML a)
-> ParsecT s u m a -> ParsecT s u m (HTMLMatcher TreeHTML 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 a -> HTMLMatcher TreeHTML a
forall (a :: * -> *) b. b -> HTMLMatcher a b
Match (ParsecT s u m a -> ParsecT s u m (HTMLMatcher TreeHTML a))
-> ParsecT s u m a -> ParsecT s u m (HTMLMatcher TreeHTML a)
forall a b. (a -> b) -> a -> b
$ ParsecT s u m a -> ParsecT s u m a
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (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)
match)) (String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
htmlGenParserContains String
elemTag Maybe (ParsecT s u m a)
match [Many (Tree (String, Attrs))]
manyElHeads)
      ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML 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
<|> (HTMLMatcher TreeHTML a
 -> [HTMLMatcher TreeHTML a] -> [HTMLMatcher TreeHTML a])
-> ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
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 (:) ((String -> HTMLMatcher TreeHTML a)
-> ParsecT s u m String -> ParsecT s u m (HTMLMatcher TreeHTML 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 String -> HTMLMatcher TreeHTML a
forall (a :: * -> *) b. String -> HTMLMatcher a b
IText (ParsecT s u m String -> ParsecT s u m (HTMLMatcher TreeHTML a))
-> ParsecT s u m String -> ParsecT s u m (HTMLMatcher TreeHTML a)
forall a b. (a -> b) -> a -> b
$ ParsecT s u m String -> ParsecT s u m String
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT s u m String
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m String
stylingElem) (String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
htmlGenParserContains String
elemTag Maybe (ParsecT s u m a)
match [Many (Tree (String, Attrs))]
manyElHeads) 
      ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML 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 TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ((Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a)
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a)
treeElemParserContains Maybe (ParsecT s u m a)
match [Many (Tree (String, Attrs))]
manyElHeads)
               ParsecT s u m ([Many (Tree (String, Attrs))], TreeHTML a)
-> (([Many (Tree (String, Attrs))], TreeHTML a)
    -> ParsecT s u m [HTMLMatcher TreeHTML a])
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall a b.
ParsecT s u m a -> (a -> ParsecT s u m b) -> ParsecT s u m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\([Many (Tree (String, Attrs))]
sM, TreeHTML a
a) -> ([HTMLMatcher TreeHTML a] -> [HTMLMatcher TreeHTML a])
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML 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 ((TreeHTML a -> HTMLMatcher TreeHTML a
forall (a :: * -> *) b. a b -> HTMLMatcher a b
Element TreeHTML a
a)HTMLMatcher TreeHTML a
-> [HTMLMatcher TreeHTML a] -> [HTMLMatcher TreeHTML a]
forall a. a -> [a] -> [a]
:) (String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
htmlGenParserContains String
elemTag Maybe (ParsecT s u m a)
match [Many (Tree (String, Attrs))]
sM)))
      ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML 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
<|> (HTMLMatcher TreeHTML a
 -> [HTMLMatcher TreeHTML a] -> [HTMLMatcher TreeHTML a])
-> ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
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 (:) ((Char -> HTMLMatcher TreeHTML a)
-> ParsecT s u m Char -> ParsecT s u m (HTMLMatcher TreeHTML 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 (String -> HTMLMatcher TreeHTML a
forall (a :: * -> *) b. String -> HTMLMatcher a b
IText (String -> HTMLMatcher TreeHTML a)
-> (Char -> String) -> Char -> HTMLMatcher TreeHTML a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> ShowS
forall a. a -> [a] -> [a]
:[])) (ParsecT s u m Char -> ParsecT s u m Char
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT s u m Char -> ParsecT s u m Char)
-> ParsecT s u m Char -> ParsecT s u m Char
forall a b. (a -> b) -> a -> b
$ String -> ParsecT s u m Char
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
specificChar' String
elemTag)) (String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
htmlGenParserContains String
elemTag Maybe (ParsecT s u m a)
match [Many (Tree (String, Attrs))]
manyElHeads)
      ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML 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
<|> (String -> ParsecT s u m String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
endTag String
elemTag
           ParsecT s u m String
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML 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
>> case [Many (Tree (String, Attrs))] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ((Many (Tree (String, Attrs)) -> Bool)
-> [Many (Tree (String, Attrs))] -> [Many (Tree (String, Attrs))]
forall a. (a -> Bool) -> [a] -> [a]
filter (\case {One Tree (String, Attrs)
a -> Bool
True; Many (Tree (String, Attrs))
_ -> Bool
False}) [Many (Tree (String, Attrs))]
manyElHeadss) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 of
                Bool
True -> [HTMLMatcher TreeHTML a] -> ParsecT s u m [HTMLMatcher TreeHTML a]
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return []
                Bool
False -> String -> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s u (m :: * -> *) a. String -> ParsecT s u m a
parserFail (String -> ParsecT s u m [HTMLMatcher TreeHTML a])
-> String -> ParsecT s u m [HTMLMatcher TreeHTML a]
forall a b. (a -> b) -> a -> b
$ String
"still havent yielded " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> [Many (Tree (String, Attrs))] -> String
forall a. Show a => a -> String
show [Many (Tree (String, Attrs))]
manyElHeadss) 

specificChar' :: Stream s m Char => Elem -> ParsecT s u m Char
specificChar' :: forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
specificChar' String
elemTag = do
  ParsecT s u m Char -> ParsecT s u m ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy (Maybe [String]
-> [(String, Maybe String)] -> ParsecT s u m (String, Attrs)
forall s (m :: * -> *) u.
Stream s m Char =>
Maybe [String]
-> [(String, Maybe String)] -> ParsecT s u m (String, Attrs)
parseOpeningTag Maybe [String]
forall a. Maybe a
Nothing [] ParsecT s u m (String, Attrs)
-> ParsecT s u m Char -> ParsecT s u m Char
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
>> 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 () -> String -> ParsecT s u m ()
forall s u (m :: * -> *) a.
ParsecT s u m a -> String -> ParsecT s u m a
<?> String
"error on specificChar' (tag found)"
  ParsecT s u m String -> ParsecT s u m ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy (String -> ParsecT s u m String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
endTag String
elemTag) 
  ParsecT s u m Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
anyChar 



innerParserContains :: (Stream s m Char, ShowHTML a) =>
                       Maybe (ParsecT s u m a)
                    -> Elem
                    -> SubTree ElemHead
                    -> ParsecT s u m ([a], String, [Tree ElemHead])
innerParserContains :: forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a)
-> String
-> Forest (String, Attrs)
-> ParsecT s u m ([a], String, Forest (String, Attrs))
innerParserContains Maybe (ParsecT s u m a)
match String
tag Forest (String, Attrs)
subTree =
  case String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem String
tag [String]
selfClosing of
    Bool
True -> if Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Forest (String, Attrs) -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Forest (String, Attrs)
subTree then ParsecT s u m ([a], String, Forest (String, Attrs))
forall a. HasCallStack => a
undefined else do
      (ParsecT s u m String -> ParsecT s u m String
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (String -> ParsecT s u m String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
">") ParsecT s u m String
-> ParsecT s u m String -> ParsecT s u m String
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
<|> String -> ParsecT s u m String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"/>")
      ([a], String, Forest (String, Attrs))
-> ParsecT s u m ([a], String, Forest (String, Attrs))
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return ([a]
forall a. Monoid a => a
mempty, String
forall a. Monoid a => a
mempty, Forest (String, Attrs)
forall a. Monoid a => a
mempty)
    Bool
False -> do
      Char -> ParsecT s u m Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'>'
      [HTMLMatcher TreeHTML a]
x <- String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
htmlGenParserContains String
tag Maybe (ParsecT s u m a)
match ([Many (Tree (String, Attrs))] -> [Many (Tree (String, Attrs))]
forall a. [a] -> [a]
reverse ([Many (Tree (String, Attrs))] -> [Many (Tree (String, Attrs))])
-> [Many (Tree (String, Attrs))] -> [Many (Tree (String, Attrs))]
forall a b. (a -> b) -> a -> b
$ Forest (String, Attrs)
-> [Many (Tree (String, Attrs))] -> [Many (Tree (String, Attrs))]
forall a. Eq a => [Tree a] -> [Many (Tree a)] -> [Many (Tree a)]
groupify Forest (String, Attrs)
subTree [])
      let
        -- | need to ensure all the trees are in order
        (String
inText, [a]
matchBook, Forest (String, Attrs)
treees) = (HTMLMatcher TreeHTML a
 -> (String, [a], Forest (String, Attrs))
 -> (String, [a], Forest (String, Attrs)))
-> (String, [a], Forest (String, Attrs))
-> [HTMLMatcher TreeHTML a]
-> (String, [a], Forest (String, Attrs))
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr HTMLMatcher TreeHTML a
-> (String, [a], Forest (String, Attrs))
-> (String, [a], Forest (String, Attrs))
forall a.
ShowHTML a =>
HTMLMatcher TreeHTML a
-> (String, [a], Forest (String, Attrs))
-> (String, [a], Forest (String, Attrs))
foldFuncTrup (String, [a], Forest (String, Attrs))
forall a. Monoid a => a
mempty ([HTMLMatcher TreeHTML a]
x)
      ([a], String, Forest (String, Attrs))
-> ParsecT s u m ([a], String, Forest (String, Attrs))
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return ([a]
matchBook, String
inText, Forest (String, Attrs)
treees)

-- | Very similar to treeElemParserSpecific except that it allows for a new nodes in the HTML DOM tree
-- | to exist at random as long as when we resume parsing we still find all of the branches we found in the
-- | TreeHTML a that is given as an arg to this function
similarTreeH :: (Stream s m Char, ShowHTML a)
             => Maybe (ParsecT s u m a)
             -> TreeHTML a
             -> ParsecT s u m (TreeHTML a)
similarTreeH :: forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a) -> TreeHTML a -> ParsecT s u m (TreeHTML a)
similarTreeH Maybe (ParsecT s u m a)
matchh TreeHTML a
treeH = do
  (String
e,Attrs
at) <- Maybe [String]
-> [(String, Maybe String)] -> ParsecT s u m (String, Attrs)
forall s (m :: * -> *) u.
Stream s m Char =>
Maybe [String]
-> [(String, Maybe String)] -> ParsecT s u m (String, Attrs)
parseOpeningTag ([String] -> Maybe [String]
forall a. a -> Maybe a
Just ([String] -> Maybe [String]) -> [String] -> Maybe [String]
forall a b. (a -> b) -> a -> b
$ [TreeHTML a -> String
forall b. TreeHTML b -> String
forall (a :: * -> *) b. ElementRep a => a b -> String
elTag TreeHTML a
treeH]) (((((String, String) -> (String, Maybe String))
-> [(String, String)] -> [(String, Maybe String)]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (((String, String) -> (String, Maybe String))
 -> [(String, String)] -> [(String, Maybe String)])
-> ((String -> Maybe String)
    -> (String, String) -> (String, Maybe String))
-> (String -> Maybe String)
-> [(String, String)]
-> [(String, Maybe String)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> Maybe String)
-> (String, String) -> (String, Maybe String)
forall a b. (a -> b) -> (String, a) -> (String, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) String -> Maybe String
forall a. a -> Maybe a
Just) ([(String, String)] -> [(String, Maybe String)])
-> (Attrs -> [(String, String)])
-> Attrs
-> [(String, Maybe String)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Attrs -> [(String, String)]
forall k a. Map k a -> [(k, a)]
Map.toList (Attrs -> [(String, Maybe String)])
-> Attrs -> [(String, Maybe String)]
forall a b. (a -> b) -> a -> b
$ TreeHTML a -> Attrs
forall b. TreeHTML b -> Attrs
forall (a :: * -> *) b. ElementRep a => a b -> Attrs
attrs TreeHTML a
treeH)
  Char -> ParsecT s u m Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'>'
  (String
inTx, [a]
m, Forest (String, Attrs)
inTr) <-
    ([HTMLMatcher TreeHTML a] -> (String, [a], Forest (String, Attrs)))
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m (String, [a], Forest (String, Attrs))
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 TreeHTML a
 -> (String, [a], Forest (String, Attrs))
 -> (String, [a], Forest (String, Attrs)))
-> (String, [a], Forest (String, Attrs))
-> [HTMLMatcher TreeHTML a]
-> (String, [a], Forest (String, Attrs))
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr HTMLMatcher TreeHTML a
-> (String, [a], Forest (String, Attrs))
-> (String, [a], Forest (String, Attrs))
forall a.
ShowHTML a =>
HTMLMatcher TreeHTML a
-> (String, [a], Forest (String, Attrs))
-> (String, [a], Forest (String, Attrs))
foldFuncTrup (String, [a], Forest (String, Attrs))
forall a. Monoid a => a
mempty) (String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
htmlGenParserContains String
e Maybe (ParsecT s u m a)
matchh (Forest (String, Attrs)
-> [Many (Tree (String, Attrs))] -> [Many (Tree (String, Attrs))]
forall a. Eq a => [Tree a] -> [Many (Tree a)] -> [Many (Tree a)]
groupify (TreeHTML a -> Forest (String, Attrs)
forall a. TreeHTML a -> Forest (String, Attrs)
_innerTree' TreeHTML a
treeH) []))
  TreeHTML a -> ParsecT s u m (TreeHTML a)
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (TreeHTML a -> ParsecT s u m (TreeHTML a))
-> TreeHTML a -> ParsecT s u m (TreeHTML a)
forall a b. (a -> b) -> a -> b
$ String
-> Attrs -> [a] -> String -> Forest (String, Attrs) -> TreeHTML a
forall a.
String
-> Attrs -> [a] -> String -> Forest (String, Attrs) -> TreeHTML a
TreeHTML String
e Attrs
at [a]
m String
inTx Forest (String, Attrs)
inTr
  
  -- treeElemParserContains matchh (elTag treeH) (Map.toList $ attrs treeH) (_innerTree' treeH)
  
-- treeElemParserContains :: (Stream s m Char, ShowHTML a) => Maybe (ParsecT s u m a)
                                 -- -> [Many (Tree ElemHead)]
                                 -- -> ParsecT s u m ([Many (Tree ElemHead)], TreeHTML a)

-- | Returns an entire group of highly similar elements based on their specifications such
-- | as their innerTrees, the element tag, and attributes.
-- |
-- | This can be used to autonomously determine the structure of and find search result items after you've submitted a form
-- htmlGroupSimilar :: (Stream s m Char, ShowHTML a)
--                  => Maybe [Elem]
--                  ->  Maybe (ParsecT s u m a)
--                  -> [(String, Maybe String)]
--                  -> ParsecT s u m (GroupHtml TreeHTML a)
-- htmlGroupSimilar elemOpts matchh attrsSubset = 
--   -- Not sure about the order yet tho
--   fmap mkGH $ try (treeElemParser elemOpts matchh attrsSubset
--                    >>= (\treeH -> fmap (treeH :) (some (try $ similarTreeH matchh treeH))))

-- I could rewrite groups to become more versatile by allowing optionally
-- any number of '\n' after parsing a similar tree 


-- some (try $ similarTreeH matchh treeH) <* optional (many $ char '\n')



htmlGroupSimilar :: (Stream s m Char, ShowHTML a)
                 => Maybe [Elem]
                 ->  Maybe (ParsecT s u m a)
                 -> [(String, Maybe String)]
                 -> ParsecT s u m (GroupHtml TreeHTML a)
htmlGroupSimilar :: forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe [String]
-> Maybe (ParsecT s u m a)
-> [(String, Maybe String)]
-> ParsecT s u m (GroupHtml TreeHTML a)
htmlGroupSimilar Maybe [String]
elemOpts Maybe (ParsecT s u m a)
matchh [(String, Maybe String)]
attrsSubset = 
  -- Not sure about the order yet tho
  ([TreeHTML a] -> GroupHtml TreeHTML a)
-> ParsecT s u m [TreeHTML a]
-> ParsecT s u m (GroupHtml TreeHTML 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 [TreeHTML a] -> GroupHtml TreeHTML a
forall (e :: * -> *) a. ElementRep e => [e a] -> GroupHtml e a
mkGH (ParsecT s u m [TreeHTML a]
 -> ParsecT s u m (GroupHtml TreeHTML a))
-> ParsecT s u m [TreeHTML a]
-> ParsecT s u m (GroupHtml TreeHTML a)
forall a b. (a -> b) -> a -> b
$ (do
                 
                  TreeHTML a
treeH <- Maybe [String]
-> Maybe (ParsecT s u m a)
-> [(String, Maybe String)]
-> ParsecT s u m (TreeHTML a)
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe [String]
-> Maybe (ParsecT s u m a)
-> [(String, Maybe String)]
-> ParsecT s u m (TreeHTML a)
treeElemParser Maybe [String]
elemOpts Maybe (ParsecT s u m a)
matchh [(String, Maybe String)]
attrsSubset ParsecT s u m (TreeHTML a)
-> ParsecT s u m () -> ParsecT s u m (TreeHTML a)
forall a b. ParsecT s u m a -> ParsecT s u m b -> ParsecT s u m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT s u m ()
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m ()
nl 
                  [TreeHTML a]
treeHs <- ParsecT s u m (TreeHTML a) -> ParsecT s u m [TreeHTML a]
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 (TreeHTML a) -> ParsecT s u m [TreeHTML a])
-> ParsecT s u m (TreeHTML a) -> ParsecT s u m [TreeHTML a]
forall a b. (a -> b) -> a -> b
$ ParsecT s u m (TreeHTML a) -> ParsecT s u m (TreeHTML a)
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT s u m (TreeHTML a) -> ParsecT s u m (TreeHTML a))
-> ParsecT s u m (TreeHTML a) -> ParsecT s u m (TreeHTML a)
forall a b. (a -> b) -> a -> b
$ Maybe (ParsecT s u m a) -> TreeHTML a -> ParsecT s u m (TreeHTML a)
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a) -> TreeHTML a -> ParsecT s u m (TreeHTML a)
similarTreeH Maybe (ParsecT s u m a)
matchh TreeHTML a
treeH ParsecT s u m (TreeHTML a)
-> ParsecT s u m () -> ParsecT s u m (TreeHTML a)
forall a b. ParsecT s u m a -> ParsecT s u m b -> ParsecT s u m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT s u m ()
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m ()
nl
                  [TreeHTML a] -> ParsecT s u m [TreeHTML a]
forall a. a -> ParsecT s u m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([TreeHTML a] -> ParsecT s u m [TreeHTML a])
-> [TreeHTML a] -> ParsecT s u m [TreeHTML a]
forall a b. (a -> b) -> a -> b
$ TreeHTML a
treeH TreeHTML a -> [TreeHTML a] -> [TreeHTML a]
forall a. a -> [a] -> [a]
: [TreeHTML a]
treeHs 
              )


--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
takeTill :: (a -> Bool) -> [a] -> [a]
takeTill :: forall a. (a -> Bool) -> [a] -> [a]
takeTill a -> Bool
_ [] = []
takeTill a -> Bool
f (a
x:[a]
xs) = if a -> Bool
f a
x then a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[] else a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
takeTill a -> Bool
f [a]
xs 

-- | yields how many are still worth trying
tryElHeads :: (Elem, Attrs)
           -> [Many (Tree ElemHead)]
           -> ParsecT s u m ([Tree ElemHead], [Many (Tree ElemHead)])
tryElHeads :: forall s u (m :: * -> *).
(String, Attrs)
-> [Many (Tree (String, Attrs))]
-> ParsecT
     s u m (Forest (String, Attrs), [Many (Tree (String, Attrs))])
tryElHeads (String, Attrs)
_ [] = String
-> ParsecT
     s u m (Forest (String, Attrs), [Many (Tree (String, Attrs))])
forall s u (m :: * -> *) a. String -> ParsecT s u m a
parserFail String
"none of me opening tags worked laddy" 
tryElHeads (String, Attrs)
tagAttrs ((Many (Node (String, Attrs)
label Forest (String, Attrs)
forest)):[Many (Tree (String, Attrs))]
outputStack) =
  if (String, Attrs)
tagAttrs (String, Attrs) -> (String, Attrs) -> Bool
forall a. Eq a => a -> a -> Bool
== (String, Attrs)
label
  then (Forest (String, Attrs), [Many (Tree (String, Attrs))])
-> ParsecT
     s u m (Forest (String, Attrs), [Many (Tree (String, Attrs))])
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return ((Forest (String, Attrs), [Many (Tree (String, Attrs))])
 -> ParsecT
      s u m (Forest (String, Attrs), [Many (Tree (String, Attrs))]))
-> (Forest (String, Attrs), [Many (Tree (String, Attrs))])
-> ParsecT
     s u m (Forest (String, Attrs), [Many (Tree (String, Attrs))])
forall a b. (a -> b) -> a -> b
$ (Forest (String, Attrs)
forest, (Tree (String, Attrs) -> Many (Tree (String, Attrs))
forall a. a -> Many a
Many ((String, Attrs) -> Forest (String, Attrs) -> Tree (String, Attrs)
forall a. a -> [Tree a] -> Tree a
Node (String, Attrs)
label Forest (String, Attrs)
forest))Many (Tree (String, Attrs))
-> [Many (Tree (String, Attrs))] -> [Many (Tree (String, Attrs))]
forall a. a -> [a] -> [a]
:[Many (Tree (String, Attrs))]
outputStack) -- added back to stack 
  else (String, Attrs)
-> [Many (Tree (String, Attrs))]
-> ParsecT
     s u m (Forest (String, Attrs), [Many (Tree (String, Attrs))])
forall s u (m :: * -> *).
(String, Attrs)
-> [Many (Tree (String, Attrs))]
-> ParsecT
     s u m (Forest (String, Attrs), [Many (Tree (String, Attrs))])
tryElHeads (String, Attrs)
tagAttrs [Many (Tree (String, Attrs))]
outputStack -- deleted from temp stack if not found 
tryElHeads (String, Attrs)
tagAttrs ((One (Node (String, Attrs)
label Forest (String, Attrs)
forest)):[Many (Tree (String, Attrs))]
outputStack) =
  if (String, Attrs)
tagAttrs (String, Attrs) -> (String, Attrs) -> Bool
forall a. Eq a => a -> a -> Bool
== (String, Attrs)
label
  then (Forest (String, Attrs), [Many (Tree (String, Attrs))])
-> ParsecT
     s u m (Forest (String, Attrs), [Many (Tree (String, Attrs))])
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return ((Forest (String, Attrs), [Many (Tree (String, Attrs))])
 -> ParsecT
      s u m (Forest (String, Attrs), [Many (Tree (String, Attrs))]))
-> (Forest (String, Attrs), [Many (Tree (String, Attrs))])
-> ParsecT
     s u m (Forest (String, Attrs), [Many (Tree (String, Attrs))])
forall a b. (a -> b) -> a -> b
$ (Forest (String, Attrs)
forest, [Many (Tree (String, Attrs))]
outputStack)
  else String
-> ParsecT
     s u m (Forest (String, Attrs), [Many (Tree (String, Attrs))])
forall s u (m :: * -> *) a. String -> ParsecT s u m a
parserFail (String
 -> ParsecT
      s u m (Forest (String, Attrs), [Many (Tree (String, Attrs))]))
-> String
-> ParsecT
     s u m (Forest (String, Attrs), [Many (Tree (String, Attrs))])
forall a b. (a -> b) -> a -> b
$ String
"missing element" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> (String, Attrs) -> String
forall a. Show a => a -> String
show (String, Attrs)
label 

tryElHeads' :: (Elem, Attrs)
           -> [Many (Tree ElemHead)]
           -> Either String ([Tree ElemHead], [Many (Tree ElemHead)])
tryElHeads' :: (String, Attrs)
-> [Many (Tree (String, Attrs))]
-> Either
     String (Forest (String, Attrs), [Many (Tree (String, Attrs))])
tryElHeads' (String, Attrs)
_ [] = String
-> Either
     String (Forest (String, Attrs), [Many (Tree (String, Attrs))])
forall a b. a -> Either a b
Left String
"none of me opening tags worked laddy" 
tryElHeads' (String, Attrs)
tagAttrs ((Many (Node (String, Attrs)
label Forest (String, Attrs)
forest)):[Many (Tree (String, Attrs))]
outputStack) =
  if (String, Attrs)
tagAttrs (String, Attrs) -> (String, Attrs) -> Bool
forall a. Eq a => a -> a -> Bool
== (String, Attrs)
label
  then (Forest (String, Attrs), [Many (Tree (String, Attrs))])
-> Either
     String (Forest (String, Attrs), [Many (Tree (String, Attrs))])
forall a. a -> Either String a
forall (m :: * -> *) a. Monad m => a -> m a
return ((Forest (String, Attrs), [Many (Tree (String, Attrs))])
 -> Either
      String (Forest (String, Attrs), [Many (Tree (String, Attrs))]))
-> (Forest (String, Attrs), [Many (Tree (String, Attrs))])
-> Either
     String (Forest (String, Attrs), [Many (Tree (String, Attrs))])
forall a b. (a -> b) -> a -> b
$ (Forest (String, Attrs)
forest, (Tree (String, Attrs) -> Many (Tree (String, Attrs))
forall a. a -> Many a
Many ((String, Attrs) -> Forest (String, Attrs) -> Tree (String, Attrs)
forall a. a -> [Tree a] -> Tree a
Node (String, Attrs)
label Forest (String, Attrs)
forest))Many (Tree (String, Attrs))
-> [Many (Tree (String, Attrs))] -> [Many (Tree (String, Attrs))]
forall a. a -> [a] -> [a]
:[Many (Tree (String, Attrs))]
outputStack) -- added back to stack 
  else (String, Attrs)
-> [Many (Tree (String, Attrs))]
-> Either
     String (Forest (String, Attrs), [Many (Tree (String, Attrs))])
tryElHeads' (String, Attrs)
tagAttrs [Many (Tree (String, Attrs))]
outputStack -- deleted from temp stack if not found 
tryElHeads' (String, Attrs)
tagAttrs ((One (Node (String, Attrs)
label Forest (String, Attrs)
forest)):[Many (Tree (String, Attrs))]
outputStack) =
  if (String, Attrs)
tagAttrs (String, Attrs) -> (String, Attrs) -> Bool
forall a. Eq a => a -> a -> Bool
== (String, Attrs)
label
  then (Forest (String, Attrs), [Many (Tree (String, Attrs))])
-> Either
     String (Forest (String, Attrs), [Many (Tree (String, Attrs))])
forall a. a -> Either String a
forall (m :: * -> *) a. Monad m => a -> m a
return ((Forest (String, Attrs), [Many (Tree (String, Attrs))])
 -> Either
      String (Forest (String, Attrs), [Many (Tree (String, Attrs))]))
-> (Forest (String, Attrs), [Many (Tree (String, Attrs))])
-> Either
     String (Forest (String, Attrs), [Many (Tree (String, Attrs))])
forall a b. (a -> b) -> a -> b
$ (Forest (String, Attrs)
forest, [Many (Tree (String, Attrs))]
outputStack)
  else String
-> Either
     String (Forest (String, Attrs), [Many (Tree (String, Attrs))])
forall a b. a -> Either a b
Left (String
 -> Either
      String (Forest (String, Attrs), [Many (Tree (String, Attrs))]))
-> String
-> Either
     String (Forest (String, Attrs), [Many (Tree (String, Attrs))])
forall a b. (a -> b) -> a -> b
$ String
"missing element"   String -> ShowS
forall a. Semigroup a => a -> a -> a
<> (String, Attrs) -> String
forall a. Show a => a -> String
show (String, Attrs)
label -- I believe Left "missing element"



innerParserSpecific :: (Stream s m Char, ShowHTML a) =>
                       Maybe (ParsecT s u m a)
                    -> Elem
                    -> SubTree ElemHead
                    -> ParsecT s u m ([a], String, [Tree ElemHead]) 
innerParserSpecific :: forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a)
-> String
-> Forest (String, Attrs)
-> ParsecT s u m ([a], String, Forest (String, Attrs))
innerParserSpecific Maybe (ParsecT s u m a)
match String
tag Forest (String, Attrs)
subTree =
  case String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem String
tag [String]
selfClosing of
    Bool
True -> if Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Forest (String, Attrs) -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Forest (String, Attrs)
subTree then ParsecT s u m ([a], String, Forest (String, Attrs))
forall a. HasCallStack => a
undefined else do
      (ParsecT s u m String -> ParsecT s u m String
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (String -> ParsecT s u m String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
">") ParsecT s u m String
-> ParsecT s u m String -> ParsecT s u m String
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
<|> String -> ParsecT s u m String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"/>")
      ([a], String, Forest (String, Attrs))
-> ParsecT s u m ([a], String, Forest (String, Attrs))
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return ([a]
forall a. Monoid a => a
mempty, String
forall a. Monoid a => a
mempty, Forest (String, Attrs)
forall a. Monoid a => a
mempty)
    Bool
False -> do 
      -- char '>'
      [HTMLMatcher TreeHTML a]
x <- String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
String
-> Maybe (ParsecT s u m a)
-> [Many (Tree (String, Attrs))]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
htmlGenParserRepeat String
tag Maybe (ParsecT s u m a)
match ([Many (Tree (String, Attrs))] -> [Many (Tree (String, Attrs))]
forall a. [a] -> [a]
reverse ([Many (Tree (String, Attrs))] -> [Many (Tree (String, Attrs))])
-> [Many (Tree (String, Attrs))] -> [Many (Tree (String, Attrs))]
forall a b. (a -> b) -> a -> b
$ Forest (String, Attrs)
-> [Many (Tree (String, Attrs))] -> [Many (Tree (String, Attrs))]
forall a. Eq a => [Tree a] -> [Many (Tree a)] -> [Many (Tree a)]
groupify Forest (String, Attrs)
subTree [])
      let
        -- | need to ensure all the trees are in order 
        (String
inText, [a]
matchBook, Forest (String, Attrs)
treees) = (HTMLMatcher TreeHTML a
 -> (String, [a], Forest (String, Attrs))
 -> (String, [a], Forest (String, Attrs)))
-> (String, [a], Forest (String, Attrs))
-> [HTMLMatcher TreeHTML a]
-> (String, [a], Forest (String, Attrs))
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr HTMLMatcher TreeHTML a
-> (String, [a], Forest (String, Attrs))
-> (String, [a], Forest (String, Attrs))
forall a.
ShowHTML a =>
HTMLMatcher TreeHTML a
-> (String, [a], Forest (String, Attrs))
-> (String, [a], Forest (String, Attrs))
foldFuncTrup (String, [a], Forest (String, Attrs))
forall a. Monoid a => a
mempty ([HTMLMatcher TreeHTML a]
x)
      ([a], String, Forest (String, Attrs))
-> ParsecT s u m ([a], String, Forest (String, Attrs))
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return ([a]
matchBook, String
inText, Forest (String, Attrs)
treees)





-- currentElHead :: Stream s m Char =>
--                  ParsecT s u m a
--               -> [Many (Tree ElemHead)]
--               -> ParsecT s u m [HTMLMatcher TreeHTML a]
-- currentElHead = do
--   (m, inTx, inTr) <- innerParserSpecific mElHead1 match elem 
--   (:) <$> (TreeHTML e at m inTx inTr) <*> htmlGenParserRepeat match (mElHead1:mElHead2:manyElHeads)



-- treeElemParserSpecificContinuous match (manyElHeads) = do
--   (eIns, at)  <- parseOpeningTagDesc (Just [(fst mElHead1), (fst mEleHead2)]) []
--   if attrs e == snd mElHead1 
--     then currentElHead match manyElHeads 
--     else nextElHead match manyElHeads <|> parserFail "did not match on either of the next two previous elements" 
--     if attrs e == snd mElHead2     -- should also have this or similar behaviour in case (One a) 
--     then
--       do
--         (matches, inText, inTree) <- innerParserSpecific mElHead2 match eIns
--         (:[]) <$> (TreeHTML e at matches inText inTree) <*> htmlGenParserRepeat match (mElHead2:manyElHeads)
--     else parserFail "did not match on either of the next two previous elements" 
--          -- case e1,e2,parserFail

      
      -- x <- specificRepetitiveForest (reverse $ groupify subTree []) (fromMaybe parserZero matchh)
      -- ---
      -- -- Everything here is to avoid false positives from being too
      -- -- general while developing 
      -- option "" (string "Hockey")
      -- many space
      -- option "" (string "tour case suing ")
      -- ---
      -- endTag tag
      -- --can be followed by whatever: 
      -- -- (y, _) <- manyTill_ (htmlGenParserFlex matchh) (endTag tag) 
      -- let
      --   -- | need to ensure all the trees are in order 
      --   (inText, matchBook, treees) = foldr foldFuncTrup mempty (x)
      -- return $ TreeHTML tag attrsOut matchBook (reverse inText) (reverse treees)--(_matches itr) (_innerText itr) (innerTree itr)


-- | IS THIS IN THE RIGHT ORDER OR DOES IT NEED TO BE REVERSED?
-- | Creates a simplified set of instructions for parsing a very specific Tree structure 
groupify :: Eq a => [Tree a] -> [Many (Tree a)] -> [Many (Tree a)]
groupify :: forall a. Eq a => [Tree a] -> [Many (Tree a)] -> [Many (Tree a)]
groupify [] [Many (Tree a)]
acc = [Many (Tree a)]
acc
groupify (Tree a
tree:[Tree a]
forest) [] = [Tree a] -> [Many (Tree a)] -> [Many (Tree a)]
forall a. Eq a => [Tree a] -> [Many (Tree a)] -> [Many (Tree a)]
groupify [Tree a]
forest (Tree a -> Many (Tree a)
forall a. a -> Many a
One Tree a
treeMany (Tree a) -> [Many (Tree a)] -> [Many (Tree a)]
forall a. a -> [a] -> [a]
:[])
groupify ((Node a
elemHead [Tree a]
subForest):[Tree a]
forest) (Many (Tree a)
mTree:[Many (Tree a)]
acc) =
  case Many (Tree a)
mTree of
    One (Node a
elemHeadPrev [Tree a]
subForestPrev) ->
      if a
elemHead a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
elemHeadPrev
      -- here is maybe where I could add in checking if forests are equal ? 
      then [Tree a] -> [Many (Tree a)] -> [Many (Tree a)]
forall a. Eq a => [Tree a] -> [Many (Tree a)] -> [Many (Tree a)]
groupify [Tree a]
forest ([Many (Tree a)] -> [Many (Tree a)])
-> [Many (Tree a)] -> [Many (Tree a)]
forall a b. (a -> b) -> a -> b
$ ((Tree a -> Many (Tree a)
forall a. a -> Many a
Many (a -> [Tree a] -> Tree a
forall a. a -> [Tree a] -> Tree a
Node a
elemHeadPrev [Tree a]
subForestPrev))Many (Tree a) -> [Many (Tree a)] -> [Many (Tree a)]
forall a. a -> [a] -> [a]
:[Many (Tree a)]
acc)
      else [Tree a] -> [Many (Tree a)] -> [Many (Tree a)]
forall a. Eq a => [Tree a] -> [Many (Tree a)] -> [Many (Tree a)]
groupify [Tree a]
forest ([Many (Tree a)] -> [Many (Tree a)])
-> [Many (Tree a)] -> [Many (Tree a)]
forall a b. (a -> b) -> a -> b
$ ((Tree a -> Many (Tree a)
forall a. a -> Many a
One (a -> [Tree a] -> Tree a
forall a. a -> [Tree a] -> Tree a
Node a
elemHead [Tree a]
subForest)) Many (Tree a) -> [Many (Tree a)] -> [Many (Tree a)]
forall a. a -> [a] -> [a]
: (Tree a -> Many (Tree a)
forall a. a -> Many a
One (a -> [Tree a] -> Tree a
forall a. a -> [Tree a] -> Tree a
Node a
elemHeadPrev [Tree a]
subForestPrev)) Many (Tree a) -> [Many (Tree a)] -> [Many (Tree a)]
forall a. a -> [a] -> [a]
: [Many (Tree a)]
acc)
      --  we want to ensure then that this One constructor isn't touched again, we need to create
      --  another One constructor with the incoming `tree` that was peeled off
    Many (Node a
elemHeadPrev [Tree a]
subForestPrev) ->
      if a
elemHead a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
elemHeadPrev
      then [Tree a] -> [Many (Tree a)] -> [Many (Tree a)]
forall a. Eq a => [Tree a] -> [Many (Tree a)] -> [Many (Tree a)]
groupify [Tree a]
forest ((Tree a -> Many (Tree a)
forall a. a -> Many a
Many (a -> [Tree a] -> Tree a
forall a. a -> [Tree a] -> Tree a
Node a
elemHeadPrev [Tree a]
subForestPrev))Many (Tree a) -> [Many (Tree a)] -> [Many (Tree a)]
forall a. a -> [a] -> [a]
:[Many (Tree a)]
acc)
      else [Tree a] -> [Many (Tree a)] -> [Many (Tree a)]
forall a. Eq a => [Tree a] -> [Many (Tree a)] -> [Many (Tree a)]
groupify [Tree a]
forest ((Tree a -> Many (Tree a)
forall a. a -> Many a
One (a -> [Tree a] -> Tree a
forall a. a -> [Tree a] -> Tree a
Node a
elemHead [Tree a]
subForest))
                            Many (Tree a) -> [Many (Tree a)] -> [Many (Tree a)]
forall a. a -> [a] -> [a]
: (Tree a -> Many (Tree a)
forall a. a -> Many a
Many (a -> [Tree a] -> Tree a
forall a. a -> [Tree a] -> Tree a
Node a
elemHeadPrev [Tree a]
subForestPrev))
                            Many (Tree a) -> [Many (Tree a)] -> [Many (Tree a)]
forall a. a -> [a] -> [a]
: [Many (Tree a)]
acc)



-------------------------------------------------------------------------------------------------------------------


-- | Returns a minimum of 2 --> almost like `same` should be function ; same :: a -> [a] to be applied to some doc/String
-- | note: not sure if this exists but here's where we could handle iterating names of attributes 
-- | Can generalize to ElementRep e
htmlGroup  :: (Stream s m Char, ShowHTML a)
               => Maybe [Elem]
               ->  Maybe (ParsecT s u m a)
               -> [(String, Maybe String)]
               -> ParsecT s u m (GroupHtml TreeHTML a)
htmlGroup :: forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe [String]
-> Maybe (ParsecT s u m a)
-> [(String, Maybe String)]
-> ParsecT s u m (GroupHtml TreeHTML a)
htmlGroup Maybe [String]
elemOpts Maybe (ParsecT s u m a)
matchh [(String, Maybe String)]
attrsSubset = 
  -- Not sure about the order yet tho
  ([TreeHTML a] -> GroupHtml TreeHTML a)
-> ParsecT s u m [TreeHTML a]
-> ParsecT s u m (GroupHtml TreeHTML 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 [TreeHTML a] -> GroupHtml TreeHTML a
forall (e :: * -> *) a. ElementRep e => [e a] -> GroupHtml e a
mkGH (ParsecT s u m [TreeHTML a]
 -> ParsecT s u m (GroupHtml TreeHTML a))
-> ParsecT s u m [TreeHTML a]
-> ParsecT s u m (GroupHtml TreeHTML a)
forall a b. (a -> b) -> a -> b
$ ParsecT s u m [TreeHTML a] -> ParsecT s u m [TreeHTML a]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (Maybe [String]
-> Maybe (ParsecT s u m a)
-> [(String, Maybe String)]
-> ParsecT s u m (TreeHTML a)
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe [String]
-> Maybe (ParsecT s u m a)
-> [(String, Maybe String)]
-> ParsecT s u m (TreeHTML a)
treeElemParser Maybe [String]
elemOpts Maybe (ParsecT s u m a)
matchh [(String, Maybe String)]
attrsSubset
                   ParsecT s u m (TreeHTML a)
-> (TreeHTML a -> ParsecT s u m [TreeHTML a])
-> ParsecT s u m [TreeHTML a]
forall a b.
ParsecT s u m a -> (a -> ParsecT s u m b) -> ParsecT s u m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\TreeHTML a
treeH -> ([TreeHTML a] -> [TreeHTML a])
-> ParsecT s u m [TreeHTML a] -> ParsecT s u m [TreeHTML 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 (TreeHTML a
treeH TreeHTML a -> [TreeHTML a] -> [TreeHTML a]
forall a. a -> [a] -> [a]
:) (ParsecT s u m (TreeHTML a) -> ParsecT s u m [TreeHTML a]
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 (TreeHTML a) -> ParsecT s u m (TreeHTML a)
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT s u m (TreeHTML a) -> ParsecT s u m (TreeHTML a))
-> ParsecT s u m (TreeHTML a) -> ParsecT s u m (TreeHTML a)
forall a b. (a -> b) -> a -> b
$ Maybe (ParsecT s u m a) -> TreeHTML a -> ParsecT s u m (TreeHTML a)
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a) -> TreeHTML a -> ParsecT s u m (TreeHTML a)
sameTreeH Maybe (ParsecT s u m a)
matchh TreeHTML a
treeH))))

-- | Html table group   
table :: Stream s m Char => ParsecT s u m (GroupHtml TreeHTML String) 
table :: forall s (m :: * -> *) u.
Stream s m Char =>
ParsecT s u m (GroupHtml TreeHTML String)
table = Maybe [String]
-> Maybe (ParsecT s u m String)
-> [(String, Maybe String)]
-> ParsecT s u m (GroupHtml TreeHTML String)
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe [String]
-> Maybe (ParsecT s u m a)
-> [(String, Maybe String)]
-> ParsecT s u m (GroupHtml TreeHTML a)
htmlGroup ([String] -> Maybe [String]
forall a. a -> Maybe a
Just [String
"tr"]) Maybe (ParsecT s u m String)
forall a. Maybe a
Nothing []





-- test for

-- Previous Element of (if Many) :: Many a
-- Next element of Many a
-- Match?
-- IText String
  
  -- until endTag 


-- | Build [Many (Tree ElemHead)]
-- | Write parser that tries each case OR parses openingTag and then decides what case it fits
  -- | For set (Previous, Next) if Next is True then delete Previous parser
  -- |    Next becomes "Previous" in future equation(s)
  
-- | Fail onto plain IText (of parent element that the parser is currently in)




--------------------------------------------------------------------------------------------------------------------
---Generalizations----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


-- | Inner parser of treeElemParserSpecific 
-- specificRepetitiveForest :: (Stream s m Char, ShowHTML a)
--                          => [Many (Tree ElemHead)]
--                          -> ParsecT s u m a
--                          -> ParsecT s u m [HTMLMatcher TreeHTML a]
-- specificRepetitiveForest [] _ = return []
-- specificRepetitiveForest (mElHead1:mElHead2:manyElHeads) match = do
--   -- | ysA could be == []
--   htmlGenParserRepeat match (manyElHeads) -- NEW 
--   ysA {-maybeNext-} <- multiTreeElemHeadParser match mElHead2
--   ysB <- case ysA of
--            Just a -> specificRepetitiveForest (mElHead2:manyElHeads) match 
--            Nothing ->
--              -- WHat happens to the rest of Many ElHeads in this case? 
--              htmlGenParserRepeat match (multiTreeElemHeadParser match mElHead1)
--              htmlGenParserRepeat match (manyElHeads) -- NEW 
--   return (ysA <> ysB)
  -- let
  --   -- funcP :: (ShowHTML a, Stream s m Char) => ParsecT s u m [TreeHTML a]
  --   funcP = multiTreeElemHeadParser match mElHead1 
  -- y <- htmlGenParserRepeat match funcP -- this literally just allows for matching on multiple elems too
  -- -- Only applies to "specific" functions, prev: any case 
  -- ys <- case y of
  --   -- Discard last parsed pattern and go to next element formula on success of `y` 
  --   ((Element _):xs') -> specificRepetitiveForest manyElHeads match
  --   _                -> specificRepetitiveForest (manyElHead:manyElHeads) match
  -- -- return all results 






-- | This is all I actually need , no need for recursion here, since thats already done in top level func
{-# DEPRECATED multiTreeElemHeadParser "use specificContinuous style functions" #-} 
multiTreeElemHeadParser :: (Stream s m Char, ShowHTML a) =>
                          ParsecT s u m a
                       -> Many (Tree ElemHead)
                       -> ParsecT s u m [HTMLMatcher TreeHTML a]
multiTreeElemHeadParser :: forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
ParsecT s u m a
-> Many (Tree (String, Attrs))
-> ParsecT s u m [HTMLMatcher TreeHTML a]
multiTreeElemHeadParser ParsecT s u m a
match Many (Tree (String, Attrs))
mTree = case Many (Tree (String, Attrs))
mTree of
  Many (Node (String
elem, Attrs
attrs) Forest (String, Attrs)
subTree) ->
    (([TreeHTML a] -> [HTMLMatcher TreeHTML a])
-> ParsecT s u m [TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML 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 (([TreeHTML a] -> [HTMLMatcher TreeHTML a])
 -> ParsecT s u m [TreeHTML a]
 -> ParsecT s u m [HTMLMatcher TreeHTML a])
-> ((TreeHTML a -> HTMLMatcher TreeHTML a)
    -> [TreeHTML a] -> [HTMLMatcher TreeHTML a])
-> (TreeHTML a -> HTMLMatcher TreeHTML a)
-> ParsecT s u m [TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TreeHTML a -> HTMLMatcher TreeHTML a)
-> [TreeHTML a] -> [HTMLMatcher TreeHTML a]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) TreeHTML a -> HTMLMatcher TreeHTML a
forall (a :: * -> *) b. a b -> HTMLMatcher a b
Element (ParsecT s u m (TreeHTML a) -> ParsecT s u m [TreeHTML a]
forall a. ParsecT s u m a -> ParsecT s u m [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (ParsecT s u m (TreeHTML a) -> ParsecT s u m (TreeHTML a)
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT s u m (TreeHTML a) -> ParsecT s u m (TreeHTML a))
-> ParsecT s u m (TreeHTML a) -> ParsecT s u m (TreeHTML a)
forall a b. (a -> b) -> a -> b
$ Maybe (ParsecT s u m a)
-> String
-> [(String, String)]
-> Forest (String, Attrs)
-> ParsecT s u m (TreeHTML a)
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a)
-> String
-> [(String, String)]
-> Forest (String, Attrs)
-> ParsecT s u m (TreeHTML a)
treeElemParserSpecific (ParsecT s u m a -> Maybe (ParsecT s u m a)
forall a. a -> Maybe a
Just ParsecT s u m a
match) String
elem (Attrs -> [(String, String)]
forall k a. Map k a -> [(k, a)]
Map.toList Attrs
attrs) Forest (String, Attrs)
subTree ))
  One (Node (String
elem, Attrs
attrs) Forest (String, Attrs)
subTree) ->
    Maybe (ParsecT s u m a)
-> String
-> [(String, String)]
-> Forest (String, Attrs)
-> ParsecT s u m (TreeHTML a)
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a)
-> String
-> [(String, String)]
-> Forest (String, Attrs)
-> ParsecT s u m (TreeHTML a)
treeElemParserSpecific (ParsecT s u m a -> Maybe (ParsecT s u m a)
forall a. a -> Maybe a
Just ParsecT s u m a
match) String
elem (Attrs -> [(String, String)]
forall k a. Map k a -> [(k, a)]
Map.toList Attrs
attrs) Forest (String, Attrs)
subTree ParsecT s u m (TreeHTML a)
-> (TreeHTML a -> ParsecT s u m [HTMLMatcher TreeHTML a])
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall a b.
ParsecT s u m a -> (a -> ParsecT s u m b) -> ParsecT s u m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [HTMLMatcher TreeHTML a] -> ParsecT s u m [HTMLMatcher TreeHTML a]
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return ([HTMLMatcher TreeHTML a]
 -> ParsecT s u m [HTMLMatcher TreeHTML a])
-> (TreeHTML a -> [HTMLMatcher TreeHTML a])
-> TreeHTML a
-> ParsecT s u m [HTMLMatcher TreeHTML a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (HTMLMatcher TreeHTML a
 -> [HTMLMatcher TreeHTML a] -> [HTMLMatcher TreeHTML a])
-> [HTMLMatcher TreeHTML a]
-> HTMLMatcher TreeHTML a
-> [HTMLMatcher TreeHTML a]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (:) [] (HTMLMatcher TreeHTML a -> [HTMLMatcher TreeHTML a])
-> (TreeHTML a -> HTMLMatcher TreeHTML a)
-> TreeHTML a
-> [HTMLMatcher TreeHTML a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TreeHTML a -> HTMLMatcher TreeHTML a
forall (a :: * -> *) b. a b -> HTMLMatcher a b
Element
                                                                        -- like return . (\x -> x :[])



-- | Is able to repeat / execute any pattern that returns multiple elements of same type
-- |(see manyTreeElemHeadParser)
-- htmlGenParserRepeat :: (Stream s m Char, ShowHTML a) =>
                       -- ParsecT s u m a
                    -- -> [Many (Tree ElemHead)]
                    -- -> ParsecT s u m [TreeHTML a] -- Can just apply multiTreeElemHeadParser (if i should) inside
                    -- -> ParsecT s u m [HTMLMatcher TreeHTML a]


-- | HTMLGenParserRepeat is in this use case always going to be exact ie these 3 elems then the end tag
-- |  ... and maybe some text in between there

-- | OF the cases we can do this:
  -- | parse and repeat function/recurse
  -- | or find end tag >> return [] which ends list 
  



  -- -- (eIns, at)  <- parseOpeningTagDesc (Just [(fst mElHead1), (fst mEleHead2)]) []
  -- let
  --   atSet :: Elem -> [Tree ElemHead] -> [Tree ElemHead]
  --   atSet = filter (\(Node (e, a)) -> if eIns == e then True else False) elSet 
      

            
  -- f' eIns at elHeads
                                     

  -- -- | Note that this is not a manyTill but should behave like any parser, f (however f is defined)
  -- -- | then once thats complete, parse endTag (with maybe some text) 

    -- f :: Stream s m Char =>
         -- Maybe (ParsecT s u m a)
      -- -> Elem
      -- -> Attrs
      -- -> [Many (Tree ElemHead)]
      -- -> ParsecT s u m ([Many (Tree ElemHead)], TreeHTML a)
    --
    -- At this point, we 100% know that the head of the list is the correct one 
    -- f match endT at elHeads = do
      -- (m, inTx, inTr) <- innerParserSpecific match endT (fromMany . fst $ elHeads)

      -- case fst elHeads of
        -- Many a -> "" --doesnt have to be there at all 
        -- One a -> "" -- has to be there 
   
   -- then get attrs 


       -- OR filter for successful el tag eg "a" was apart of allowed set, it was "a" and now you
       -- filter the allowed set for "a" to reference attrs to try

                                     --eg: ref "a" ("a", fromList [("x", "y")])
                                     

    -- This function should also handle (Many a) control flow
      -- case manyA
      --  One a -> delete elHead from state if openTag matches
      --  Many a -> delete if

      --  if (Many a) on second one then reset (call treeElePSC again with delete elHeads) 
    -- f' match eIns at manyElHeads
      --  at == (snd mElHead1) && eIns == (fst mElHead1) = f eIns at manyElHeads 
      --  at == (snd mElHead2) && eIns == (fst mElHead2) = f eIns at (tail manyElHeads) 
      --  otherwise = parserFail "does not match on elements"
  

fromMany :: Many a -> a
fromMany :: forall a. Many a -> a
fromMany (One a
a) = a
a
fromMany (Many a
a) = a
a 



---OLD
  -- <|> (do
  --         -- this gets into treeElemParserSpecific territory
  --         (eIns, at)  <- parseOpeningTagDesc (Just [(fst mElHead1), (fst mEleHead2)]) []
  --         if attrs e == snd mElHead1 -- (elTag e == (fst mElHead1) && (attrs a) == (snd mElHead1)
  --           then f manyElHeads 
  --           do
  --             (m, inTx, inTr) <- innerParserSpecific mElHead1 match eIns  
  --             (:) <$> (TreeHTML e at m inTx inTr) <*> htmlGenParserRepeat match (mElHead1:mElHead2:manyElHeads)
  --           else
  --           -- should also have this or similar behaviour in case (One a) 
  --           if attrs e == snd mElHead2
  --           then
  --             do
  --               (matches, inText, inTree) <- innerParserSpecific mElHead2 match elem 
  --               (:[]) <$> (TreeHTML e at matches inText inTree) <*> htmlGenParserRepeat match (mElHead2:manyElHeads)
  --           else parserFail "did not match on either of the next two previous elements" 
  --         -- case e1,e2,parserFail
  --     )

  
-- | Note that multiTreeElemHeadParser is still not handled, all I need to do is auto delete if only one
-- | actual function of multiTreeElemHeadParser will not be used but broken up


  -- <|> ((fmap . fmap) (Element) parsesTreeHs) -- list of elements, could be single element or multiple 
  -- <|> (do { x <- anyChar; return (IText (x:[]):[]) }) -- just allows for singleton creation (x:[])



    
     -- then f manyElHeads
     -- else
       -- if at == (snd mElHead2) && eIns == (fst mElHead2)
       -- then f (tail manyElHeads)
       -- else parserFail "does not match on elements"

  
--     -- should also have this or similar behaviour in case (One a) 
--     i
--     then
--       do
--         (matches, inText, inTree) <- innerParserSpecific mElHead2 match elem 
--         (:[]) <$> (TreeHTML e at matches inText inTree) <*> htmlGenParserRepeat match (mElHead2:manyElHeads)
--     else parserFail "did not match on either of the next two previous elements" 
--   -- case e1,e2,parserFail
-- )


-- might still be:

--   htmlGenParserRepeat
--   endTag tag

-- as opposed to:

--   manyTill_ htmlGenParserRepeat (endTag tag)

-- but it could still need to be the second in order to work 

---------- where htmlGenParserRepeat =
--     <|> 
--     --- if this succeeds (we try this first) then we reset I believe with this parser as Prev 

--   (do { x <- try match;  return $ (Match x):[] })
--   <|> (do { txt <- try stylingElem; return $ (IText txt):[] }) 

  
--   -- <|> ((fmap . fmap) (if innerText' == searchTerm then Element else (IText . innerText')) parsesTreeHs) -- list of elements, could be single element or multiple
--   <|> multiTreeElemHeadParser match mElHead1
  
  
  
--   <|> ((fmap . fmap) (Element) parsesTreeHs) -- list of elements, could be single element or multiple 
--   <|> (do { x <- anyChar; return (IText (x:[]):[]) }) -- just allows for singleton creation (x:[])
-- -----------



-- Doesnt change the structure of the page at all just how text is styled like MS word stuff
stylingTags :: [String]
stylingTags = [String
"abbr", String
"b", String
"big", String
"acronym", String
"dfn", String
"em", String
"font", String
"i", String
"mark", String
"q", String
"small"] -- , "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 String
stylingElem = do
  (String
e,Attrs
_) <- Maybe [String]
-> [(String, Maybe String)] -> ParsecT s u m (String, Attrs)
forall s (m :: * -> *) u.
Stream s m Char =>
Maybe [String]
-> [(String, Maybe String)] -> ParsecT s u m (String, Attrs)
parseOpeningTag ([String] -> Maybe [String]
forall a. a -> Maybe a
Just [String]
stylingTags) []
  Char -> ParsecT s u m Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'>'
  ((String, String) -> String)
-> ParsecT s u m (String, String) -> ParsecT s u m String
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 (String, String) -> String
forall a b. (a, b) -> a
fst (ParsecT s u m (String, String) -> ParsecT s u m String)
-> ParsecT s u m (String, String) -> ParsecT s u m String
forall a b. (a -> b) -> a -> b
$ ParsecT s u m Char
-> ParsecT s u m String -> ParsecT s u m (String, String)
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 (String -> ParsecT s u m String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
endTag String
e)
  




-- | Interface to find same element    
sameTreeH :: (Stream s m Char, ShowHTML a)
              => Maybe (ParsecT s u m a)
              -> TreeHTML a
              -> ParsecT s u m (TreeHTML a)
sameTreeH :: forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a) -> TreeHTML a -> ParsecT s u m (TreeHTML a)
sameTreeH Maybe (ParsecT s u m a)
matchh TreeHTML a
treeH = Maybe (ParsecT s u m a)
-> String
-> [(String, String)]
-> Forest (String, Attrs)
-> ParsecT s u m (TreeHTML a)
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a)
-> String
-> [(String, String)]
-> Forest (String, Attrs)
-> ParsecT s u m (TreeHTML a)
treeElemParserSpecific Maybe (ParsecT s u m a)
matchh (TreeHTML a -> String
forall b. TreeHTML b -> String
forall (a :: * -> *) b. ElementRep a => a b -> String
elTag TreeHTML a
treeH) (Attrs -> [(String, String)]
forall k a. Map k a -> [(k, a)]
Map.toList (Attrs -> [(String, String)]) -> Attrs -> [(String, String)]
forall a b. (a -> b) -> a -> b
$ TreeHTML a -> Attrs
forall b. TreeHTML b -> Attrs
forall (a :: * -> *) b. ElementRep a => a b -> Attrs
attrs TreeHTML a
treeH) (TreeHTML a -> Forest (String, Attrs)
forall a. TreeHTML a -> Forest (String, Attrs)
_innerTree' TreeHTML a
treeH)

-- I could do 2 things at the same time as calling findSameTreeH : use in findNaive, use in `some` 

                                      

-- this implementation would cause issues for when we want to check equality of trees
-- we would need to set the inside tree element parser + we would also need to think about how to     handle matches -->> maybe check after for matches > 0?
htmlGenParserFlex :: (Stream s m Char, ShowHTML a) =>
                     Maybe (ParsecT s u m a)
                  -> ParsecT s u m (HTMLMatcher TreeHTML a)
htmlGenParserFlex :: forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a) -> ParsecT s u m (HTMLMatcher TreeHTML a)
htmlGenParserFlex Maybe (ParsecT s u m a)
a = (ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML a)
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (a -> HTMLMatcher TreeHTML a
forall (a :: * -> *) b. b -> HTMLMatcher a b
Match (a -> HTMLMatcher TreeHTML a)
-> ParsecT s u m a -> ParsecT s u m (HTMLMatcher TreeHTML 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)
a)))
                      ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML 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 TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML a)
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (TreeHTML a -> HTMLMatcher TreeHTML a
forall (a :: * -> *) b. a b -> HTMLMatcher a b
Element (TreeHTML a -> HTMLMatcher TreeHTML a)
-> ParsecT s u m (TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe [String]
-> Maybe (ParsecT s u m a)
-> [(String, Maybe String)]
-> ParsecT s u m (TreeHTML a)
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe [String]
-> Maybe (ParsecT s u m a)
-> [(String, Maybe String)]
-> ParsecT s u m (TreeHTML a)
treeElemParser Maybe [String]
forall a. Maybe a
Nothing Maybe (ParsecT s u m a)
a [])   --(treeElemParserAnyInside a))
                      ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML 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
<|> ((String -> HTMLMatcher TreeHTML a
forall (a :: * -> *) b. String -> HTMLMatcher a b
IText (String -> HTMLMatcher TreeHTML a)
-> (Char -> String) -> Char -> HTMLMatcher TreeHTML a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> ShowS
forall a. a -> [a] -> [a]
:[])) (Char -> HTMLMatcher TreeHTML a)
-> ParsecT s u m Char -> ParsecT s u m (HTMLMatcher TreeHTML 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)
    







--- Not in use: ----------------------------------------------------------------------------------------------------








htmlGenParser :: (Stream s m Char, ShowHTML a)
              => ParsecT s u m a
              -> ParsecT s u m (TreeHTML a)
              -> ParsecT s u m (HTMLMatcher TreeHTML a)
htmlGenParser :: forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
ParsecT s u m a
-> ParsecT s u m (TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML a)
htmlGenParser ParsecT s u m a
a ParsecT s u m (TreeHTML a)
parseTreeH = (a -> HTMLMatcher TreeHTML a
forall (a :: * -> *) b. b -> HTMLMatcher a b
Match (a -> HTMLMatcher TreeHTML a)
-> ParsecT s u m a -> ParsecT s u m (HTMLMatcher TreeHTML a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT s u m a -> ParsecT s u m a
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT s u m a
a)
                             ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML 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
<|> (TreeHTML a -> HTMLMatcher TreeHTML a
forall (a :: * -> *) b. a b -> HTMLMatcher a b
Element (TreeHTML a -> HTMLMatcher TreeHTML a)
-> ParsecT s u m (TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT s u m (TreeHTML a) -> ParsecT s u m (TreeHTML a)
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT s u m (TreeHTML a)
parseTreeH)
                             ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML 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
<|> ((Char -> HTMLMatcher TreeHTML a)
-> ParsecT s u m Char -> ParsecT s u m (HTMLMatcher TreeHTML 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 (String -> HTMLMatcher TreeHTML a
forall (a :: * -> *) b. String -> HTMLMatcher a b
IText (String -> HTMLMatcher TreeHTML a)
-> (Char -> String) -> Char -> HTMLMatcher TreeHTML a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> ShowS
forall a. a -> [a] -> [a]
:[])) ParsecT s u m Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
anyChar)




{-# DEPRECATED specificForest "you likely need specificRepetitiveForest" #-}
-- | Library function for when you want an exact match, if 3 of ElemHead A then it looks for 3 Elemhead A
 -- accumMaybe' :: [HTMLMatcher] -> ParsecT s u m a
specificForest :: (Stream s m Char, ShowHTML a) =>
                  [Tree ElemHead]
               -> ParsecT s u m a
               -> ParsecT s u m [HTMLMatcher TreeHTML a]
specificForest :: forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Forest (String, Attrs)
-> ParsecT s u m a -> ParsecT s u m [HTMLMatcher TreeHTML a]
specificForest [] ParsecT s u m a
_ = [HTMLMatcher TreeHTML a] -> ParsecT s u m [HTMLMatcher TreeHTML a]
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return [] --or could be allowing for tail text
specificForest (Tree (String, Attrs)
x:Forest (String, Attrs)
xs) ParsecT s u m a
match = do
  HTMLMatcher TreeHTML a
y <- ParsecT s u m a
-> ParsecT s u m (TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML a)
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
ParsecT s u m a
-> ParsecT s u m (TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML a)
htmlGenParser ParsecT s u m a
match (Tree (String, Attrs)
-> ParsecT s u m a -> ParsecT s u m (TreeHTML a)
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Tree (String, Attrs)
-> ParsecT s u m a -> ParsecT s u m (TreeHTML a)
nodeToTreeElemExpr Tree (String, Attrs)
x ParsecT s u m a
match)
  [HTMLMatcher TreeHTML a]
ys <- case HTMLMatcher TreeHTML a
y of
     Element TreeHTML a
_ -> Forest (String, Attrs)
-> ParsecT s u m a -> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Forest (String, Attrs)
-> ParsecT s u m a -> ParsecT s u m [HTMLMatcher TreeHTML a]
specificForest Forest (String, Attrs)
xs ParsecT s u m a
match
     HTMLMatcher TreeHTML a
_ -> Forest (String, Attrs)
-> ParsecT s u m a -> ParsecT s u m [HTMLMatcher TreeHTML a]
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Forest (String, Attrs)
-> ParsecT s u m a -> ParsecT s u m [HTMLMatcher TreeHTML a]
specificForest (Tree (String, Attrs)
xTree (String, Attrs)
-> Forest (String, Attrs) -> Forest (String, Attrs)
forall a. a -> [a] -> [a]
:Forest (String, Attrs)
xs) ParsecT s u m a
match
  [HTMLMatcher TreeHTML a] -> ParsecT s u m [HTMLMatcher TreeHTML a]
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (HTMLMatcher TreeHTML a
y HTMLMatcher TreeHTML a
-> [HTMLMatcher TreeHTML a] -> [HTMLMatcher TreeHTML a]
forall a. a -> [a] -> [a]
: [HTMLMatcher TreeHTML a]
ys) 


  
nodeToTreeElemExpr :: (Stream s m Char, ShowHTML a) =>
                      Tree ElemHead
                   -> ParsecT s u m a
                   -> ParsecT s u m (TreeHTML a)
nodeToTreeElemExpr :: forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Tree (String, Attrs)
-> ParsecT s u m a -> ParsecT s u m (TreeHTML a)
nodeToTreeElemExpr (Node (String
elem, Attrs
attrs) Forest (String, Attrs)
subTree) ParsecT s u m a
match =
  Maybe (ParsecT s u m a)
-> String
-> [(String, String)]
-> Forest (String, Attrs)
-> ParsecT s u m (TreeHTML a)
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a)
-> String
-> [(String, String)]
-> Forest (String, Attrs)
-> ParsecT s u m (TreeHTML a)
treeElemParserSpecific (ParsecT s u m a -> Maybe (ParsecT s u m a)
forall a. a -> Maybe a
Just ParsecT s u m a
match) String
elem (Attrs -> [(String, String)]
forall k a. Map k a -> [(k, a)]
Map.toList Attrs
attrs) Forest (String, Attrs)
subTree



-----------------------------------------------------------------------------------------------------------------------------Main---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


-- | Used by treeElemParser' 
innerElemParser2 :: (ShowHTML a, Stream s m Char) =>
                   String
                -> Maybe (ParsecT s u m a)
                -> ParsecT s u m [HTMLMatcher TreeHTML a]
innerElemParser2 :: forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
String
-> Maybe (ParsecT s u m a)
-> ParsecT s u m [HTMLMatcher TreeHTML a]
innerElemParser2 String
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
'>'
                                  -- >> manyTill (try (Element <$> treeElemParser Nothing innerSpec [])) (try (endTag eTag))
                                  ParsecT s u m Char
-> ParsecT s u m [HTMLMatcher TreeHTML a]
-> ParsecT s u m [HTMLMatcher TreeHTML 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 TreeHTML a)
-> ParsecT s u m String -> ParsecT s u m [HTMLMatcher TreeHTML 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 TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML a)
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (a -> HTMLMatcher TreeHTML a
forall (a :: * -> *) b. b -> HTMLMatcher a b
Match (a -> HTMLMatcher TreeHTML a)
-> ParsecT s u m a -> ParsecT s u m (HTMLMatcher TreeHTML 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 TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML 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 TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML a)
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (String -> HTMLMatcher TreeHTML a
forall (a :: * -> *) b. String -> HTMLMatcher a b
IText (String -> HTMLMatcher TreeHTML a)
-> ParsecT s u m String -> ParsecT s u m (HTMLMatcher TreeHTML a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT s u m String
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m String
stylingElem))
                                               ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML 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 TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML a)
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (TreeHTML a -> HTMLMatcher TreeHTML a
forall (a :: * -> *) b. a b -> HTMLMatcher a b
Element (TreeHTML a -> HTMLMatcher TreeHTML a)
-> ParsecT s u m (TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe [String]
-> Maybe (ParsecT s u m a)
-> [(String, Maybe String)]
-> ParsecT s u m (TreeHTML a)
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe [String]
-> Maybe (ParsecT s u m a)
-> [(String, Maybe String)]
-> ParsecT s u m (TreeHTML a)
treeElemParser' Maybe [String]
forall a. Maybe a
Nothing Maybe (ParsecT s u m a)
innerSpec [])
                                               ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML a)
-> ParsecT s u m (HTMLMatcher TreeHTML 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
<|> ((String -> HTMLMatcher TreeHTML a
forall (a :: * -> *) b. String -> HTMLMatcher a b
IText (String -> HTMLMatcher TreeHTML a)
-> (Char -> String) -> Char -> HTMLMatcher TreeHTML a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> ShowS
forall a. a -> [a] -> [a]
:[])) (Char -> HTMLMatcher TreeHTML a)
-> ParsecT s u m Char -> ParsecT s u m (HTMLMatcher TreeHTML 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)) (ParsecT s u m String -> ParsecT s u m String
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT s u m String -> ParsecT s u m String)
-> ParsecT s u m String -> ParsecT s u m String
forall a b. (a -> b) -> a -> b
$ String -> ParsecT s u m String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
endTag String
eTag)

--- NEXT 3 ARE NOT IN USE, useful for API?

treeElemParserAnyInside :: (Stream s m Char, ShowHTML a) => Maybe (ParsecT s u m a) -> ParsecT s u m (TreeHTML a)
treeElemParserAnyInside :: forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe (ParsecT s u m a) -> ParsecT s u m (TreeHTML a)
treeElemParserAnyInside Maybe (ParsecT s u m a)
match = Maybe [String]
-> Maybe (ParsecT s u m a)
-> [(String, Maybe String)]
-> ParsecT s u m (TreeHTML a)
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe [String]
-> Maybe (ParsecT s u m a)
-> [(String, Maybe String)]
-> ParsecT s u m (TreeHTML a)
treeElemParser Maybe [String]
forall a. Maybe a
Nothing Maybe (ParsecT s u m a)
match []

--------------------------------------------------------------------------------------------------------------------
-------------------------------------------Groupings--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

  -- (_, treeH) <- manyTill_ (anyChar) (try $ treeElemParser elemOpts matchh attrsSubset)
  -- treeHs <- some (try $ findSameTreeH matchh treeH)
  -- return $ mkGH (treeH : treeHs) 


anyHtmlGroup :: (ShowHTML a, Stream s m Char) => ParsecT s u m (GroupHtml TreeHTML a)
anyHtmlGroup :: forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
ParsecT s u m (GroupHtml TreeHTML a)
anyHtmlGroup = Maybe [String]
-> Maybe (ParsecT s u m a)
-> [(String, Maybe String)]
-> ParsecT s u m (GroupHtml TreeHTML a)
forall s (m :: * -> *) a u.
(Stream s m Char, ShowHTML a) =>
Maybe [String]
-> Maybe (ParsecT s u m a)
-> [(String, Maybe String)]
-> ParsecT s u m (GroupHtml TreeHTML a)
htmlGroup Maybe [String]
forall a. Maybe a
Nothing Maybe (ParsecT s u m a)
forall a. Maybe a
Nothing [] 


-- Maybe this func should go in Find.hs
-- 0 -> Nothing
findAllSpaceMutExGroups :: (ShowHTML a, Stream s m Char) => ParsecT s u m (Maybe [GroupHtml TreeHTML a])
findAllSpaceMutExGroups :: forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
ParsecT s u m (Maybe [GroupHtml TreeHTML a])
findAllSpaceMutExGroups = ParsecT s u m (GroupHtml TreeHTML a)
-> ParsecT s u m (Maybe [GroupHtml TreeHTML a])
forall s (m :: * -> *) u a.
Stream s m Char =>
ParsecT s u m a -> ParsecT s u m (Maybe [a])
findNaive ParsecT s u m (GroupHtml TreeHTML a)
forall a s (m :: * -> *) u.
(ShowHTML a, Stream s m Char) =>
ParsecT s u m (GroupHtml TreeHTML a)
anyHtmlGroup 




findAllMutExGroups' :: a
findAllMutExGroups' = a
forall a. HasCallStack => a
undefined -- prime in name until renaming errors complete
-- deals with cases where attr:selected="true" exists since there will be two subgroups that
-- require concatenations

-- find == runParserOnHtml :: ParsecT s u m (Maybe [a]) ; a ~ GroupHtml b

-- Note: If we can find all groups, then we can find all non-groups ~ tree/display functionality 


-- findSomeHtmlNaive (try findAnyHtmlGroup) htmlText 


------------------------------------------------------------------------------------------------------------------------------------------------------Numerically Flexible Pattern Matching On Specific Elements----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

  
-- | What if above was of type :: [Many (Tree a)] -> ParsecT s u m [HTMLMatcher a]

-- [Node a _, Node b _, Node c _, Node d _, Node e _]  -> [Many tree1, Many tree2, One tree3]

                                   -- (((IText . (:[])) <$> anyChar) >>= return . flip (:) [])
                                   -- (Match <$> try a) >>= return . flip (:) [])

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------



















-- <|> (parseOpeningTag
--        >>= (\(e,a) -> do
--                a <- parseInnerBodySpecific *> endTag e
--                if e == (fst mElHead1) && a == (snd mElHead1)
--                  then 
--                case e of
--                  (fst mElHead1) -> 
--                htmlGenParserRepeat match 
               
--                a <> b
--                where b = 
--                        case e of
--                          mElHead1 -> do
--                            do innerBody of TreeElem(Specific) 
                  
--                           htmlGenParserRepeat match (mElHead1:mEleHead2:manyElHeads)
--                           a <> b

--                 mElHead2 -> htmlGenParserRepeat match (mElHead_1_OR_Both:manyElHeads) 
--            )
--   <|> element2 


-- elemSkeleton :: Stream s m Char => ParsecT s u m (Tree ElemHead)
-- elemSkeleton tags attrsIn pat = do
--   (e, a) <- parseOpeningTag --
--   branches <- case elem e selfClosing of
--     True -> return $ Node (e, a) []
--     False -> fmap (Node (e, a)) (manyHtml (elemSkeleton (ANY _ _))) *> endTag e
--   return Node