{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Zenacy.HTML.Internal.Zip
( HTMLZipper
, HTMLZipAction
, HTMLIter
, HTMLZipPath(..)
, htmlZip
, htmlZipM
, htmlUnzip
, htmlUnzipM
, htmlZipNode
, htmlZipNodeM
, htmlZipRoot
, htmlZipRootM
, htmlZipUp
, htmlZipParent
, htmlZipFirst
, htmlZipLast
, htmlZipFind
, htmlZipNext
, htmlZipPrev
, htmlZipGet
, htmlZipTest
, htmlZipTestNode
, htmlZipTestName
, htmlZipTestFirst
, htmlZipTestLast
, htmlZipModify
, htmlZipModifyM
, htmlZipDelete
, htmlZipCollapse
, htmlZipInsertBefore
, htmlZipInsertAfter
, htmlZipContentBefore
, htmlZipContentAfter
, htmlZipContentLeft
, htmlZipContentRight
, htmlZipDropBefore
, htmlZipDropAfter
, htmlZipDropLeft
, htmlZipDropRight
, htmlZipPruneBefore
, htmlZipPruneAfter
, htmlZipPruneLeft
, htmlZipPruneRight
, htmlZipRepeat
, htmlZipStepNext
, htmlZipStepBack
, htmlZipSearch
, htmlZipIndex
, htmlIter
, htmlIterZipper
, htmlIterSearch
, htmlIterModify
, htmlIterNext
, htmlIterBack
, htmlZipPath
, htmlZipPathEmpty
, htmlZipPathFind
) where
import Zenacy.HTML.Internal.Core
import Zenacy.HTML.Internal.HTML
import Zenacy.HTML.Internal.Oper
import Control.Monad
( (>=>)
)
import Data.Bool
( bool
)
import Data.Default
( Default(..)
)
import Data.Maybe
( fromMaybe
, isNothing
)
import Data.Monoid
( (<>)
)
import Data.Text
( Text
)
import qualified Data.Text as T
( unpack
)
data HTMLCrumb = HTMLCrumb HTMLNode [HTMLNode] [HTMLNode]
data HTMLZipper = HTMLZipper HTMLNode [HTMLCrumb]
type HTMLZipAction = HTMLZipper -> Maybe HTMLZipper
data Direction = Down | Across | Up
data HTMLIter = HTMLIter Direction HTMLZipper
newtype HTMLZipPath = HTMLZipPath [Int] deriving (Int -> HTMLZipPath -> ShowS
[HTMLZipPath] -> ShowS
HTMLZipPath -> String
(Int -> HTMLZipPath -> ShowS)
-> (HTMLZipPath -> String)
-> ([HTMLZipPath] -> ShowS)
-> Show HTMLZipPath
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [HTMLZipPath] -> ShowS
$cshowList :: [HTMLZipPath] -> ShowS
show :: HTMLZipPath -> String
$cshow :: HTMLZipPath -> String
showsPrec :: Int -> HTMLZipPath -> ShowS
$cshowsPrec :: Int -> HTMLZipPath -> ShowS
Show, HTMLZipPath -> HTMLZipPath -> Bool
(HTMLZipPath -> HTMLZipPath -> Bool)
-> (HTMLZipPath -> HTMLZipPath -> Bool) -> Eq HTMLZipPath
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: HTMLZipPath -> HTMLZipPath -> Bool
$c/= :: HTMLZipPath -> HTMLZipPath -> Bool
== :: HTMLZipPath -> HTMLZipPath -> Bool
$c== :: HTMLZipPath -> HTMLZipPath -> Bool
Eq, Eq HTMLZipPath
Eq HTMLZipPath
-> (HTMLZipPath -> HTMLZipPath -> Ordering)
-> (HTMLZipPath -> HTMLZipPath -> Bool)
-> (HTMLZipPath -> HTMLZipPath -> Bool)
-> (HTMLZipPath -> HTMLZipPath -> Bool)
-> (HTMLZipPath -> HTMLZipPath -> Bool)
-> (HTMLZipPath -> HTMLZipPath -> HTMLZipPath)
-> (HTMLZipPath -> HTMLZipPath -> HTMLZipPath)
-> Ord HTMLZipPath
HTMLZipPath -> HTMLZipPath -> Bool
HTMLZipPath -> HTMLZipPath -> Ordering
HTMLZipPath -> HTMLZipPath -> HTMLZipPath
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 :: HTMLZipPath -> HTMLZipPath -> HTMLZipPath
$cmin :: HTMLZipPath -> HTMLZipPath -> HTMLZipPath
max :: HTMLZipPath -> HTMLZipPath -> HTMLZipPath
$cmax :: HTMLZipPath -> HTMLZipPath -> HTMLZipPath
>= :: HTMLZipPath -> HTMLZipPath -> Bool
$c>= :: HTMLZipPath -> HTMLZipPath -> Bool
> :: HTMLZipPath -> HTMLZipPath -> Bool
$c> :: HTMLZipPath -> HTMLZipPath -> Bool
<= :: HTMLZipPath -> HTMLZipPath -> Bool
$c<= :: HTMLZipPath -> HTMLZipPath -> Bool
< :: HTMLZipPath -> HTMLZipPath -> Bool
$c< :: HTMLZipPath -> HTMLZipPath -> Bool
compare :: HTMLZipPath -> HTMLZipPath -> Ordering
$ccompare :: HTMLZipPath -> HTMLZipPath -> Ordering
$cp1Ord :: Eq HTMLZipPath
Ord)
instance Default HTMLZipPath where
def :: HTMLZipPath
def = HTMLZipPath
htmlZipPathEmpty
htmlZip :: HTMLNode -> HTMLZipper
htmlZip :: HTMLNode -> HTMLZipper
htmlZip HTMLNode
x = HTMLNode -> [HTMLCrumb] -> HTMLZipper
HTMLZipper HTMLNode
x []
htmlZipM :: Monad m => HTMLNode -> m HTMLZipper
htmlZipM :: HTMLNode -> m HTMLZipper
htmlZipM = HTMLZipper -> m HTMLZipper
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HTMLZipper -> m HTMLZipper)
-> (HTMLNode -> HTMLZipper) -> HTMLNode -> m HTMLZipper
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HTMLNode -> HTMLZipper
htmlZip
htmlUnzip :: HTMLZipper -> HTMLNode
htmlUnzip :: HTMLZipper -> HTMLNode
htmlUnzip = HTMLZipper -> HTMLNode
htmlZipNode (HTMLZipper -> HTMLNode)
-> (HTMLZipper -> HTMLZipper) -> HTMLZipper -> HTMLNode
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HTMLZipper -> HTMLZipper
htmlZipRoot
htmlUnzipM :: Monad m => HTMLZipper -> m HTMLNode
htmlUnzipM :: HTMLZipper -> m HTMLNode
htmlUnzipM = HTMLNode -> m HTMLNode
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HTMLNode -> m HTMLNode)
-> (HTMLZipper -> HTMLNode) -> HTMLZipper -> m HTMLNode
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HTMLZipper -> HTMLNode
htmlUnzip
htmlZipNode :: HTMLZipper -> HTMLNode
htmlZipNode :: HTMLZipper -> HTMLNode
htmlZipNode (HTMLZipper HTMLNode
x [HTMLCrumb]
_) = HTMLNode
x
htmlZipNodeM :: Monad m => HTMLZipper -> m HTMLNode
htmlZipNodeM :: HTMLZipper -> m HTMLNode
htmlZipNodeM = HTMLNode -> m HTMLNode
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HTMLNode -> m HTMLNode)
-> (HTMLZipper -> HTMLNode) -> HTMLZipper -> m HTMLNode
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HTMLZipper -> HTMLNode
htmlZipNode
htmlZipRoot :: HTMLZipper -> HTMLZipper
htmlZipRoot :: HTMLZipper -> HTMLZipper
htmlZipRoot HTMLZipper
x = HTMLZipper
-> (HTMLZipper -> HTMLZipper) -> Maybe HTMLZipper -> HTMLZipper
forall b a. b -> (a -> b) -> Maybe a -> b
maybe HTMLZipper
x HTMLZipper -> HTMLZipper
htmlZipRoot (Maybe HTMLZipper -> HTMLZipper) -> Maybe HTMLZipper -> HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLZipper -> Maybe HTMLZipper
htmlZipParent HTMLZipper
x
htmlZipRootM :: Monad m => HTMLZipper -> m HTMLZipper
htmlZipRootM :: HTMLZipper -> m HTMLZipper
htmlZipRootM = HTMLZipper -> m HTMLZipper
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HTMLZipper -> m HTMLZipper)
-> (HTMLZipper -> HTMLZipper) -> HTMLZipper -> m HTMLZipper
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HTMLZipper -> HTMLZipper
htmlZipRoot
htmlZipUp :: HTMLZipper -> Maybe HTMLZipper
htmlZipUp :: HTMLZipper -> Maybe HTMLZipper
htmlZipUp = HTMLZipper -> Maybe HTMLZipper
htmlZipParent
htmlZipParent :: HTMLZipper -> Maybe HTMLZipper
htmlZipParent :: HTMLZipper -> Maybe HTMLZipper
htmlZipParent = \case
HTMLZipper HTMLNode
x [] -> Maybe HTMLZipper
forall a. Maybe a
Nothing
HTMLZipper HTMLNode
x ((HTMLCrumb HTMLNode
n [HTMLNode]
ls [HTMLNode]
rs):[HTMLCrumb]
cs) ->
let c :: [HTMLNode]
c = [HTMLNode] -> [HTMLNode]
forall a. [a] -> [a]
reverse [HTMLNode]
ls [HTMLNode] -> [HTMLNode] -> [HTMLNode]
forall a. Semigroup a => a -> a -> a
<> [HTMLNode
x] [HTMLNode] -> [HTMLNode] -> [HTMLNode]
forall a. Semigroup a => a -> a -> a
<> [HTMLNode]
rs
in case HTMLNode
n of
HTMLDocument Text
n [] ->
HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just (HTMLZipper -> Maybe HTMLZipper) -> HTMLZipper -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLNode -> [HTMLCrumb] -> HTMLZipper
HTMLZipper (Text -> [HTMLNode] -> HTMLNode
HTMLDocument Text
n [HTMLNode]
c) [HTMLCrumb]
cs
HTMLDoctype Text
n Maybe Text
p Maybe Text
s ->
Maybe HTMLZipper
forall a. Maybe a
Nothing
HTMLFragment Text
n [] ->
HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just (HTMLZipper -> Maybe HTMLZipper) -> HTMLZipper -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLNode -> [HTMLCrumb] -> HTMLZipper
HTMLZipper (Text -> [HTMLNode] -> HTMLNode
HTMLFragment Text
n [HTMLNode]
c) [HTMLCrumb]
cs
HTMLElement Text
n HTMLNamespace
s [HTMLAttr]
a [] ->
HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just (HTMLZipper -> Maybe HTMLZipper) -> HTMLZipper -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLNode -> [HTMLCrumb] -> HTMLZipper
HTMLZipper (Text -> HTMLNamespace -> [HTMLAttr] -> [HTMLNode] -> HTMLNode
HTMLElement Text
n HTMLNamespace
s [HTMLAttr]
a [HTMLNode]
c) [HTMLCrumb]
cs
HTMLTemplate HTMLNamespace
s [HTMLAttr]
a HTMLNode
c ->
Maybe HTMLZipper
forall a. Maybe a
Nothing
HTMLText Text
t ->
Maybe HTMLZipper
forall a. Maybe a
Nothing
HTMLComment Text
c ->
Maybe HTMLZipper
forall a. Maybe a
Nothing
htmlZipFirst :: HTMLZipper -> Maybe HTMLZipper
htmlZipFirst :: HTMLZipper -> Maybe HTMLZipper
htmlZipFirst (HTMLZipper HTMLNode
y [HTMLCrumb]
z) = case HTMLNode
y of
HTMLDocument Text
n [HTMLNode]
c ->
[HTMLNode] -> HTMLNode -> Maybe HTMLZipper
f [HTMLNode]
c (HTMLNode -> Maybe HTMLZipper) -> HTMLNode -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ Text -> [HTMLNode] -> HTMLNode
HTMLDocument Text
n []
HTMLFragment Text
n [HTMLNode]
c ->
[HTMLNode] -> HTMLNode -> Maybe HTMLZipper
f [HTMLNode]
c (HTMLNode -> Maybe HTMLZipper) -> HTMLNode -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ Text -> [HTMLNode] -> HTMLNode
HTMLFragment Text
n []
HTMLElement Text
n HTMLNamespace
s [HTMLAttr]
a [HTMLNode]
c ->
[HTMLNode] -> HTMLNode -> Maybe HTMLZipper
f [HTMLNode]
c (HTMLNode -> Maybe HTMLZipper) -> HTMLNode -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ Text -> HTMLNamespace -> [HTMLAttr] -> [HTMLNode] -> HTMLNode
HTMLElement Text
n HTMLNamespace
s [HTMLAttr]
a []
HTMLNode
_ -> Maybe HTMLZipper
forall a. Maybe a
Nothing
where
f :: [HTMLNode] -> HTMLNode -> Maybe HTMLZipper
f [] HTMLNode
n = Maybe HTMLZipper
forall a. Maybe a
Nothing
f (HTMLNode
h:[HTMLNode]
rs) HTMLNode
n = HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just (HTMLZipper -> Maybe HTMLZipper) -> HTMLZipper -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLNode -> [HTMLCrumb] -> HTMLZipper
HTMLZipper HTMLNode
h ((HTMLNode -> [HTMLNode] -> [HTMLNode] -> HTMLCrumb
HTMLCrumb HTMLNode
n [] [HTMLNode]
rs)HTMLCrumb -> [HTMLCrumb] -> [HTMLCrumb]
forall a. a -> [a] -> [a]
:[HTMLCrumb]
z)
htmlZipLast :: HTMLZipper -> Maybe HTMLZipper
htmlZipLast :: HTMLZipper -> Maybe HTMLZipper
htmlZipLast (HTMLZipper HTMLNode
y [HTMLCrumb]
z) = case HTMLNode
y of
HTMLDocument Text
n [HTMLNode]
c ->
[HTMLNode] -> HTMLNode -> Maybe HTMLZipper
f [HTMLNode]
c (HTMLNode -> Maybe HTMLZipper) -> HTMLNode -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ Text -> [HTMLNode] -> HTMLNode
HTMLDocument Text
n []
HTMLFragment Text
n [HTMLNode]
c ->
[HTMLNode] -> HTMLNode -> Maybe HTMLZipper
f [HTMLNode]
c (HTMLNode -> Maybe HTMLZipper) -> HTMLNode -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ Text -> [HTMLNode] -> HTMLNode
HTMLFragment Text
n []
HTMLElement Text
n HTMLNamespace
s [HTMLAttr]
a [HTMLNode]
c ->
[HTMLNode] -> HTMLNode -> Maybe HTMLZipper
f [HTMLNode]
c (HTMLNode -> Maybe HTMLZipper) -> HTMLNode -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ Text -> HTMLNamespace -> [HTMLAttr] -> [HTMLNode] -> HTMLNode
HTMLElement Text
n HTMLNamespace
s [HTMLAttr]
a []
HTMLNode
_ -> Maybe HTMLZipper
forall a. Maybe a
Nothing
where
f :: [HTMLNode] -> HTMLNode -> Maybe HTMLZipper
f [] HTMLNode
n = Maybe HTMLZipper
forall a. Maybe a
Nothing
f [HTMLNode]
xs HTMLNode
n = let (HTMLNode
h:[HTMLNode]
ls) = [HTMLNode] -> [HTMLNode]
forall a. [a] -> [a]
reverse [HTMLNode]
xs in HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just (HTMLZipper -> Maybe HTMLZipper) -> HTMLZipper -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$
HTMLNode -> [HTMLCrumb] -> HTMLZipper
HTMLZipper HTMLNode
h ((HTMLNode -> [HTMLNode] -> [HTMLNode] -> HTMLCrumb
HTMLCrumb HTMLNode
n [HTMLNode]
ls [])HTMLCrumb -> [HTMLCrumb] -> [HTMLCrumb]
forall a. a -> [a] -> [a]
:[HTMLCrumb]
z)
htmlZipFind :: (HTMLNode -> Bool) -> HTMLZipper -> Maybe HTMLZipper
htmlZipFind :: (HTMLNode -> Bool) -> HTMLZipper -> Maybe HTMLZipper
htmlZipFind HTMLNode -> Bool
p (HTMLZipper HTMLNode
y [HTMLCrumb]
z) = case HTMLNode
y of
HTMLDocument Text
n [HTMLNode]
c ->
[HTMLNode] -> HTMLNode -> Maybe HTMLZipper
f [HTMLNode]
c (HTMLNode -> Maybe HTMLZipper) -> HTMLNode -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ Text -> [HTMLNode] -> HTMLNode
HTMLDocument Text
n []
HTMLFragment Text
n [HTMLNode]
c ->
[HTMLNode] -> HTMLNode -> Maybe HTMLZipper
f [HTMLNode]
c (HTMLNode -> Maybe HTMLZipper) -> HTMLNode -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ Text -> [HTMLNode] -> HTMLNode
HTMLFragment Text
n []
HTMLElement Text
n HTMLNamespace
s [HTMLAttr]
a [HTMLNode]
c ->
[HTMLNode] -> HTMLNode -> Maybe HTMLZipper
f [HTMLNode]
c (HTMLNode -> Maybe HTMLZipper) -> HTMLNode -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ Text -> HTMLNamespace -> [HTMLAttr] -> [HTMLNode] -> HTMLNode
HTMLElement Text
n HTMLNamespace
s [HTMLAttr]
a []
HTMLNode
_ -> Maybe HTMLZipper
forall a. Maybe a
Nothing
where
f :: [HTMLNode] -> HTMLNode -> Maybe HTMLZipper
f [HTMLNode]
c HTMLNode
n = case (HTMLNode -> Bool) -> [HTMLNode] -> ([HTMLNode], [HTMLNode])
forall a. (a -> Bool) -> [a] -> ([a], [a])
break HTMLNode -> Bool
p [HTMLNode]
c of
([HTMLNode]
ls, []) -> Maybe HTMLZipper
forall a. Maybe a
Nothing
([HTMLNode]
ls, HTMLNode
h:[HTMLNode]
rs) -> HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just (HTMLZipper -> Maybe HTMLZipper) -> HTMLZipper -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$
HTMLNode -> [HTMLCrumb] -> HTMLZipper
HTMLZipper HTMLNode
h ((HTMLNode -> [HTMLNode] -> [HTMLNode] -> HTMLCrumb
HTMLCrumb HTMLNode
n ([HTMLNode] -> [HTMLNode]
forall a. [a] -> [a]
reverse [HTMLNode]
ls) [HTMLNode]
rs)HTMLCrumb -> [HTMLCrumb] -> [HTMLCrumb]
forall a. a -> [a] -> [a]
:[HTMLCrumb]
z)
htmlZipNext :: HTMLZipper -> Maybe HTMLZipper
htmlZipNext :: HTMLZipper -> Maybe HTMLZipper
htmlZipNext = \case
HTMLZipper HTMLNode
x [] -> Maybe HTMLZipper
forall a. Maybe a
Nothing
HTMLZipper HTMLNode
x ((HTMLCrumb HTMLNode
n [HTMLNode]
ls []):[HTMLCrumb]
cs) -> Maybe HTMLZipper
forall a. Maybe a
Nothing
HTMLZipper HTMLNode
x ((HTMLCrumb HTMLNode
n [HTMLNode]
ls (HTMLNode
h:[HTMLNode]
rs)):[HTMLCrumb]
cs) ->
HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just (HTMLZipper -> Maybe HTMLZipper) -> HTMLZipper -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLNode -> [HTMLCrumb] -> HTMLZipper
HTMLZipper HTMLNode
h ((HTMLNode -> [HTMLNode] -> [HTMLNode] -> HTMLCrumb
HTMLCrumb HTMLNode
n (HTMLNode
xHTMLNode -> [HTMLNode] -> [HTMLNode]
forall a. a -> [a] -> [a]
:[HTMLNode]
ls) [HTMLNode]
rs)HTMLCrumb -> [HTMLCrumb] -> [HTMLCrumb]
forall a. a -> [a] -> [a]
:[HTMLCrumb]
cs)
htmlZipPrev :: HTMLZipper -> Maybe HTMLZipper
htmlZipPrev :: HTMLZipper -> Maybe HTMLZipper
htmlZipPrev = \case
HTMLZipper HTMLNode
x [] -> Maybe HTMLZipper
forall a. Maybe a
Nothing
HTMLZipper HTMLNode
x ((HTMLCrumb HTMLNode
n [] [HTMLNode]
rs):[HTMLCrumb]
cs) -> Maybe HTMLZipper
forall a. Maybe a
Nothing
HTMLZipper HTMLNode
x ((HTMLCrumb HTMLNode
n (HTMLNode
h:[HTMLNode]
ls) [HTMLNode]
rs):[HTMLCrumb]
cs) ->
HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just (HTMLZipper -> Maybe HTMLZipper) -> HTMLZipper -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLNode -> [HTMLCrumb] -> HTMLZipper
HTMLZipper HTMLNode
h ((HTMLNode -> [HTMLNode] -> [HTMLNode] -> HTMLCrumb
HTMLCrumb HTMLNode
n [HTMLNode]
ls (HTMLNode
xHTMLNode -> [HTMLNode] -> [HTMLNode]
forall a. a -> [a] -> [a]
:[HTMLNode]
rs))HTMLCrumb -> [HTMLCrumb] -> [HTMLCrumb]
forall a. a -> [a] -> [a]
:[HTMLCrumb]
cs)
htmlZipGet :: Int -> HTMLZipper -> Maybe HTMLZipper
htmlZipGet :: Int -> HTMLZipper -> Maybe HTMLZipper
htmlZipGet Int
n HTMLZipper
z
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = Maybe HTMLZipper
forall a. Maybe a
Nothing
| Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = HTMLZipper -> Maybe HTMLZipper
htmlZipFirst HTMLZipper
z
| Bool
otherwise = HTMLZipper -> Maybe HTMLZipper
htmlZipFirst HTMLZipper
z Maybe HTMLZipper
-> (HTMLZipper -> Maybe HTMLZipper) -> Maybe HTMLZipper
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> HTMLZipper -> Maybe HTMLZipper
forall t. (Eq t, Num t) => t -> HTMLZipper -> Maybe HTMLZipper
f Int
n
where
f :: t -> HTMLZipper -> Maybe HTMLZipper
f t
0 = HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just
f t
n = HTMLZipper -> Maybe HTMLZipper
htmlZipNext (HTMLZipper -> Maybe HTMLZipper)
-> (HTMLZipper -> Maybe HTMLZipper)
-> HTMLZipper
-> Maybe HTMLZipper
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> t -> HTMLZipper -> Maybe HTMLZipper
f (t
n t -> t -> t
forall a. Num a => a -> a -> a
- t
1)
htmlZipTest :: (HTMLZipper -> Bool) -> HTMLZipper -> Maybe HTMLZipper
htmlZipTest :: (HTMLZipper -> Bool) -> HTMLZipper -> Maybe HTMLZipper
htmlZipTest HTMLZipper -> Bool
f HTMLZipper
z = Maybe HTMLZipper -> Maybe HTMLZipper -> Bool -> Maybe HTMLZipper
forall a. a -> a -> Bool -> a
bool Maybe HTMLZipper
forall a. Maybe a
Nothing (HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just HTMLZipper
z) (Bool -> Maybe HTMLZipper) -> Bool -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLZipper -> Bool
f HTMLZipper
z
htmlZipTestNode :: (HTMLNode -> Bool) -> HTMLZipper -> Maybe HTMLZipper
htmlZipTestNode :: (HTMLNode -> Bool) -> HTMLZipper -> Maybe HTMLZipper
htmlZipTestNode HTMLNode -> Bool
f = (HTMLZipper -> Bool) -> HTMLZipper -> Maybe HTMLZipper
htmlZipTest ((HTMLZipper -> Bool) -> HTMLZipper -> Maybe HTMLZipper)
-> (HTMLZipper -> Bool) -> HTMLZipper -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLNode -> Bool
f (HTMLNode -> Bool)
-> (HTMLZipper -> HTMLNode) -> HTMLZipper -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HTMLZipper -> HTMLNode
htmlZipNode
htmlZipTestName :: Text -> HTMLZipper -> Maybe HTMLZipper
htmlZipTestName :: Text -> HTMLZipper -> Maybe HTMLZipper
htmlZipTestName Text
x = (HTMLZipper -> Bool) -> HTMLZipper -> Maybe HTMLZipper
htmlZipTest (Text -> HTMLNode -> Bool
htmlElemHasName Text
x (HTMLNode -> Bool)
-> (HTMLZipper -> HTMLNode) -> HTMLZipper -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HTMLZipper -> HTMLNode
htmlZipNode)
htmlZipTestFirst :: HTMLZipper -> Maybe HTMLZipper
htmlZipTestFirst :: HTMLZipper -> Maybe HTMLZipper
htmlZipTestFirst = (HTMLZipper -> Bool) -> HTMLZipper -> Maybe HTMLZipper
htmlZipTest (Maybe HTMLZipper -> Bool
forall a. Maybe a -> Bool
isNothing (Maybe HTMLZipper -> Bool)
-> (HTMLZipper -> Maybe HTMLZipper) -> HTMLZipper -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HTMLZipper -> Maybe HTMLZipper
htmlZipPrev)
htmlZipTestLast :: HTMLZipper -> Maybe HTMLZipper
htmlZipTestLast :: HTMLZipper -> Maybe HTMLZipper
htmlZipTestLast = (HTMLZipper -> Bool) -> HTMLZipper -> Maybe HTMLZipper
htmlZipTest (Maybe HTMLZipper -> Bool
forall a. Maybe a -> Bool
isNothing (Maybe HTMLZipper -> Bool)
-> (HTMLZipper -> Maybe HTMLZipper) -> HTMLZipper -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HTMLZipper -> Maybe HTMLZipper
htmlZipNext)
htmlZipModify :: (HTMLNode -> HTMLNode) -> HTMLZipper -> HTMLZipper
htmlZipModify :: (HTMLNode -> HTMLNode) -> HTMLZipper -> HTMLZipper
htmlZipModify HTMLNode -> HTMLNode
f (HTMLZipper HTMLNode
y [HTMLCrumb]
z) = HTMLNode -> [HTMLCrumb] -> HTMLZipper
HTMLZipper (HTMLNode -> HTMLNode
f HTMLNode
y) [HTMLCrumb]
z
htmlZipModifyM :: Monad m => (HTMLNode -> HTMLNode) -> HTMLZipper -> m HTMLZipper
htmlZipModifyM :: (HTMLNode -> HTMLNode) -> HTMLZipper -> m HTMLZipper
htmlZipModifyM HTMLNode -> HTMLNode
f = HTMLZipper -> m HTMLZipper
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HTMLZipper -> m HTMLZipper)
-> (HTMLZipper -> HTMLZipper) -> HTMLZipper -> m HTMLZipper
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (HTMLNode -> HTMLNode) -> HTMLZipper -> HTMLZipper
htmlZipModify HTMLNode -> HTMLNode
f
htmlZipDelete :: HTMLZipper -> Maybe HTMLZipper
htmlZipDelete :: HTMLZipper -> Maybe HTMLZipper
htmlZipDelete = \case
HTMLZipper HTMLNode
x [] -> Maybe HTMLZipper
forall a. Maybe a
Nothing
HTMLZipper HTMLNode
x ((HTMLCrumb HTMLNode
n [HTMLNode]
l [HTMLNode]
r):[HTMLCrumb]
cs) ->
let c :: [HTMLNode]
c = [HTMLNode] -> [HTMLNode]
forall a. [a] -> [a]
reverse [HTMLNode]
l [HTMLNode] -> [HTMLNode] -> [HTMLNode]
forall a. Semigroup a => a -> a -> a
<> [HTMLNode]
r
in case HTMLNode
n of
HTMLDocument Text
n [] ->
HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just (HTMLZipper -> Maybe HTMLZipper) -> HTMLZipper -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLNode -> [HTMLCrumb] -> HTMLZipper
HTMLZipper (Text -> [HTMLNode] -> HTMLNode
HTMLDocument Text
n [HTMLNode]
c) [HTMLCrumb]
cs
HTMLFragment Text
n [] ->
HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just (HTMLZipper -> Maybe HTMLZipper) -> HTMLZipper -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLNode -> [HTMLCrumb] -> HTMLZipper
HTMLZipper (Text -> [HTMLNode] -> HTMLNode
HTMLFragment Text
n [HTMLNode]
c) [HTMLCrumb]
cs
HTMLElement Text
n HTMLNamespace
s [HTMLAttr]
a [] ->
HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just (HTMLZipper -> Maybe HTMLZipper) -> HTMLZipper -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLNode -> [HTMLCrumb] -> HTMLZipper
HTMLZipper (Text -> HTMLNamespace -> [HTMLAttr] -> [HTMLNode] -> HTMLNode
HTMLElement Text
n HTMLNamespace
s [HTMLAttr]
a [HTMLNode]
c) [HTMLCrumb]
cs
HTMLNode
_ -> Maybe HTMLZipper
forall a. Maybe a
Nothing
htmlZipCollapse :: HTMLZipper -> Maybe HTMLZipper
htmlZipCollapse :: HTMLZipper -> Maybe HTMLZipper
htmlZipCollapse = \case
HTMLZipper HTMLNode
x [] -> Maybe HTMLZipper
forall a. Maybe a
Nothing
HTMLZipper HTMLNode
x ((HTMLCrumb HTMLNode
n [HTMLNode]
l [HTMLNode]
r):[HTMLCrumb]
cs) ->
let c :: [HTMLNode]
c = [HTMLNode] -> [HTMLNode]
forall a. [a] -> [a]
reverse [HTMLNode]
l [HTMLNode] -> [HTMLNode] -> [HTMLNode]
forall a. Semigroup a => a -> a -> a
<> HTMLNode -> [HTMLNode]
htmlNodeContent HTMLNode
x [HTMLNode] -> [HTMLNode] -> [HTMLNode]
forall a. Semigroup a => a -> a -> a
<> [HTMLNode]
r
in case HTMLNode
n of
HTMLDocument Text
n [] ->
HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just (HTMLZipper -> Maybe HTMLZipper) -> HTMLZipper -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLNode -> [HTMLCrumb] -> HTMLZipper
HTMLZipper (Text -> [HTMLNode] -> HTMLNode
HTMLDocument Text
n [HTMLNode]
c) [HTMLCrumb]
cs
HTMLFragment Text
n [] ->
HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just (HTMLZipper -> Maybe HTMLZipper) -> HTMLZipper -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLNode -> [HTMLCrumb] -> HTMLZipper
HTMLZipper (Text -> [HTMLNode] -> HTMLNode
HTMLFragment Text
n [HTMLNode]
c) [HTMLCrumb]
cs
HTMLElement Text
n HTMLNamespace
s [HTMLAttr]
a [] ->
HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just (HTMLZipper -> Maybe HTMLZipper) -> HTMLZipper -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLNode -> [HTMLCrumb] -> HTMLZipper
HTMLZipper (Text -> HTMLNamespace -> [HTMLAttr] -> [HTMLNode] -> HTMLNode
HTMLElement Text
n HTMLNamespace
s [HTMLAttr]
a [HTMLNode]
c) [HTMLCrumb]
cs
HTMLNode
_ -> Maybe HTMLZipper
forall a. Maybe a
Nothing
htmlZipInsertBefore :: HTMLNode -> HTMLZipper -> Maybe HTMLZipper
htmlZipInsertBefore :: HTMLNode -> HTMLZipper -> Maybe HTMLZipper
htmlZipInsertBefore HTMLNode
y = \case
HTMLZipper HTMLNode
x [] -> Maybe HTMLZipper
forall a. Maybe a
Nothing
HTMLZipper HTMLNode
x ((HTMLCrumb HTMLNode
n [HTMLNode]
l [HTMLNode]
r):[HTMLCrumb]
cs) ->
HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just (HTMLZipper -> Maybe HTMLZipper) -> HTMLZipper -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLNode -> [HTMLCrumb] -> HTMLZipper
HTMLZipper HTMLNode
x ((HTMLNode -> [HTMLNode] -> [HTMLNode] -> HTMLCrumb
HTMLCrumb HTMLNode
n (HTMLNode
yHTMLNode -> [HTMLNode] -> [HTMLNode]
forall a. a -> [a] -> [a]
:[HTMLNode]
l) [HTMLNode]
r)HTMLCrumb -> [HTMLCrumb] -> [HTMLCrumb]
forall a. a -> [a] -> [a]
:[HTMLCrumb]
cs)
htmlZipInsertAfter :: HTMLNode -> HTMLZipper -> Maybe HTMLZipper
htmlZipInsertAfter :: HTMLNode -> HTMLZipper -> Maybe HTMLZipper
htmlZipInsertAfter HTMLNode
y = \case
HTMLZipper HTMLNode
x [] -> Maybe HTMLZipper
forall a. Maybe a
Nothing
HTMLZipper HTMLNode
x ((HTMLCrumb HTMLNode
n [HTMLNode]
l [HTMLNode]
r):[HTMLCrumb]
cs) ->
HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just (HTMLZipper -> Maybe HTMLZipper) -> HTMLZipper -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLNode -> [HTMLCrumb] -> HTMLZipper
HTMLZipper HTMLNode
x ((HTMLNode -> [HTMLNode] -> [HTMLNode] -> HTMLCrumb
HTMLCrumb HTMLNode
n [HTMLNode]
l (HTMLNode
yHTMLNode -> [HTMLNode] -> [HTMLNode]
forall a. a -> [a] -> [a]
:[HTMLNode]
r))HTMLCrumb -> [HTMLCrumb] -> [HTMLCrumb]
forall a. a -> [a] -> [a]
:[HTMLCrumb]
cs)
htmlZipContentBefore :: HTMLZipper -> [HTMLNode]
htmlZipContentBefore :: HTMLZipper -> [HTMLNode]
htmlZipContentBefore = \case
HTMLZipper HTMLNode
x [] -> []
HTMLZipper HTMLNode
x ((HTMLCrumb HTMLNode
n [HTMLNode]
l [HTMLNode]
r):[HTMLCrumb]
cs) -> [HTMLNode] -> [HTMLNode]
forall a. [a] -> [a]
reverse [HTMLNode]
l
htmlZipContentAfter :: HTMLZipper -> [HTMLNode]
htmlZipContentAfter :: HTMLZipper -> [HTMLNode]
htmlZipContentAfter = \case
HTMLZipper HTMLNode
x [] -> []
HTMLZipper HTMLNode
x ((HTMLCrumb HTMLNode
n [HTMLNode]
l [HTMLNode]
r):[HTMLCrumb]
cs) -> [HTMLNode]
r
htmlZipContentLeft :: HTMLZipper -> [HTMLNode]
htmlZipContentLeft :: HTMLZipper -> [HTMLNode]
htmlZipContentLeft = HTMLZipper -> [HTMLNode]
htmlZipContentBefore
htmlZipContentRight :: HTMLZipper -> [HTMLNode]
htmlZipContentRight :: HTMLZipper -> [HTMLNode]
htmlZipContentRight = HTMLZipper -> [HTMLNode]
htmlZipContentAfter
htmlZipDropBefore :: HTMLZipper -> Maybe HTMLZipper
htmlZipDropBefore :: HTMLZipper -> Maybe HTMLZipper
htmlZipDropBefore = \case
HTMLZipper HTMLNode
x [] -> Maybe HTMLZipper
forall a. Maybe a
Nothing
HTMLZipper HTMLNode
x ((HTMLCrumb HTMLNode
n [HTMLNode]
_ [HTMLNode]
r):[HTMLCrumb]
cs) ->
HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just (HTMLZipper -> Maybe HTMLZipper) -> HTMLZipper -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLNode -> [HTMLCrumb] -> HTMLZipper
HTMLZipper HTMLNode
x ((HTMLNode -> [HTMLNode] -> [HTMLNode] -> HTMLCrumb
HTMLCrumb HTMLNode
n [] [HTMLNode]
r)HTMLCrumb -> [HTMLCrumb] -> [HTMLCrumb]
forall a. a -> [a] -> [a]
:[HTMLCrumb]
cs)
htmlZipDropAfter :: HTMLZipper -> Maybe HTMLZipper
htmlZipDropAfter :: HTMLZipper -> Maybe HTMLZipper
htmlZipDropAfter = \case
HTMLZipper HTMLNode
x [] -> Maybe HTMLZipper
forall a. Maybe a
Nothing
HTMLZipper HTMLNode
x ((HTMLCrumb HTMLNode
n [HTMLNode]
l [HTMLNode]
_):[HTMLCrumb]
cs) ->
HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just (HTMLZipper -> Maybe HTMLZipper) -> HTMLZipper -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLNode -> [HTMLCrumb] -> HTMLZipper
HTMLZipper HTMLNode
x ((HTMLNode -> [HTMLNode] -> [HTMLNode] -> HTMLCrumb
HTMLCrumb HTMLNode
n [HTMLNode]
l [])HTMLCrumb -> [HTMLCrumb] -> [HTMLCrumb]
forall a. a -> [a] -> [a]
:[HTMLCrumb]
cs)
htmlZipDropLeft :: HTMLZipper -> Maybe HTMLZipper
htmlZipDropLeft :: HTMLZipper -> Maybe HTMLZipper
htmlZipDropLeft = HTMLZipper -> Maybe HTMLZipper
htmlZipDropBefore
htmlZipDropRight :: HTMLZipper -> Maybe HTMLZipper
htmlZipDropRight :: HTMLZipper -> Maybe HTMLZipper
htmlZipDropRight = HTMLZipper -> Maybe HTMLZipper
htmlZipDropAfter
htmlZipPruneBefore :: HTMLZipper -> Maybe HTMLZipper
htmlZipPruneBefore :: HTMLZipper -> Maybe HTMLZipper
htmlZipPruneBefore = (HTMLZipper -> Maybe HTMLZipper)
-> (HTMLZipper -> Maybe HTMLZipper)
-> HTMLZipper
-> Maybe HTMLZipper
htmlZipRepeat HTMLZipper -> Maybe HTMLZipper
safeDrop HTMLZipper -> Maybe HTMLZipper
htmlZipParent
where
safeDrop :: HTMLZipper -> Maybe HTMLZipper
safeDrop HTMLZipper
z = HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just (HTMLZipper -> Maybe HTMLZipper) -> HTMLZipper -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLZipper -> Maybe HTMLZipper -> HTMLZipper
forall a. a -> Maybe a -> a
fromMaybe HTMLZipper
z (Maybe HTMLZipper -> HTMLZipper) -> Maybe HTMLZipper -> HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLZipper -> Maybe HTMLZipper
htmlZipDropBefore HTMLZipper
z
htmlZipPruneAfter :: HTMLZipper -> Maybe HTMLZipper
htmlZipPruneAfter :: HTMLZipper -> Maybe HTMLZipper
htmlZipPruneAfter = (HTMLZipper -> Maybe HTMLZipper)
-> (HTMLZipper -> Maybe HTMLZipper)
-> HTMLZipper
-> Maybe HTMLZipper
htmlZipRepeat HTMLZipper -> Maybe HTMLZipper
safeDrop HTMLZipper -> Maybe HTMLZipper
htmlZipParent
where
safeDrop :: HTMLZipper -> Maybe HTMLZipper
safeDrop HTMLZipper
z = HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just (HTMLZipper -> Maybe HTMLZipper) -> HTMLZipper -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLZipper -> Maybe HTMLZipper -> HTMLZipper
forall a. a -> Maybe a -> a
fromMaybe HTMLZipper
z (Maybe HTMLZipper -> HTMLZipper) -> Maybe HTMLZipper -> HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLZipper -> Maybe HTMLZipper
htmlZipDropAfter HTMLZipper
z
htmlZipPruneLeft :: HTMLZipper -> Maybe HTMLZipper
htmlZipPruneLeft :: HTMLZipper -> Maybe HTMLZipper
htmlZipPruneLeft = HTMLZipper -> Maybe HTMLZipper
htmlZipPruneBefore
htmlZipPruneRight :: HTMLZipper -> Maybe HTMLZipper
htmlZipPruneRight :: HTMLZipper -> Maybe HTMLZipper
htmlZipPruneRight = HTMLZipper -> Maybe HTMLZipper
htmlZipPruneAfter
htmlZipRepeat :: HTMLZipAction -> HTMLZipAction -> HTMLZipAction
htmlZipRepeat :: (HTMLZipper -> Maybe HTMLZipper)
-> (HTMLZipper -> Maybe HTMLZipper)
-> HTMLZipper
-> Maybe HTMLZipper
htmlZipRepeat HTMLZipper -> Maybe HTMLZipper
f HTMLZipper -> Maybe HTMLZipper
g HTMLZipper
z =
case HTMLZipper -> Maybe HTMLZipper
f HTMLZipper
z of
Maybe HTMLZipper
Nothing -> Maybe HTMLZipper
forall a. Maybe a
Nothing
Just HTMLZipper
z1 -> case HTMLZipper -> Maybe HTMLZipper
g HTMLZipper
z1 of
Maybe HTMLZipper
Nothing -> HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just HTMLZipper
z1
Just HTMLZipper
z2 -> (HTMLZipper -> Maybe HTMLZipper)
-> (HTMLZipper -> Maybe HTMLZipper)
-> HTMLZipper
-> Maybe HTMLZipper
htmlZipRepeat HTMLZipper -> Maybe HTMLZipper
f HTMLZipper -> Maybe HTMLZipper
g HTMLZipper
z2
htmlZipStepNext :: HTMLZipper -> Maybe HTMLZipper
htmlZipStepNext :: HTMLZipper -> Maybe HTMLZipper
htmlZipStepNext = (HTMLZipper -> Maybe HTMLZipper)
-> (HTMLZipper -> Maybe HTMLZipper)
-> HTMLZipper
-> Maybe HTMLZipper
htmlZipStep HTMLZipper -> Maybe HTMLZipper
htmlZipFirst HTMLZipper -> Maybe HTMLZipper
htmlZipNext
htmlZipStepBack :: HTMLZipper -> Maybe HTMLZipper
htmlZipStepBack :: HTMLZipper -> Maybe HTMLZipper
htmlZipStepBack = (HTMLZipper -> Maybe HTMLZipper)
-> (HTMLZipper -> Maybe HTMLZipper)
-> HTMLZipper
-> Maybe HTMLZipper
htmlZipStep HTMLZipper -> Maybe HTMLZipper
htmlZipLast HTMLZipper -> Maybe HTMLZipper
htmlZipPrev
htmlZipStep :: HTMLZipAction -> HTMLZipAction -> HTMLZipAction
htmlZipStep :: (HTMLZipper -> Maybe HTMLZipper)
-> (HTMLZipper -> Maybe HTMLZipper)
-> HTMLZipper
-> Maybe HTMLZipper
htmlZipStep HTMLZipper -> Maybe HTMLZipper
first HTMLZipper -> Maybe HTMLZipper
next HTMLZipper
z =
case HTMLZipper -> Maybe HTMLZipper
first HTMLZipper
z of
Just HTMLZipper
x -> HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just HTMLZipper
x
Maybe HTMLZipper
Nothing -> HTMLZipper -> Maybe HTMLZipper
f HTMLZipper
z
where
f :: HTMLZipper -> Maybe HTMLZipper
f HTMLZipper
x = case HTMLZipper -> Maybe HTMLZipper
next HTMLZipper
x of
Just HTMLZipper
a -> HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just HTMLZipper
a
Maybe HTMLZipper
Nothing -> case HTMLZipper -> Maybe HTMLZipper
htmlZipParent HTMLZipper
x of
Just HTMLZipper
b -> HTMLZipper -> Maybe HTMLZipper
f HTMLZipper
b
Maybe HTMLZipper
Nothing -> Maybe HTMLZipper
forall a. Maybe a
Nothing
htmlZipSearch
:: (HTMLZipper -> Maybe HTMLZipper)
-> (HTMLZipper -> Bool)
-> HTMLZipper
-> Maybe HTMLZipper
htmlZipSearch :: (HTMLZipper -> Maybe HTMLZipper)
-> (HTMLZipper -> Bool) -> HTMLZipper -> Maybe HTMLZipper
htmlZipSearch HTMLZipper -> Maybe HTMLZipper
step HTMLZipper -> Bool
test HTMLZipper
x
| HTMLZipper -> Bool
test HTMLZipper
x = HTMLZipper -> Maybe HTMLZipper
forall a. a -> Maybe a
Just HTMLZipper
x
| Bool
otherwise = Maybe HTMLZipper
-> (HTMLZipper -> Maybe HTMLZipper)
-> Maybe HTMLZipper
-> Maybe HTMLZipper
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Maybe HTMLZipper
forall a. Maybe a
Nothing ((HTMLZipper -> Maybe HTMLZipper)
-> (HTMLZipper -> Bool) -> HTMLZipper -> Maybe HTMLZipper
htmlZipSearch HTMLZipper -> Maybe HTMLZipper
step HTMLZipper -> Bool
test) (Maybe HTMLZipper -> Maybe HTMLZipper)
-> Maybe HTMLZipper -> Maybe HTMLZipper
forall a b. (a -> b) -> a -> b
$ HTMLZipper -> Maybe HTMLZipper
step HTMLZipper
x
htmlZipIndex :: HTMLZipper -> Maybe Int
htmlZipIndex :: HTMLZipper -> Maybe Int
htmlZipIndex = \case
HTMLZipper HTMLNode
_ [] -> Maybe Int
forall a. Maybe a
Nothing
HTMLZipper HTMLNode
_ ((HTMLCrumb HTMLNode
_ [HTMLNode]
ls [HTMLNode]
_):[HTMLCrumb]
_) -> Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> Int -> Maybe Int
forall a b. (a -> b) -> a -> b
$ [HTMLNode] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [HTMLNode]
ls
htmlZipDump :: HTMLZipper -> String
htmlZipDump :: HTMLZipper -> String
htmlZipDump (HTMLZipper HTMLNode
n [HTMLCrumb]
cs) =
HTMLNode -> String
name HTMLNode
n String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"\n" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> [HTMLCrumb] -> String
go [HTMLCrumb]
cs
where
go :: [HTMLCrumb] -> String
go :: [HTMLCrumb] -> String
go [] = String
""
go ((HTMLCrumb HTMLNode
n [HTMLNode]
ls [HTMLNode]
rs):[HTMLCrumb]
cs) =
HTMLNode -> String
name HTMLNode
n String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"\n"
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" ls: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> [HTMLNode] -> String
names [HTMLNode]
ls String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"\n"
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" rs: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> [HTMLNode] -> String
names [HTMLNode]
rs String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"\n"
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> [HTMLCrumb] -> String
go [HTMLCrumb]
cs
name :: HTMLNode -> String
name = Text -> String
T.unpack (Text -> String) -> (HTMLNode -> Text) -> HTMLNode -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HTMLNode -> Text
htmlElemName
names :: [HTMLNode] -> String
names = [String] -> String
forall a. Show a => a -> String
show ([String] -> String)
-> ([HTMLNode] -> [String]) -> [HTMLNode] -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (HTMLNode -> String) -> [HTMLNode] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map HTMLNode -> String
name
htmlIter :: HTMLZipper -> HTMLIter
htmlIter :: HTMLZipper -> HTMLIter
htmlIter = Direction -> HTMLZipper -> HTMLIter
HTMLIter Direction
Down
htmlIterZipper :: HTMLIter -> HTMLZipper
htmlIterZipper :: HTMLIter -> HTMLZipper
htmlIterZipper (HTMLIter Direction
_ HTMLZipper
z) = HTMLZipper
z
htmlIterModify :: (HTMLZipper -> HTMLZipper) -> HTMLIter -> HTMLIter
htmlIterModify :: (HTMLZipper -> HTMLZipper) -> HTMLIter -> HTMLIter
htmlIterModify HTMLZipper -> HTMLZipper
f (HTMLIter Direction
d HTMLZipper
z) = (Direction -> HTMLZipper -> HTMLIter
HTMLIter Direction
d (HTMLZipper -> HTMLIter) -> HTMLZipper -> HTMLIter
forall a b. (a -> b) -> a -> b
$ HTMLZipper -> HTMLZipper
f HTMLZipper
z)
htmlIterNext :: HTMLIter -> Maybe HTMLIter
htmlIterNext :: HTMLIter -> Maybe HTMLIter
htmlIterNext = (HTMLZipper -> Maybe HTMLZipper)
-> (HTMLZipper -> Maybe HTMLZipper) -> HTMLIter -> Maybe HTMLIter
iterStep HTMLZipper -> Maybe HTMLZipper
htmlZipFirst HTMLZipper -> Maybe HTMLZipper
htmlZipNext
htmlIterBack :: HTMLIter -> Maybe HTMLIter
htmlIterBack :: HTMLIter -> Maybe HTMLIter
htmlIterBack = (HTMLZipper -> Maybe HTMLZipper)
-> (HTMLZipper -> Maybe HTMLZipper) -> HTMLIter -> Maybe HTMLIter
iterStep HTMLZipper -> Maybe HTMLZipper
htmlZipLast HTMLZipper -> Maybe HTMLZipper
htmlZipPrev
iterStep
:: (HTMLZipper -> Maybe HTMLZipper)
-> (HTMLZipper -> Maybe HTMLZipper)
-> HTMLIter
-> Maybe HTMLIter
iterStep :: (HTMLZipper -> Maybe HTMLZipper)
-> (HTMLZipper -> Maybe HTMLZipper) -> HTMLIter -> Maybe HTMLIter
iterStep HTMLZipper -> Maybe HTMLZipper
first HTMLZipper -> Maybe HTMLZipper
next = HTMLIter -> Maybe HTMLIter
go
where
go :: HTMLIter -> Maybe HTMLIter
go (HTMLIter Direction
d HTMLZipper
z) =
case Direction
d of
Direction
Down ->
case HTMLZipper -> Maybe HTMLZipper
first HTMLZipper
z of
Just HTMLZipper
x -> HTMLIter -> Maybe HTMLIter
forall a. a -> Maybe a
Just (HTMLIter -> Maybe HTMLIter) -> HTMLIter -> Maybe HTMLIter
forall a b. (a -> b) -> a -> b
$ Direction -> HTMLZipper -> HTMLIter
HTMLIter Direction
Down HTMLZipper
x
Maybe HTMLZipper
Nothing -> HTMLIter -> Maybe HTMLIter
go (HTMLIter -> Maybe HTMLIter) -> HTMLIter -> Maybe HTMLIter
forall a b. (a -> b) -> a -> b
$ Direction -> HTMLZipper -> HTMLIter
HTMLIter Direction
Across HTMLZipper
z
Direction
Across ->
case HTMLZipper -> Maybe HTMLZipper
next HTMLZipper
z of
Just HTMLZipper
x -> HTMLIter -> Maybe HTMLIter
forall a. a -> Maybe a
Just (HTMLIter -> Maybe HTMLIter) -> HTMLIter -> Maybe HTMLIter
forall a b. (a -> b) -> a -> b
$ Direction -> HTMLZipper -> HTMLIter
HTMLIter Direction
Down HTMLZipper
x
Maybe HTMLZipper
Nothing -> HTMLIter -> Maybe HTMLIter
go (HTMLIter -> Maybe HTMLIter) -> HTMLIter -> Maybe HTMLIter
forall a b. (a -> b) -> a -> b
$ Direction -> HTMLZipper -> HTMLIter
HTMLIter Direction
Up HTMLZipper
z
Direction
Up ->
case HTMLZipper -> Maybe HTMLZipper
htmlZipParent HTMLZipper
z of
Just HTMLZipper
x -> HTMLIter -> Maybe HTMLIter
forall a. a -> Maybe a
Just (HTMLIter -> Maybe HTMLIter) -> HTMLIter -> Maybe HTMLIter
forall a b. (a -> b) -> a -> b
$ Direction -> HTMLZipper -> HTMLIter
HTMLIter Direction
Across HTMLZipper
x
Maybe HTMLZipper
Nothing -> Maybe HTMLIter
forall a. Maybe a
Nothing
htmlIterSearch
:: (HTMLIter -> Maybe HTMLIter)
-> (HTMLZipper -> Bool)
-> HTMLIter
-> Maybe HTMLIter
htmlIterSearch :: (HTMLIter -> Maybe HTMLIter)
-> (HTMLZipper -> Bool) -> HTMLIter -> Maybe HTMLIter
htmlIterSearch HTMLIter -> Maybe HTMLIter
step HTMLZipper -> Bool
test x :: HTMLIter
x@(HTMLIter Direction
_ HTMLZipper
z)
| HTMLZipper -> Bool
test HTMLZipper
z = HTMLIter -> Maybe HTMLIter
forall a. a -> Maybe a
Just HTMLIter
x
| Bool
otherwise = Maybe HTMLIter
-> (HTMLIter -> Maybe HTMLIter) -> Maybe HTMLIter -> Maybe HTMLIter
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Maybe HTMLIter
forall a. Maybe a
Nothing ((HTMLIter -> Maybe HTMLIter)
-> (HTMLZipper -> Bool) -> HTMLIter -> Maybe HTMLIter
htmlIterSearch HTMLIter -> Maybe HTMLIter
step HTMLZipper -> Bool
test) (Maybe HTMLIter -> Maybe HTMLIter)
-> Maybe HTMLIter -> Maybe HTMLIter
forall a b. (a -> b) -> a -> b
$ HTMLIter -> Maybe HTMLIter
step HTMLIter
x
htmlZipPathEmpty :: HTMLZipPath
htmlZipPathEmpty :: HTMLZipPath
htmlZipPathEmpty = [Int] -> HTMLZipPath
HTMLZipPath []
htmlZipPath :: HTMLZipper -> HTMLZipPath
htmlZipPath :: HTMLZipper -> HTMLZipPath
htmlZipPath = HTMLZipPath
-> (HTMLZipPath -> HTMLZipPath) -> Maybe HTMLZipPath -> HTMLZipPath
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([Int] -> HTMLZipPath
HTMLZipPath []) HTMLZipPath -> HTMLZipPath
forall a. a -> a
id (Maybe HTMLZipPath -> HTMLZipPath)
-> (HTMLZipper -> Maybe HTMLZipPath) -> HTMLZipper -> HTMLZipPath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HTMLZipPath -> HTMLZipper -> Maybe HTMLZipPath
go HTMLZipPath
htmlZipPathEmpty
where
go :: HTMLZipPath -> HTMLZipper -> Maybe HTMLZipPath
go :: HTMLZipPath -> HTMLZipper -> Maybe HTMLZipPath
go (HTMLZipPath [Int]
p) HTMLZipper
z =
case HTMLZipper -> Maybe Int
htmlZipIndex HTMLZipper
z of
Maybe Int
Nothing ->
HTMLZipPath -> Maybe HTMLZipPath
forall a. a -> Maybe a
Just (HTMLZipPath -> Maybe HTMLZipPath)
-> HTMLZipPath -> Maybe HTMLZipPath
forall a b. (a -> b) -> a -> b
$ [Int] -> HTMLZipPath
HTMLZipPath [Int]
p
Just Int
x ->
case HTMLZipper -> Maybe HTMLZipper
htmlZipParent HTMLZipper
z of
Maybe HTMLZipper
Nothing -> Maybe HTMLZipPath
forall a. Maybe a
Nothing
Just HTMLZipper
y -> HTMLZipPath -> HTMLZipper -> Maybe HTMLZipPath
go ([Int] -> HTMLZipPath
HTMLZipPath ([Int] -> HTMLZipPath) -> [Int] -> HTMLZipPath
forall a b. (a -> b) -> a -> b
$ Int
x Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: [Int]
p) HTMLZipper
y
htmlZipPathFind :: HTMLZipPath -> HTMLZipper -> Maybe HTMLZipper
htmlZipPathFind :: HTMLZipPath -> HTMLZipper -> Maybe HTMLZipper
htmlZipPathFind (HTMLZipPath [Int]
p) = [Int] -> HTMLZipper -> Maybe HTMLZipper
f [Int]
p
where
f :: [Int] -> HTMLZipper -> Maybe HTMLZipper
f [] = HTMLZipper -> Maybe HTMLZipper
forall (f :: * -> *) a. Applicative f => a -> f a
pure
f (Int
x:[Int]
xs) = Int -> HTMLZipper -> Maybe HTMLZipper
htmlZipGet Int
x (HTMLZipper -> Maybe HTMLZipper)
-> (HTMLZipper -> Maybe HTMLZipper)
-> HTMLZipper
-> Maybe HTMLZipper
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> [Int] -> HTMLZipper -> Maybe HTMLZipper
f [Int]
xs