{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
module Text.Pandoc.Lua.Marshaling.CommonState () where
import Foreign.Lua (Lua, Peekable, Pushable)
import Foreign.Lua.Types.Peekable (reportValueOnFailure)
import Foreign.Lua.Userdata (ensureUserdataMetatable, pushAnyWithMetatable,
toAnyWithName)
import Text.Pandoc.Class (CommonState (..))
import Text.Pandoc.Logging (LogMessage, showLogMessage)
import Text.Pandoc.Lua.Marshaling.AnyValue (AnyValue (..))
import qualified Data.Map as Map
import qualified Data.Text as Text
import qualified Foreign.Lua as Lua
import qualified Text.Pandoc.Lua.Util as LuaUtil
commonStateTypeName :: String
commonStateTypeName :: FilePath
commonStateTypeName = FilePath
"Pandoc CommonState"
instance Peekable CommonState where
peek :: StackIndex -> Lua CommonState
peek StackIndex
idx = FilePath
-> (StackIndex -> Lua (Maybe CommonState))
-> StackIndex
-> Lua CommonState
forall a.
FilePath -> (StackIndex -> Lua (Maybe a)) -> StackIndex -> Lua a
reportValueOnFailure FilePath
commonStateTypeName
(StackIndex -> FilePath -> Lua (Maybe CommonState)
forall a. StackIndex -> FilePath -> Lua (Maybe a)
`toAnyWithName` FilePath
commonStateTypeName) StackIndex
idx
instance Pushable CommonState where
push :: CommonState -> Lua ()
push CommonState
st = Lua () -> CommonState -> Lua ()
forall a. Lua () -> a -> Lua ()
pushAnyWithMetatable Lua ()
pushCommonStateMetatable CommonState
st
where
pushCommonStateMetatable :: Lua ()
pushCommonStateMetatable = FilePath -> Lua () -> Lua ()
ensureUserdataMetatable FilePath
commonStateTypeName (Lua () -> Lua ()) -> Lua () -> Lua ()
forall a b. (a -> b) -> a -> b
$ do
FilePath -> (CommonState -> AnyValue -> Lua NumResults) -> Lua ()
forall a. ToHaskellFunction a => FilePath -> a -> Lua ()
LuaUtil.addFunction FilePath
"__index" CommonState -> AnyValue -> Lua NumResults
indexCommonState
FilePath -> (CommonState -> Lua NumResults) -> Lua ()
forall a. ToHaskellFunction a => FilePath -> a -> Lua ()
LuaUtil.addFunction FilePath
"__pairs" CommonState -> Lua NumResults
pairsCommonState
indexCommonState :: CommonState -> AnyValue -> Lua Lua.NumResults
indexCommonState :: CommonState -> AnyValue -> Lua NumResults
indexCommonState CommonState
st (AnyValue StackIndex
idx) = StackIndex -> Lua Type
Lua.ltype StackIndex
idx Lua Type -> (Type -> Lua NumResults) -> Lua NumResults
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Type
Lua.TypeString -> NumResults
1 NumResults -> Lua () -> Lua NumResults
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (StackIndex -> Lua Text
forall a. Peekable a => StackIndex -> Lua a
Lua.peek StackIndex
idx Lua Text -> (Text -> Lua ()) -> Lua ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Lua ()
pushField)
Type
_ -> NumResults
1 NumResults -> Lua () -> Lua NumResults
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Lua ()
Lua.pushnil
where
pushField :: Text.Text -> Lua ()
pushField :: Text -> Lua ()
pushField Text
name = case Text
-> [(Text, CommonState -> Lua ())] -> Maybe (CommonState -> Lua ())
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
name [(Text, CommonState -> Lua ())]
commonStateFields of
Just CommonState -> Lua ()
pushValue -> CommonState -> Lua ()
pushValue CommonState
st
Maybe (CommonState -> Lua ())
Nothing -> Lua ()
Lua.pushnil
pairsCommonState :: CommonState -> Lua Lua.NumResults
pairsCommonState :: CommonState -> Lua NumResults
pairsCommonState CommonState
st = do
(AnyValue -> AnyValue -> Lua NumResults) -> Lua ()
forall a. ToHaskellFunction a => a -> Lua ()
Lua.pushHaskellFunction AnyValue -> AnyValue -> Lua NumResults
nextFn
Lua ()
Lua.pushnil
Lua ()
Lua.pushnil
NumResults -> Lua NumResults
forall (m :: * -> *) a. Monad m => a -> m a
return NumResults
3
where
nextFn :: AnyValue -> AnyValue -> Lua Lua.NumResults
nextFn :: AnyValue -> AnyValue -> Lua NumResults
nextFn AnyValue
_ (AnyValue StackIndex
idx) =
StackIndex -> Lua Type
Lua.ltype StackIndex
idx Lua Type -> (Type -> Lua NumResults) -> Lua NumResults
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Type
Lua.TypeNil -> case [(Text, CommonState -> Lua ())]
commonStateFields of
[] -> NumResults
2 NumResults -> Lua () -> Lua NumResults
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (Lua ()
Lua.pushnil Lua () -> Lua () -> Lua ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Lua ()
Lua.pushnil)
(Text
key, CommonState -> Lua ()
pushValue):[(Text, CommonState -> Lua ())]
_ -> NumResults
2 NumResults -> Lua () -> Lua NumResults
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (Text -> Lua ()
forall a. Pushable a => a -> Lua ()
Lua.push Text
key Lua () -> Lua () -> Lua ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> CommonState -> Lua ()
pushValue CommonState
st)
Type
Lua.TypeString -> do
Text
key <- StackIndex -> Lua Text
forall a. Peekable a => StackIndex -> Lua a
Lua.peek StackIndex
idx
case [(Text, CommonState -> Lua ())] -> [(Text, CommonState -> Lua ())]
forall a. [a] -> [a]
tail ([(Text, CommonState -> Lua ())]
-> [(Text, CommonState -> Lua ())])
-> [(Text, CommonState -> Lua ())]
-> [(Text, CommonState -> Lua ())]
forall a b. (a -> b) -> a -> b
$ ((Text, CommonState -> Lua ()) -> Bool)
-> [(Text, CommonState -> Lua ())]
-> [(Text, CommonState -> Lua ())]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile ((Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
key) (Text -> Bool)
-> ((Text, CommonState -> Lua ()) -> Text)
-> (Text, CommonState -> Lua ())
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, CommonState -> Lua ()) -> Text
forall a b. (a, b) -> a
fst) [(Text, CommonState -> Lua ())]
commonStateFields of
[] -> NumResults
2 NumResults -> Lua () -> Lua NumResults
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (Lua ()
Lua.pushnil Lua () -> Lua () -> Lua ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Lua ()
Lua.pushnil)
(Text
nextKey, CommonState -> Lua ()
pushValue):[(Text, CommonState -> Lua ())]
_ -> NumResults
2 NumResults -> Lua () -> Lua NumResults
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (Text -> Lua ()
forall a. Pushable a => a -> Lua ()
Lua.push Text
nextKey Lua () -> Lua () -> Lua ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> CommonState -> Lua ()
pushValue CommonState
st)
Type
_ -> NumResults
2 NumResults -> Lua () -> Lua NumResults
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (Lua ()
Lua.pushnil Lua () -> Lua () -> Lua ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Lua ()
Lua.pushnil)
commonStateFields :: [(Text.Text, CommonState -> Lua ())]
commonStateFields :: [(Text, CommonState -> Lua ())]
commonStateFields =
[ (Text
"input_files", [FilePath] -> Lua ()
forall a. Pushable a => a -> Lua ()
Lua.push ([FilePath] -> Lua ())
-> (CommonState -> [FilePath]) -> CommonState -> Lua ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CommonState -> [FilePath]
stInputFiles)
, (Text
"output_file", Optional FilePath -> Lua ()
forall a. Pushable a => a -> Lua ()
Lua.push (Optional FilePath -> Lua ())
-> (CommonState -> Optional FilePath) -> CommonState -> Lua ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe FilePath -> Optional FilePath
forall a. Maybe a -> Optional a
Lua.Optional (Maybe FilePath -> Optional FilePath)
-> (CommonState -> Maybe FilePath)
-> CommonState
-> Optional FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CommonState -> Maybe FilePath
stOutputFile)
, (Text
"log", [LogMessage] -> Lua ()
forall a. Pushable a => a -> Lua ()
Lua.push ([LogMessage] -> Lua ())
-> (CommonState -> [LogMessage]) -> CommonState -> Lua ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CommonState -> [LogMessage]
stLog)
, (Text
"request_headers", Map Text Text -> Lua ()
forall a. Pushable a => a -> Lua ()
Lua.push (Map Text Text -> Lua ())
-> (CommonState -> Map Text Text) -> CommonState -> Lua ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Text, Text)] -> Map Text Text
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(Text, Text)] -> Map Text Text)
-> (CommonState -> [(Text, Text)]) -> CommonState -> Map Text Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CommonState -> [(Text, Text)]
stRequestHeaders)
, (Text
"resource_path", [FilePath] -> Lua ()
forall a. Pushable a => a -> Lua ()
Lua.push ([FilePath] -> Lua ())
-> (CommonState -> [FilePath]) -> CommonState -> Lua ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CommonState -> [FilePath]
stResourcePath)
, (Text
"source_url", Optional Text -> Lua ()
forall a. Pushable a => a -> Lua ()
Lua.push (Optional Text -> Lua ())
-> (CommonState -> Optional Text) -> CommonState -> Lua ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe Text -> Optional Text
forall a. Maybe a -> Optional a
Lua.Optional (Maybe Text -> Optional Text)
-> (CommonState -> Maybe Text) -> CommonState -> Optional Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CommonState -> Maybe Text
stSourceURL)
, (Text
"user_data_dir", Optional FilePath -> Lua ()
forall a. Pushable a => a -> Lua ()
Lua.push (Optional FilePath -> Lua ())
-> (CommonState -> Optional FilePath) -> CommonState -> Lua ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe FilePath -> Optional FilePath
forall a. Maybe a -> Optional a
Lua.Optional (Maybe FilePath -> Optional FilePath)
-> (CommonState -> Maybe FilePath)
-> CommonState
-> Optional FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CommonState -> Maybe FilePath
stUserDataDir)
, (Text
"trace", Bool -> Lua ()
forall a. Pushable a => a -> Lua ()
Lua.push (Bool -> Lua ()) -> (CommonState -> Bool) -> CommonState -> Lua ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CommonState -> Bool
stTrace)
, (Text
"verbosity", FilePath -> Lua ()
forall a. Pushable a => a -> Lua ()
Lua.push (FilePath -> Lua ())
-> (CommonState -> FilePath) -> CommonState -> Lua ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Verbosity -> FilePath
forall a. Show a => a -> FilePath
show (Verbosity -> FilePath)
-> (CommonState -> Verbosity) -> CommonState -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CommonState -> Verbosity
stVerbosity)
]
logMessageTypeName :: String
logMessageTypeName :: FilePath
logMessageTypeName = FilePath
"Pandoc LogMessage"
instance Peekable LogMessage where
peek :: StackIndex -> Lua LogMessage
peek StackIndex
idx = FilePath
-> (StackIndex -> Lua (Maybe LogMessage))
-> StackIndex
-> Lua LogMessage
forall a.
FilePath -> (StackIndex -> Lua (Maybe a)) -> StackIndex -> Lua a
reportValueOnFailure FilePath
logMessageTypeName
(StackIndex -> FilePath -> Lua (Maybe LogMessage)
forall a. StackIndex -> FilePath -> Lua (Maybe a)
`toAnyWithName` FilePath
logMessageTypeName) StackIndex
idx
instance Pushable LogMessage where
push :: LogMessage -> Lua ()
push LogMessage
msg = Lua () -> LogMessage -> Lua ()
forall a. Lua () -> a -> Lua ()
pushAnyWithMetatable Lua ()
pushLogMessageMetatable LogMessage
msg
where
pushLogMessageMetatable :: Lua ()
pushLogMessageMetatable = FilePath -> Lua () -> Lua ()
ensureUserdataMetatable FilePath
logMessageTypeName (Lua () -> Lua ()) -> Lua () -> Lua ()
forall a b. (a -> b) -> a -> b
$
FilePath -> (LogMessage -> Lua Text) -> Lua ()
forall a. ToHaskellFunction a => FilePath -> a -> Lua ()
LuaUtil.addFunction FilePath
"__tostring" LogMessage -> Lua Text
tostringLogMessage
tostringLogMessage :: LogMessage -> Lua Text.Text
tostringLogMessage :: LogMessage -> Lua Text
tostringLogMessage = Text -> Lua Text
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> Lua Text)
-> (LogMessage -> Text) -> LogMessage -> Lua Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LogMessage -> Text
showLogMessage