{-# LANGUAGE FlexibleContexts     #-}
{-# LANGUAGE OverloadedStrings    #-}
{- |
Copyright   : © 2021-2023 Albert Krewinkel
License     : MIT
Maintainer  : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>

Shared functions used in multiple types.
-}
module Text.Pandoc.Lua.Marshal.Shared
  ( -- * Walking
    walkBlocksAndInlines
  ) where

import Prelude hiding (lookup)
import Control.Monad ((>=>))
import HsLua
import {-# SOURCE #-} Text.Pandoc.Lua.Marshal.Block
import {-# SOURCE #-} Text.Pandoc.Lua.Marshal.Inline
import Text.Pandoc.Lua.Marshal.Filter
import Text.Pandoc.Definition
import Text.Pandoc.Lua.Topdown
import Text.Pandoc.Lua.Walk
import Text.Pandoc.Walk

-- | Walk blocks and inlines.
walkBlocksAndInlines :: (LuaError e,
                         Walkable (SpliceList Block) a,
                         Walkable (SpliceList Inline) a,
                         Walkable [Block] a,
                         Walkable [Inline] a,
                         Walkable Topdown a)
                     => Filter
                     -> a -> LuaE e a
walkBlocksAndInlines :: forall e a.
(LuaError e, Walkable (SpliceList Block) a,
 Walkable (SpliceList Inline) a, Walkable [Block] a,
 Walkable [Inline] a, Walkable Topdown a) =>
Filter -> a -> LuaE e a
walkBlocksAndInlines Filter
filter' =
  case Filter -> WalkingOrder
filterWalkingOrder Filter
filter' of
    WalkingOrder
WalkTopdown     -> forall a b (m :: * -> *).
(Walkable a b, Monad m, Applicative m, Functor m) =>
(a -> m a) -> b -> m b
walkM (forall e. LuaError e => Filter -> Topdown -> LuaE e Topdown
applyFilterTopdown Filter
filter')
    WalkingOrder
WalkForEachType -> forall e a.
(LuaError e, Walkable (SpliceList Inline) a) =>
Filter -> a -> LuaE e a
walkInlineSplicing Filter
filter'
                   forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall e a.
(LuaError e, Walkable [Inline] a) =>
Filter -> a -> LuaE e a
walkInlinesStraight Filter
filter'
                   forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall e a.
(LuaError e, Walkable (SpliceList Block) a) =>
Filter -> a -> LuaE e a
walkBlockSplicing Filter
filter'
                   forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall e a.
(LuaError e, Walkable [Block] a) =>
Filter -> a -> LuaE e a
walkBlocksStraight Filter
filter'

-- | Applies a filter by processing the root node(s) first and descending
-- towards the leaves depth-first.
applyFilterTopdown :: LuaError e
                   => Filter
                   -> Topdown -> LuaE e Topdown
applyFilterTopdown :: forall e. LuaError e => Filter -> Topdown -> LuaE e Topdown
applyFilterTopdown Filter
filter' topdown :: Topdown
topdown@(Topdown TraversalControl
_ TraversalNode
node) =
  case TraversalNode
node of
    TBlock Block
x ->
      case Filter
filter' forall a. Data a => Filter -> a -> Maybe FilterFunction
`getFunctionFor` Block
x of
        Maybe FilterFunction
Nothing ->
          forall (f :: * -> *) a. Applicative f => a -> f a
pure Topdown
topdown
        Just FilterFunction
fn -> do
          ([Block]
blocks, TraversalControl
ctrl) <-
            forall e a.
LuaError e =>
FilterFunction
-> Pusher e a
-> Peeker e [a]
-> a
-> LuaE e ([a], TraversalControl)
applySplicingFunction FilterFunction
fn forall e. LuaError e => Pusher e Block
pushBlock forall e. LuaError e => Peeker e [Block]
peekBlocksFuzzy Block
x
          forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ TraversalControl -> TraversalNode -> Topdown
Topdown TraversalControl
ctrl forall a b. (a -> b) -> a -> b
$ [Block] -> TraversalNode
TBlocks [Block]
blocks

    TBlocks [Block]
xs ->
      case Name
"Blocks" Name -> Filter -> Maybe FilterFunction
`lookup` Filter
filter' of
        Maybe FilterFunction
Nothing ->
          forall (f :: * -> *) a. Applicative f => a -> f a
pure Topdown
topdown
        Just FilterFunction
fn -> do
          ([Block]
blocks, TraversalControl
ctrl) <-
            forall e a.
LuaError e =>
FilterFunction
-> Pusher e a -> Peeker e a -> a -> LuaE e (a, TraversalControl)
applyStraightFunction FilterFunction
fn forall e. LuaError e => Pusher e [Block]
pushBlocks forall e. LuaError e => Peeker e [Block]
peekBlocksFuzzy [Block]
xs
          forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ TraversalControl -> TraversalNode -> Topdown
Topdown TraversalControl
ctrl forall a b. (a -> b) -> a -> b
$ [Block] -> TraversalNode
TBlocks [Block]
blocks

    TInline Inline
x ->
      case Filter
filter' forall a. Data a => Filter -> a -> Maybe FilterFunction
`getFunctionFor` Inline
x of
        Maybe FilterFunction
Nothing ->
          forall (f :: * -> *) a. Applicative f => a -> f a
pure Topdown
topdown
        Just FilterFunction
fn -> do
          ([Inline]
inlines, TraversalControl
ctrl) <-
            forall e a.
LuaError e =>
FilterFunction
-> Pusher e a
-> Peeker e [a]
-> a
-> LuaE e ([a], TraversalControl)
applySplicingFunction FilterFunction
fn forall e. LuaError e => Pusher e Inline
pushInline forall e. LuaError e => Peeker e [Inline]
peekInlinesFuzzy Inline
x
          forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ TraversalControl -> TraversalNode -> Topdown
Topdown TraversalControl
ctrl forall a b. (a -> b) -> a -> b
$ [Inline] -> TraversalNode
TInlines [Inline]
inlines

    TInlines [Inline]
xs ->
      case Name
"Inlines" Name -> Filter -> Maybe FilterFunction
`lookup` Filter
filter' of
        Maybe FilterFunction
Nothing ->
          forall (f :: * -> *) a. Applicative f => a -> f a
pure Topdown
topdown
        Just FilterFunction
fn -> do
          ([Inline]
inlines, TraversalControl
ctrl) <-
            forall e a.
LuaError e =>
FilterFunction
-> Pusher e a -> Peeker e a -> a -> LuaE e (a, TraversalControl)
applyStraightFunction FilterFunction
fn forall e. LuaError e => Pusher e [Inline]
pushInlines forall e. LuaError e => Peeker e [Inline]
peekInlinesFuzzy [Inline]
xs
          forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ TraversalControl -> TraversalNode -> Topdown
Topdown TraversalControl
ctrl forall a b. (a -> b) -> a -> b
$ [Inline] -> TraversalNode
TInlines [Inline]
inlines