@@ -48,8 +48,11 @@ runLuaFilter :: (MonadIO m)
48runLuaFilter filterPath args pd = liftIO $ do
48runLuaFilter filterPath args pd = liftIO $ do
49 lua <- newstate
49 lua <- newstate
50 Lua.openlibs lua
50 Lua.openlibs lua
51+ -- create table in registry to store filter functions
52+ Lua.push lua ("PANDOC_FILTER_FUNCTIONS"::String)
51 Lua.newtable lua
53 Lua.newtable lua
52− Lua.setglobal lua "PANDOC_FILTER_FUNCTIONS" -- hack, store functions here
54+ Lua.rawset lua Lua.registryindex
55+ -- store module in global "pandoc"
53 pushPandocModule lua
56 pushPandocModule lua
54 Lua.setglobal lua "pandoc"
57 Lua.setglobal lua "pandoc"
55 status <- Lua.loadfile lua filterPath
58 status <- Lua.loadfile lua filterPath
@@ -171,12 +174,14 @@ runLuaFilterFunction lua lf inline = do
171 Lua.pop lua 1
174 Lua.pop lua 1
172 return res
175 return res
174−-- FIXME: use registry
177+-- | Push the filter function to the top of the stack.
175pushFilterFunction :: Lua.LuaState -> LuaFilterFunction a -> IO ()
178pushFilterFunction :: Lua.LuaState -> LuaFilterFunction a -> IO ()
176pushFilterFunction lua lf = do
179pushFilterFunction lua lf = do
180+ -- The function is stored in a lua registry table, retrieve it from there.
177− Lua.getglobal lua "PANDOC_FILTER_FUNCTIONS"
181+ push lua ("PANDOC_FILTER_FUNCTIONS"::String)
182+ Lua.rawget lua Lua.registryindex
178 Lua.rawgeti lua (-1) (functionIndex lf)
183 Lua.rawgeti lua (-1) (functionIndex lf)
179− Lua.remove lua (-2) -- remove global from stack
184+ Lua.remove lua (-2) -- remove registry table from stack
181instance StackValue (LuaFilterFunction a) where
186instance StackValue (LuaFilterFunction a) where
182 valuetype _ = Lua.TFUNCTION
187 valuetype _ = Lua.TFUNCTION
@@ -185,7 +190,8 @@ instance StackValue (LuaFilterFunction a) where
185 isFn <- Lua.isfunction lua i
190 isFn <- Lua.isfunction lua i
186 when (not isFn) (error $ "Not a function at index " ++ (show i))
191 when (not isFn) (error $ "Not a function at index " ++ (show i))
187 Lua.pushvalue lua i
192 Lua.pushvalue lua i
188− Lua.getglobal lua "PANDOC_FILTER_FUNCTIONS"
193+ push lua ("PANDOC_FILTER_FUNCTIONS"::String)
194+ Lua.rawget lua Lua.registryindex
189 len <- Lua.objlen lua (-1)
195 len <- Lua.objlen lua (-1)
190 Lua.insert lua (-2)
196 Lua.insert lua (-2)
191 Lua.rawseti lua (-2) (len + 1)
197 Lua.rawseti lua (-2) (len + 1)