@@ -48,8 +48,11 @@ runLuaFilter :: (MonadIO m)
4848runLuaFilter filterPath args pd = liftIO $ do
4949 lua <- newstate
5050 Lua.openlibs lua
51+ -- create table in registry to store filter functions
52+ Lua.push lua ("PANDOC_FILTER_FUNCTIONS"::String)
5153 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"
5356 pushPandocModule lua
5457 Lua.setglobal lua "pandoc"
5558 status <- Lua.loadfile lua filterPath
@@ -171,12 +174,14 @@ runLuaFilterFunction lua lf inline = do
171174 Lua.pop lua 1
172175 return res
173176
174−-- FIXME: use registry
177+-- | Push the filter function to the top of the stack.
175178pushFilterFunction :: Lua.LuaState -> LuaFilterFunction a -> IO ()
176179pushFilterFunction lua lf = do
177− Lua.getglobal lua "PANDOC_FILTER_FUNCTIONS"
180+ -- The function is stored in a lua registry table, retrieve it from there.
181+ push lua ("PANDOC_FILTER_FUNCTIONS"::String)
182+ Lua.rawget lua Lua.registryindex
178183 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
180185
181186instance StackValue (LuaFilterFunction a) where
182187 valuetype _ = Lua.TFUNCTION
@@ -185,7 +190,8 @@ instance StackValue (LuaFilterFunction a) where
185190 isFn <- Lua.isfunction lua i
186191 when (not isFn) (error $ "Not a function at index " ++ (show i))
187192 Lua.pushvalue lua i
188− Lua.getglobal lua "PANDOC_FILTER_FUNCTIONS"
193+ push lua ("PANDOC_FILTER_FUNCTIONS"::String)
194+ Lua.rawget lua Lua.registryindex
189195 len <- Lua.objlen lua (-1)
190196 Lua.insert lua (-2)
191197 Lua.rawseti lua (-2) (len + 1)