git.iko.soy/typeable/pandoc
on master help sign in
Lua: drop useless filter function type parameter

The return-type parameter for lua filter functions is removed. It only complicated the code without introducing any additional type safety.

commit57a0759def058df0322152823bca003664b961c5 authorAlbert Krewinkel <albert@zeitkraut.de> dateTue 18 Apr 2017, 19:05 (10 years ago) parent464db59
1 file changed+20−28
src/Text/Pandoc/Lua.hs+20−28
src/Text/Pandoc/Lua.hs+20−28
@@ -73,21 +73,18 @@ runAll [] = return
7373runAll (x:xs) = walkMWithLuaFilter x >=> runAll xs
7474
7575walkMWithLuaFilter :: LuaFilter -> Pandoc -> IO Pandoc
76−walkMWithLuaFilter (LuaFilter lua inlineFnMap blockFnMap docFnMap) =
77− walkM (execInlineLuaFilter lua inlineFnMap) >=>
78− walkM (execBlockLuaFilter lua blockFnMap) >=>
79− walkM (execDocLuaFilter lua docFnMap)
76+walkMWithLuaFilter (LuaFilter lua fnMap) =
77+ walkM (execInlineLuaFilter lua fnMap) >=>
78+ walkM (execBlockLuaFilter lua fnMap) >=>
79+ walkM (execDocLuaFilter lua fnMap)
8080
81−type InlineFunctionMap = Map String (LuaFilterFunction Inline)
82−type BlockFunctionMap = Map String (LuaFilterFunction Block)
83−type DocFunctionMap = Map String (LuaFilterFunction Pandoc)
84−data LuaFilter =
85− LuaFilter LuaState InlineFunctionMap BlockFunctionMap DocFunctionMap
81+type FunctionMap = Map String LuaFilterFunction
82+data LuaFilter = LuaFilter LuaState FunctionMap
8683
87−newtype LuaFilterFunction a = LuaFilterFunction { functionIndex :: Int }
84+newtype LuaFilterFunction = LuaFilterFunction { functionIndex :: Int }
8885
8986execDocLuaFilter :: LuaState
90− -> Map String (LuaFilterFunction Pandoc)
87+ -> FunctionMap
9188 -> Pandoc -> IO Pandoc
9289execDocLuaFilter lua fnMap x = do
9390 let docFnName = "Doc"
@@ -96,7 +93,7 @@ execDocLuaFilter lua fnMap x = do
9693 Just fn -> runLuaFilterFunction lua fn x
9794
9895execBlockLuaFilter :: LuaState
99− -> Map String (LuaFilterFunction Block)
96+ -> FunctionMap
10097 -> Block -> IO Block
10198execBlockLuaFilter lua fnMap x = do
10299 let tryFilter :: String -> IO Block
@@ -121,7 +118,7 @@ execBlockLuaFilter lua fnMap x = do
121118 Table _ _ _ _ _ -> tryFilter "Table"
122119
123120execInlineLuaFilter :: LuaState
124− -> Map String (LuaFilterFunction Inline)
121+ -> FunctionMap
125122 -> Inline -> IO Inline
126123execInlineLuaFilter lua fnMap x = do
127124 let tryFilter :: String -> IO Inline
@@ -161,19 +158,14 @@ execInlineLuaFilter lua fnMap x = do
161158instance StackValue LuaFilter where
162159 valuetype _ = Lua.TTABLE
163160 push = undefined
164− peek lua i = do
165− -- TODO: find a more efficient way of doing this in a typesafe manner.
166− inlineFnMap <- Lua.peek lua i
167− blockFnMap <- Lua.peek lua i
168− docFnMap <- Lua.peek lua i
169− return $ LuaFilter lua <$> inlineFnMap <*> blockFnMap <*> docFnMap
161+ peek lua idx = fmap (LuaFilter lua) <$> Lua.peek lua idx
170162
171163-- | Helper class for pushing a single value to the stack via a lua function.
172164-- See @pushViaCall@.
173−class PushViaFilterFunction a b where
174− pushViaFilterFunction' :: LuaState -> LuaFilterFunction a -> IO () -> Int -> b
165+class PushViaFilterFunction a where
166+ pushViaFilterFunction' :: LuaState -> LuaFilterFunction -> IO () -> Int -> a
175167
176−instance (StackValue a) => PushViaFilterFunction a (IO a) where
168+instance StackValue a => PushViaFilterFunction (IO a) where
177169 pushViaFilterFunction' lua lf pushArgs num = do
178170 pushFilterFunction lua lf
179171 pushArgs
@@ -184,20 +176,20 @@ instance (StackValue a) => PushViaFilterFunction a (IO a) where
184176 ++ "value from lua stack."
185177 Just res -> res <$ Lua.pop lua 1
186178
187−instance (PushViaFilterFunction a c, StackValue b) =>
188− PushViaFilterFunction a (b -> c) where
179+instance (StackValue a, PushViaFilterFunction b) =>
180+ PushViaFilterFunction (a -> b) where
189181 pushViaFilterFunction' lua lf pushArgs num x =
190182 pushViaFilterFunction' lua lf (pushArgs *> push lua x) (num + 1)
191183
192184-- | Push an value to the stack via a lua filter function. The function is
193185-- called with all arguments that are passed to this function and is expected to
194186-- return a single value.
195−runLuaFilterFunction :: (StackValue a, PushViaFilterFunction a b)
196− => LuaState -> LuaFilterFunction a -> b
187+runLuaFilterFunction :: PushViaFilterFunction a
188+ => LuaState -> LuaFilterFunction -> a
197189runLuaFilterFunction lua lf = pushViaFilterFunction' lua lf (return ()) 0
198190
199191-- | Push the filter function to the top of the stack.
200−pushFilterFunction :: Lua.LuaState -> LuaFilterFunction a -> IO ()
192+pushFilterFunction :: Lua.LuaState -> LuaFilterFunction -> IO ()
201193pushFilterFunction lua lf = do
202194 -- The function is stored in a lua registry table, retrieve it from there.
203195 push lua ("PANDOC_FILTER_FUNCTIONS"::String)
@@ -205,7 +197,7 @@ pushFilterFunction lua lf = do
205197 Lua.rawgeti lua (-1) (functionIndex lf)
206198 Lua.remove lua (-2) -- remove registry table from stack
207199
208−instance StackValue (LuaFilterFunction a) where
200+instance StackValue LuaFilterFunction where
209201 valuetype _ = Lua.TFUNCTION
210202 push lua v = pushFilterFunction lua v
211203 peek lua i = do