{-# LANGUAGE TypeFamilies #-}
module Text.Seonbi.Html.TagStack
( HtmlTagStack
, Text.Seonbi.Html.TagStack.any
, descendsFrom
, Text.Seonbi.Html.TagStack.elem
, depth
, empty
, fromList
, last
, pop
, push
, rebase
, toList
) where
import Prelude hiding (last)
import Data.List hiding (last)
import GHC.Exts (IsList (..))
import Text.Seonbi.Html.Tag
newtype HtmlTagStack = HtmlTagStack [HtmlTag] deriving (HtmlTagStack -> HtmlTagStack -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: HtmlTagStack -> HtmlTagStack -> Bool
$c/= :: HtmlTagStack -> HtmlTagStack -> Bool
== :: HtmlTagStack -> HtmlTagStack -> Bool
$c== :: HtmlTagStack -> HtmlTagStack -> Bool
Eq, Eq HtmlTagStack
HtmlTagStack -> HtmlTagStack -> Bool
HtmlTagStack -> HtmlTagStack -> Ordering
HtmlTagStack -> HtmlTagStack -> HtmlTagStack
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: HtmlTagStack -> HtmlTagStack -> HtmlTagStack
$cmin :: HtmlTagStack -> HtmlTagStack -> HtmlTagStack
max :: HtmlTagStack -> HtmlTagStack -> HtmlTagStack
$cmax :: HtmlTagStack -> HtmlTagStack -> HtmlTagStack
>= :: HtmlTagStack -> HtmlTagStack -> Bool
$c>= :: HtmlTagStack -> HtmlTagStack -> Bool
> :: HtmlTagStack -> HtmlTagStack -> Bool
$c> :: HtmlTagStack -> HtmlTagStack -> Bool
<= :: HtmlTagStack -> HtmlTagStack -> Bool
$c<= :: HtmlTagStack -> HtmlTagStack -> Bool
< :: HtmlTagStack -> HtmlTagStack -> Bool
$c< :: HtmlTagStack -> HtmlTagStack -> Bool
compare :: HtmlTagStack -> HtmlTagStack -> Ordering
$ccompare :: HtmlTagStack -> HtmlTagStack -> Ordering
Ord)
instance IsList HtmlTagStack where
type Item HtmlTagStack = HtmlTag
fromList :: [Item HtmlTagStack] -> HtmlTagStack
fromList = [HtmlTag] -> HtmlTagStack
HtmlTagStack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> [a]
reverse
toList :: HtmlTagStack -> [Item HtmlTagStack]
toList (HtmlTagStack [HtmlTag]
tags) = forall a. [a] -> [a]
reverse [HtmlTag]
tags
instance Show HtmlTagStack where
show :: HtmlTagStack -> String
show HtmlTagStack
tags = String
"fromList " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall l. IsList l => l -> [Item l]
toList HtmlTagStack
tags)
empty :: HtmlTagStack
empty :: HtmlTagStack
empty = [HtmlTag] -> HtmlTagStack
HtmlTagStack []
depth :: HtmlTagStack -> Int
depth :: HtmlTagStack -> Int
depth (HtmlTagStack [HtmlTag]
stack) = forall (t :: * -> *) a. Foldable t => t a -> Int
Data.List.length [HtmlTag]
stack
last :: HtmlTagStack -> Maybe HtmlTag
last :: HtmlTagStack -> Maybe HtmlTag
last (HtmlTagStack []) = forall a. Maybe a
Nothing
last (HtmlTagStack (HtmlTag
tag:[HtmlTag]
_)) = forall a. a -> Maybe a
Just HtmlTag
tag
rebase :: HtmlTagStack -> HtmlTagStack -> HtmlTagStack -> HtmlTagStack
rebase :: HtmlTagStack -> HtmlTagStack -> HtmlTagStack -> HtmlTagStack
rebase (HtmlTagStack [HtmlTag]
base) (HtmlTagStack [HtmlTag]
newBase) stack :: HtmlTagStack
stack@(HtmlTagStack [HtmlTag]
l)
| [HtmlTag]
base forall a. Eq a => [a] -> [a] -> Bool
`isSuffixOf` [HtmlTag]
l = [HtmlTag] -> HtmlTagStack
HtmlTagStack forall a b. (a -> b) -> a -> b
$
forall a. Int -> [a] -> [a]
take (HtmlTagStack -> Int
depth HtmlTagStack
stack forall a. Num a => a -> a -> a
- forall (t :: * -> *) a. Foldable t => t a -> Int
length [HtmlTag]
base) [HtmlTag]
l forall a. [a] -> [a] -> [a]
++ [HtmlTag]
newBase
| Bool
otherwise = HtmlTagStack
stack
push :: HtmlTag -> HtmlTagStack -> HtmlTagStack
push :: HtmlTag -> HtmlTagStack -> HtmlTagStack
push HtmlTag
tag (HtmlTagStack [HtmlTag]
tags) =
[HtmlTag] -> HtmlTagStack
HtmlTagStack (HtmlTag
tag forall a. a -> [a] -> [a]
: [HtmlTag]
tags)
pop :: HtmlTag -> HtmlTagStack -> HtmlTagStack
pop :: HtmlTag -> HtmlTagStack -> HtmlTagStack
pop HtmlTag
tag (HtmlTagStack tags' :: [HtmlTag]
tags'@(HtmlTag
t : [HtmlTag]
ags)) =
if HtmlTag
t forall a. Eq a => a -> a -> Bool
== HtmlTag
tag
then [HtmlTag] -> HtmlTagStack
HtmlTagStack [HtmlTag]
ags
else
let
([HtmlTag]
head', [HtmlTag]
rest) = forall a. (a -> Bool) -> [a] -> ([a], [a])
span (forall a. Eq a => a -> a -> Bool
/= HtmlTag
tag) [HtmlTag]
tags'
tail' :: [HtmlTag]
tail' = case forall a. [a] -> Maybe (a, [a])
uncons [HtmlTag]
rest of
Just (HtmlTag
_, [HtmlTag]
tail'') -> [HtmlTag]
tail''
Maybe (HtmlTag, [HtmlTag])
Nothing -> []
in
[HtmlTag] -> HtmlTagStack
HtmlTagStack ([HtmlTag]
head' forall a. [a] -> [a] -> [a]
++ [HtmlTag]
tail')
pop HtmlTag
_ (HtmlTagStack []) = HtmlTagStack
empty
descendsFrom :: HtmlTagStack -> HtmlTagStack -> Bool
HtmlTagStack [HtmlTag]
a descendsFrom :: HtmlTagStack -> HtmlTagStack -> Bool
`descendsFrom` HtmlTagStack [HtmlTag]
b =
[HtmlTag]
b forall a. Eq a => [a] -> [a] -> Bool
`isSuffixOf` [HtmlTag]
a
any :: (HtmlTag -> Bool) -> HtmlTagStack -> Bool
any :: (HtmlTag -> Bool) -> HtmlTagStack -> Bool
any HtmlTag -> Bool
fn (HtmlTagStack [HtmlTag]
stack) =
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
Prelude.any HtmlTag -> Bool
fn [HtmlTag]
stack
elem :: HtmlTag -> HtmlTagStack -> Bool
elem :: HtmlTag -> HtmlTagStack -> Bool
elem HtmlTag
tag (HtmlTagStack [HtmlTag]
stack) = HtmlTag
tag forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`Prelude.elem` [HtmlTag]
stack