{-# LANGUAGE TupleSections #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE CPP #-}
{-# OPTIONS_HADDOCK hide #-}
module Text.HTML.Scalpel.Internal.Serial (
    SerialScraper
,   SerialScraperT
,   inSerial
,   stepBack
,   stepNext
,   seekBack
,   seekNext
,   untilBack
,   untilNext
) where

import Text.HTML.Scalpel.Internal.Scrape
import Text.HTML.Scalpel.Internal.Select

import Control.Applicative
import Control.Monad
import Control.Monad.Trans
import Control.Monad.Except (MonadError)
import Control.Monad.Cont (MonadCont)
import Control.Monad.Reader
import Control.Monad.State
import Control.Monad.Trans.Maybe
import Control.Monad.Writer (MonadWriter)
import Control.Monad.Fix
import Data.Bifunctor
import Data.Functor.Identity
import Data.List.PointedList (PointedList)
import Data.Maybe
import Prelude hiding (until)

import qualified Control.Monad.Fail as Fail
import qualified Data.List.PointedList as PointedList
import qualified Data.Tree as Tree
import qualified Text.StringLike as TagSoup


-- | Serial scrapers operate on a zipper of tag specs that correspond to the
-- root nodes / siblings in a document.
--
-- Access to the zipper is always performed in a move-then-read manner. For this
-- reason it is valid for the current focus of the zipper to be just off either
-- end of list such that moving forward or backward would result in reading the
-- first or last node.
--
-- These valid focuses are expressed as Nothing values at either end of the
-- zipper since they are valid positions for the focus to pass over, but not
-- valid positions to read.
type SpecZipper str = PointedList (Maybe (TagSpec str))

-- | A 'SerialScraper' allows for the application of 'Scraper's on a sequence of
-- sibling nodes. This allows for use cases like targeting the sibling of a
-- node, or extracting a sequence of sibling nodes (e.g. paragraphs (\<p\>)
-- under a header (\<h2\>)).
--
-- Conceptually serial scrapers operate on a sequence of tags that correspond to
-- the immediate children of the currently focused node. For example, given the
-- following HTML:
--
-- @
--  \<article\>
--    \<h1\>title\</h1\>
--    \<h2\>Section 1\</h2\>
--    \<p\>Paragraph 1.1\</p\>
--    \<p\>Paragraph 1.2\</p\>
--    \<h2\>Section 2\</h2\>
--    \<p\>Paragraph 2.1\</p\>
--    \<p\>Paragraph 2.2\</p\>
--  \</article\>
-- @
--
-- A serial scraper that visits the header and paragraph nodes can be executed
-- with the following:
--
-- @
-- 'chroot' "article" $ 'inSerial' $ do ...
-- @
--
-- Each 'SerialScraper' primitive follows the pattern of first moving the focus
-- backward or forward and then extracting content from the new focus.
-- Attempting to extract content from beyond the end of the sequence causes the
-- scraper to fail.
--
-- To complete the above example, the article's structure and content can be
-- extracted with the following code:
--
-- @
-- 'chroot' "article" $ 'inSerial' $ do
--     title <- 'seekNext' $ 'text' "h1"
--     sections <- many $ do
--        section <- 'seekNext' $ text "h2"
--        ps <- 'untilNext' ('matches' "h2") (many $ 'seekNext' $ 'text' "p")
--        return (section, ps)
--     return (title, sections)
-- @
--
-- Which will evaluate to:
--
-- @
--  ("title", [
--    ("Section 1", ["Paragraph 1.1", "Paragraph 1.2"]),
--    ("Section 2", ["Paragraph 2.1", "Paragraph 2.2"]),
--  ])
-- @
type SerialScraper str a = SerialScraperT str Identity a

-- | Run a serial scraper transforming over a monad 'm'.
newtype SerialScraperT str m a =
    MkSerialScraper (StateT (SpecZipper str) (MaybeT m) a)
    deriving (forall a b. a -> SerialScraperT str m b -> SerialScraperT str m a
forall a b.
(a -> b) -> SerialScraperT str m a -> SerialScraperT str m b
forall str (m :: * -> *) a b.
Functor m =>
a -> SerialScraperT str m b -> SerialScraperT str m a
forall str (m :: * -> *) a b.
Functor m =>
(a -> b) -> SerialScraperT str m a -> SerialScraperT str m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> SerialScraperT str m b -> SerialScraperT str m a
$c<$ :: forall str (m :: * -> *) a b.
Functor m =>
a -> SerialScraperT str m b -> SerialScraperT str m a
fmap :: forall a b.
(a -> b) -> SerialScraperT str m a -> SerialScraperT str m b
$cfmap :: forall str (m :: * -> *) a b.
Functor m =>
(a -> b) -> SerialScraperT str m a -> SerialScraperT str m b
Functor, forall a. a -> SerialScraperT str m a
forall a b.
SerialScraperT str m a
-> SerialScraperT str m b -> SerialScraperT str m a
forall a b.
SerialScraperT str m a
-> SerialScraperT str m b -> SerialScraperT str m b
forall a b.
SerialScraperT str m (a -> b)
-> SerialScraperT str m a -> SerialScraperT str m b
forall a b c.
(a -> b -> c)
-> SerialScraperT str m a
-> SerialScraperT str m b
-> SerialScraperT str m c
forall {str} {m :: * -> *}.
Monad m =>
Functor (SerialScraperT str m)
forall str (m :: * -> *) a. Monad m => a -> SerialScraperT str m a
forall str (m :: * -> *) a b.
Monad m =>
SerialScraperT str m a
-> SerialScraperT str m b -> SerialScraperT str m a
forall str (m :: * -> *) a b.
Monad m =>
SerialScraperT str m a
-> SerialScraperT str m b -> SerialScraperT str m b
forall str (m :: * -> *) a b.
Monad m =>
SerialScraperT str m (a -> b)
-> SerialScraperT str m a -> SerialScraperT str m b
forall str (m :: * -> *) a b c.
Monad m =>
(a -> b -> c)
-> SerialScraperT str m a
-> SerialScraperT str m b
-> SerialScraperT str m c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: forall a b.
SerialScraperT str m a
-> SerialScraperT str m b -> SerialScraperT str m a
$c<* :: forall str (m :: * -> *) a b.
Monad m =>
SerialScraperT str m a
-> SerialScraperT str m b -> SerialScraperT str m a
*> :: forall a b.
SerialScraperT str m a
-> SerialScraperT str m b -> SerialScraperT str m b
$c*> :: forall str (m :: * -> *) a b.
Monad m =>
SerialScraperT str m a
-> SerialScraperT str m b -> SerialScraperT str m b
liftA2 :: forall a b c.
(a -> b -> c)
-> SerialScraperT str m a
-> SerialScraperT str m b
-> SerialScraperT str m c
$cliftA2 :: forall str (m :: * -> *) a b c.
Monad m =>
(a -> b -> c)
-> SerialScraperT str m a
-> SerialScraperT str m b
-> SerialScraperT str m c
<*> :: forall a b.
SerialScraperT str m (a -> b)
-> SerialScraperT str m a -> SerialScraperT str m b
$c<*> :: forall str (m :: * -> *) a b.
Monad m =>
SerialScraperT str m (a -> b)
-> SerialScraperT str m a -> SerialScraperT str m b
pure :: forall a. a -> SerialScraperT str m a
$cpure :: forall str (m :: * -> *) a. Monad m => a -> SerialScraperT str m a
Applicative, forall a. SerialScraperT str m a
forall a. SerialScraperT str m a -> SerialScraperT str m [a]
forall a.
SerialScraperT str m a
-> SerialScraperT str m a -> SerialScraperT str m a
forall str (m :: * -> *).
Monad m =>
Applicative (SerialScraperT str m)
forall str (m :: * -> *) a. Monad m => SerialScraperT str m a
forall str (m :: * -> *) a.
Monad m =>
SerialScraperT str m a -> SerialScraperT str m [a]
forall str (m :: * -> *) a.
Monad m =>
SerialScraperT str m a
-> SerialScraperT str m a -> SerialScraperT str m a
forall (f :: * -> *).
Applicative f
-> (forall a. f a)
-> (forall a. f a -> f a -> f a)
-> (forall a. f a -> f [a])
-> (forall a. f a -> f [a])
-> Alternative f
many :: forall a. SerialScraperT str m a -> SerialScraperT str m [a]
$cmany :: forall str (m :: * -> *) a.
Monad m =>
SerialScraperT str m a -> SerialScraperT str m [a]
some :: forall a. SerialScraperT str m a -> SerialScraperT str m [a]
$csome :: forall str (m :: * -> *) a.
Monad m =>
SerialScraperT str m a -> SerialScraperT str m [a]
<|> :: forall a.
SerialScraperT str m a
-> SerialScraperT str m a -> SerialScraperT str m a
$c<|> :: forall str (m :: * -> *) a.
Monad m =>
SerialScraperT str m a
-> SerialScraperT str m a -> SerialScraperT str m a
empty :: forall a. SerialScraperT str m a
$cempty :: forall str (m :: * -> *) a. Monad m => SerialScraperT str m a
Alternative, forall a. a -> SerialScraperT str m a
forall a b.
SerialScraperT str m a
-> SerialScraperT str m b -> SerialScraperT str m b
forall a b.
SerialScraperT str m a
-> (a -> SerialScraperT str m b) -> SerialScraperT str m b
forall str (m :: * -> *).
Monad m =>
Applicative (SerialScraperT str m)
forall str (m :: * -> *) a. Monad m => a -> SerialScraperT str m a
forall str (m :: * -> *) a b.
Monad m =>
SerialScraperT str m a
-> SerialScraperT str m b -> SerialScraperT str m b
forall str (m :: * -> *) a b.
Monad m =>
SerialScraperT str m a
-> (a -> SerialScraperT str m b) -> SerialScraperT str m b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: forall a. a -> SerialScraperT str m a
$creturn :: forall str (m :: * -> *) a. Monad m => a -> SerialScraperT str m a
>> :: forall a b.
SerialScraperT str m a
-> SerialScraperT str m b -> SerialScraperT str m b
$c>> :: forall str (m :: * -> *) a b.
Monad m =>
SerialScraperT str m a
-> SerialScraperT str m b -> SerialScraperT str m b
>>= :: forall a b.
SerialScraperT str m a
-> (a -> SerialScraperT str m b) -> SerialScraperT str m b
$c>>= :: forall str (m :: * -> *) a b.
Monad m =>
SerialScraperT str m a
-> (a -> SerialScraperT str m b) -> SerialScraperT str m b
Monad, forall a. SerialScraperT str m a
forall a.
SerialScraperT str m a
-> SerialScraperT str m a -> SerialScraperT str m a
forall str (m :: * -> *). Monad m => Monad (SerialScraperT str m)
forall str (m :: * -> *).
Monad m =>
Alternative (SerialScraperT str m)
forall str (m :: * -> *) a. Monad m => SerialScraperT str m a
forall str (m :: * -> *) a.
Monad m =>
SerialScraperT str m a
-> SerialScraperT str m a -> SerialScraperT str m a
forall (m :: * -> *).
Alternative m
-> Monad m
-> (forall a. m a)
-> (forall a. m a -> m a -> m a)
-> MonadPlus m
mplus :: forall a.
SerialScraperT str m a
-> SerialScraperT str m a -> SerialScraperT str m a
$cmplus :: forall str (m :: * -> *) a.
Monad m =>
SerialScraperT str m a
-> SerialScraperT str m a -> SerialScraperT str m a
mzero :: forall a. SerialScraperT str m a
$cmzero :: forall str (m :: * -> *) a. Monad m => SerialScraperT str m a
MonadPlus, forall a. (a -> SerialScraperT str m a) -> SerialScraperT str m a
forall {str} {m :: * -> *}.
MonadFix m =>
Monad (SerialScraperT str m)
forall str (m :: * -> *) a.
MonadFix m =>
(a -> SerialScraperT str m a) -> SerialScraperT str m a
forall (m :: * -> *).
Monad m -> (forall a. (a -> m a) -> m a) -> MonadFix m
mfix :: forall a. (a -> SerialScraperT str m a) -> SerialScraperT str m a
$cmfix :: forall str (m :: * -> *) a.
MonadFix m =>
(a -> SerialScraperT str m a) -> SerialScraperT str m a
MonadFix,
              forall a. IO a -> SerialScraperT str m a
forall {str} {m :: * -> *}.
MonadIO m =>
Monad (SerialScraperT str m)
forall str (m :: * -> *) a.
MonadIO m =>
IO a -> SerialScraperT str m a
forall (m :: * -> *).
Monad m -> (forall a. IO a -> m a) -> MonadIO m
liftIO :: forall a. IO a -> SerialScraperT str m a
$cliftIO :: forall str (m :: * -> *) a.
MonadIO m =>
IO a -> SerialScraperT str m a
MonadIO, forall a b.
((a -> SerialScraperT str m b) -> SerialScraperT str m a)
-> SerialScraperT str m a
forall {str} {m :: * -> *}.
MonadCont m =>
Monad (SerialScraperT str m)
forall str (m :: * -> *) a b.
MonadCont m =>
((a -> SerialScraperT str m b) -> SerialScraperT str m a)
-> SerialScraperT str m a
forall (m :: * -> *).
Monad m -> (forall a b. ((a -> m b) -> m a) -> m a) -> MonadCont m
callCC :: forall a b.
((a -> SerialScraperT str m b) -> SerialScraperT str m a)
-> SerialScraperT str m a
$ccallCC :: forall str (m :: * -> *) a b.
MonadCont m =>
((a -> SerialScraperT str m b) -> SerialScraperT str m a)
-> SerialScraperT str m a
MonadCont, MonadError e, MonadReader r, MonadWriter w)

#if MIN_VERSION_base(4,9,0)
deriving instance Monad m => Fail.MonadFail (SerialScraperT str m)
#else
instance Fail.MonadFail m => Fail.MonadFail (SerialScraperT str m) where
  fail = lift . Fail.fail
#endif

instance MonadTrans (SerialScraperT str) where
  lift :: forall (m :: * -> *) a. Monad m => m a -> SerialScraperT str m a
lift m a
op = forall str (m :: * -> *) a.
StateT (SpecZipper str) (MaybeT m) a -> SerialScraperT str m a
MkSerialScraper forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ m a
op

instance MonadState s m => MonadState s (SerialScraperT str m) where
  get :: SerialScraperT str m s
get = forall str (m :: * -> *) a.
StateT (SpecZipper str) (MaybeT m) a -> SerialScraperT str m a
MkSerialScraper (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall s (m :: * -> *). MonadState s m => m s
get)
  put :: s -> SerialScraperT str m ()
put = forall str (m :: * -> *) a.
StateT (SpecZipper str) (MaybeT m) a -> SerialScraperT str m a
MkSerialScraper forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *). MonadState s m => s -> m ()
put

-- | Executes a 'SerialScraper' in the context of a 'Scraper'. The immediate
-- children of the currently focused node are visited serially.
inSerial :: (TagSoup.StringLike str, Monad m)
    => SerialScraperT str m a -> ScraperT str m a
inSerial :: forall str (m :: * -> *) a.
(StringLike str, Monad m) =>
SerialScraperT str m a -> ScraperT str m a
inSerial (MkSerialScraper StateT (SpecZipper str) (MaybeT m) a
serialScraper) = forall str (m :: * -> *) a.
ReaderT (TagSpec str) (MaybeT m) a -> ScraperT str m a
MkScraper forall a b. (a -> b) -> a -> b
$ forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT forall a b. (a -> b) -> a -> b
$ (TagVector str, [Tree Span], SelectContext) -> MaybeT m a
scraper
  where
    scraper :: (TagVector str, [Tree Span], SelectContext) -> MaybeT m a
scraper spec :: (TagVector str, [Tree Span], SelectContext)
spec@(TagVector str
vec, Tree Span
root : [Tree Span]
_, SelectContext
ctx)
      | SelectContext -> Bool
ctxInChroot SelectContext
ctx = forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT StateT (SpecZipper str) (MaybeT m) a
serialScraper
                                  (forall {str}.
StringLike str =>
(TagVector str, [Tree Span], SelectContext) -> SpecZipper str
toZipper (TagVector str
vec, forall a. Tree a -> [Tree a]
Tree.subForest Tree Span
root, SelectContext
ctx))
      | Bool
otherwise       = forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT StateT (SpecZipper str) (MaybeT m) a
serialScraper (forall {str}.
StringLike str =>
(TagVector str, [Tree Span], SelectContext) -> SpecZipper str
toZipper (TagVector str, [Tree Span], SelectContext)
spec)
    scraper (TagVector str, [Tree Span], SelectContext)
_           = forall (f :: * -> *) a. Alternative f => f a
empty

    -- Create a zipper from the current tag spec by generating a new tag spec
    -- that just contains each root node in the forest.
    toZipper :: (TagVector str, [Tree Span], SelectContext) -> SpecZipper str
toZipper (TagVector str
vector, [Tree Span]
forest, SelectContext
context) =
        forall str. StringLike str => [TagSpec str] -> SpecZipper str
zipperFromList forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map ((TagVector str
vector, , SelectContext
context) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => a -> m a
return) [Tree Span]
forest

-- | Creates a SpecZipper from a list of tag specs. This requires bookending the
-- zipper with Nothing values to denote valid focuses that are just off either
-- end of the list.
zipperFromList :: TagSoup.StringLike str => [TagSpec str] -> SpecZipper str
zipperFromList :: forall str. StringLike str => [TagSpec str] -> SpecZipper str
zipperFromList = forall a. a -> PointedList a -> PointedList a
PointedList.insertLeft forall a. Maybe a
Nothing
               forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (forall a. a -> PointedList a -> PointedList a
PointedList.insertLeft forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just)
                       (forall a. a -> PointedList a
PointedList.singleton forall a. Maybe a
Nothing)

stepWith :: (TagSoup.StringLike str, Monad m)
         => (SpecZipper str -> Maybe (SpecZipper str))
         -> ScraperT str m b
         -> SerialScraperT str m b
stepWith :: forall str (m :: * -> *) b.
(StringLike str, Monad m) =>
(SpecZipper str -> Maybe (SpecZipper str))
-> ScraperT str m b -> SerialScraperT str m b
stepWith SpecZipper str -> Maybe (SpecZipper str)
moveList (MkScraper (ReaderT TagSpec str -> MaybeT m b
scraper)) = forall str (m :: * -> *) a.
StateT (SpecZipper str) (MaybeT m) a -> SerialScraperT str m a
MkSerialScraper forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StateT forall a b. (a -> b) -> a -> b
$
    \SpecZipper str
zipper -> do
        SpecZipper str
zipper' <- forall (m :: * -> *) a. Monad m => Maybe a -> MaybeT m a
maybeT forall a b. (a -> b) -> a -> b
$ SpecZipper str -> Maybe (SpecZipper str)
moveList SpecZipper str
zipper
        TagSpec str
focus <- forall (m :: * -> *) a. Monad m => Maybe a -> MaybeT m a
maybeT forall a b. (a -> b) -> a -> b
$ forall a. PointedList a -> a
PointedList._focus SpecZipper str
zipper'
        b
value <- TagSpec str -> MaybeT m b
scraper TagSpec str
focus
        forall (m :: * -> *) a. Monad m => a -> m a
return (b
value, SpecZipper str
zipper')

-- | Move the cursor back one node and execute the given scraper on the new
-- focused node.
stepBack :: (TagSoup.StringLike str, Monad m) => ScraperT str m a -> SerialScraperT str m a
stepBack :: forall str (m :: * -> *) a.
(StringLike str, Monad m) =>
ScraperT str m a -> SerialScraperT str m a
stepBack = forall str (m :: * -> *) b.
(StringLike str, Monad m) =>
(SpecZipper str -> Maybe (SpecZipper str))
-> ScraperT str m b -> SerialScraperT str m b
stepWith forall a. PointedList a -> Maybe (PointedList a)
PointedList.previous

-- | Move the cursor forward one node and execute the given scraper on the new
-- focused node.
stepNext :: (TagSoup.StringLike str, Monad m)
    => ScraperT str m a -> SerialScraperT str m a
stepNext :: forall str (m :: * -> *) a.
(StringLike str, Monad m) =>
ScraperT str m a -> SerialScraperT str m a
stepNext = forall str (m :: * -> *) b.
(StringLike str, Monad m) =>
(SpecZipper str -> Maybe (SpecZipper str))
-> ScraperT str m b -> SerialScraperT str m b
stepWith forall a. PointedList a -> Maybe (PointedList a)
PointedList.next

seekWith :: (TagSoup.StringLike str, Monad m)
         => (SpecZipper str -> Maybe (SpecZipper str))
         -> ScraperT str m b
         -> SerialScraperT str m b
seekWith :: forall str (m :: * -> *) b.
(StringLike str, Monad m) =>
(SpecZipper str -> Maybe (SpecZipper str))
-> ScraperT str m b -> SerialScraperT str m b
seekWith SpecZipper str -> Maybe (SpecZipper str)
moveList (MkScraper (ReaderT TagSpec str -> MaybeT m b
scraper)) = forall str (m :: * -> *) a.
StateT (SpecZipper str) (MaybeT m) a -> SerialScraperT str m a
MkSerialScraper (forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StateT SpecZipper str -> MaybeT m (b, SpecZipper str)
go)
    where
      go :: SpecZipper str -> MaybeT m (b, SpecZipper str)
go SpecZipper str
zipper = do SpecZipper str
zipper' <- forall (m :: * -> *) a. Monad m => Maybe a -> MaybeT m a
maybeT forall a b. (a -> b) -> a -> b
$ SpecZipper str -> Maybe (SpecZipper str)
moveList SpecZipper str
zipper
                     SpecZipper str -> MaybeT m (b, SpecZipper str)
runScraper SpecZipper str
zipper' forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> SpecZipper str -> MaybeT m (b, SpecZipper str)
go SpecZipper str
zipper'
      runScraper :: SpecZipper str -> MaybeT m (b, SpecZipper str)
runScraper SpecZipper str
zipper = do
        TagSpec str
focus <- forall (m :: * -> *) a. Monad m => Maybe a -> MaybeT m a
maybeT forall a b. (a -> b) -> a -> b
$ forall a. PointedList a -> a
PointedList._focus SpecZipper str
zipper
        b
value <- TagSpec str -> MaybeT m b
scraper TagSpec str
focus
        forall (m :: * -> *) a. Monad m => a -> m a
return (b
value, SpecZipper str
zipper)

-- | Move the cursor backward until the given scraper is successfully able to
-- execute on the focused node. If the scraper is never successful then the
-- serial scraper will fail.
seekBack :: (TagSoup.StringLike str, Monad m)
    => ScraperT str m a -> SerialScraperT str m a
seekBack :: forall str (m :: * -> *) a.
(StringLike str, Monad m) =>
ScraperT str m a -> SerialScraperT str m a
seekBack = forall str (m :: * -> *) b.
(StringLike str, Monad m) =>
(SpecZipper str -> Maybe (SpecZipper str))
-> ScraperT str m b -> SerialScraperT str m b
seekWith forall a. PointedList a -> Maybe (PointedList a)
PointedList.previous

-- | Move the cursor forward until the given scraper is successfully able to
-- execute on the focused node. If the scraper is never successful then the
-- serial scraper will fail.
seekNext :: (TagSoup.StringLike str, Monad m)
    => ScraperT str m a -> SerialScraperT str m a
seekNext :: forall str (m :: * -> *) a.
(StringLike str, Monad m) =>
ScraperT str m a -> SerialScraperT str m a
seekNext = forall str (m :: * -> *) b.
(StringLike str, Monad m) =>
(SpecZipper str -> Maybe (SpecZipper str))
-> ScraperT str m b -> SerialScraperT str m b
seekWith forall a. PointedList a -> Maybe (PointedList a)
PointedList.next

untilWith :: (TagSoup.StringLike str, Monad m)
         => (SpecZipper str -> Maybe (SpecZipper str))
         -> (Maybe (TagSpec str) -> SpecZipper str -> SpecZipper str)
         -> ScraperT str m a
         -> SerialScraperT str m b
         -> SerialScraperT str m b
untilWith :: forall str (m :: * -> *) a b.
(StringLike str, Monad m) =>
(SpecZipper str -> Maybe (SpecZipper str))
-> (Maybe (TagSpec str) -> SpecZipper str -> SpecZipper str)
-> ScraperT str m a
-> SerialScraperT str m b
-> SerialScraperT str m b
untilWith SpecZipper str -> Maybe (SpecZipper str)
moveList Maybe (TagSpec str) -> SpecZipper str -> SpecZipper str
appendNode (MkScraper (ReaderT TagSpec str -> MaybeT m a
until)) (MkSerialScraper StateT (SpecZipper str) (MaybeT m) b
scraper) =
  forall str (m :: * -> *) a.
StateT (SpecZipper str) (MaybeT m) a -> SerialScraperT str m a
MkSerialScraper forall a b. (a -> b) -> a -> b
$ do
    SpecZipper str
inner <- forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StateT SpecZipper str -> MaybeT m (SpecZipper str, SpecZipper str)
split
    forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT StateT (SpecZipper str) (MaybeT m) b
scraper (Maybe (TagSpec str) -> SpecZipper str -> SpecZipper str
appendNode forall a. Maybe a
Nothing SpecZipper str
inner))
    where
      split :: SpecZipper str -> MaybeT m (SpecZipper str, SpecZipper str)
split SpecZipper str
zipper =
        do SpecZipper str
zipper' <- forall (m :: * -> *) a. Monad m => Maybe a -> MaybeT m a
maybeT forall a b. (a -> b) -> a -> b
$ SpecZipper str -> Maybe (SpecZipper str)
moveList SpecZipper str
zipper
           TagSpec str
spec <- forall (m :: * -> *) a. Monad m => Maybe a -> MaybeT m a
maybeT forall a b. (a -> b) -> a -> b
$ forall a. PointedList a -> a
PointedList._focus SpecZipper str
zipper'
           do TagSpec str -> MaybeT m a
until TagSpec str
spec
              forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> PointedList a
PointedList.singleton forall a. Maybe a
Nothing, SpecZipper str
zipper)
            forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (Maybe (TagSpec str) -> SpecZipper str -> SpecZipper str
appendNode (forall a. a -> Maybe a
Just TagSpec str
spec)) forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` SpecZipper str -> MaybeT m (SpecZipper str, SpecZipper str)
split SpecZipper str
zipper')
         forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> PointedList a
PointedList.singleton forall a. Maybe a
Nothing, SpecZipper str
zipper)

-- | Create a new serial context by moving the focus backward and collecting
-- nodes until the scraper matches the focused node. The serial scraper is then
-- executed on the collected nodes.
untilBack :: (TagSoup.StringLike str, Monad m)
          => ScraperT str m a -> SerialScraperT str m b -> SerialScraperT str m b
untilBack :: forall str (m :: * -> *) a b.
(StringLike str, Monad m) =>
ScraperT str m a
-> SerialScraperT str m b -> SerialScraperT str m b
untilBack = forall str (m :: * -> *) a b.
(StringLike str, Monad m) =>
(SpecZipper str -> Maybe (SpecZipper str))
-> (Maybe (TagSpec str) -> SpecZipper str -> SpecZipper str)
-> ScraperT str m a
-> SerialScraperT str m b
-> SerialScraperT str m b
untilWith forall a. PointedList a -> Maybe (PointedList a)
PointedList.previous forall a. a -> PointedList a -> PointedList a
PointedList.insertRight

-- | Create a new serial context by moving the focus forward and collecting
-- nodes until the scraper matches the focused node. The serial scraper is then
-- executed on the collected nodes.
--
-- The provided serial scraper is unable to see nodes outside the new restricted
-- context.
untilNext :: (TagSoup.StringLike str, Monad m)
          => ScraperT str m a -> SerialScraperT str m b -> SerialScraperT str m b
untilNext :: forall str (m :: * -> *) a b.
(StringLike str, Monad m) =>
ScraperT str m a
-> SerialScraperT str m b -> SerialScraperT str m b
untilNext = forall str (m :: * -> *) a b.
(StringLike str, Monad m) =>
(SpecZipper str -> Maybe (SpecZipper str))
-> (Maybe (TagSpec str) -> SpecZipper str -> SpecZipper str)
-> ScraperT str m a
-> SerialScraperT str m b
-> SerialScraperT str m b
untilWith forall a. PointedList a -> Maybe (PointedList a)
PointedList.next forall a. a -> PointedList a -> PointedList a
PointedList.insertLeft

maybeT :: Monad m => Maybe a -> MaybeT m a
maybeT :: forall (m :: * -> *) a. Monad m => Maybe a -> MaybeT m a
maybeT = forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => a -> m a
return