git.iko.soy/typeable/pandoc
on master help sign in
data/pandoc.lua: drop function pandoc.global_filter

The function `global_filter` was used internally to get the implicitly defined global filter. It was of little value to end-users, but caused unnecessary code duplication in pandoc. The function has hence been dropped. Internally, the global filter is now received by interpreting the global table as lua filter. This is a Lua API change.

commit9be2c7624cb0cf3ef63516e5df959672958058bc authorAlbert Krewinkel <albert@zeitkraut.de> dateFri 29 Dec 2017, 09:40 (9 years ago) parent820ee07
5 files changed+9−68
data/pandoc.lua+1−36 doc/lua-filters.md−19 src/Text/Pandoc/Lua.hs+7−11 src/Text/Pandoc/Lua/Filter.hs−2 src/Text/Pandoc/Lua/Util.hs+1
data/pandoc.lua+1−36
@@ -23,7 +23,7 @@ THIS SOFTWARE.
2323-- @copyright © 2017 Albert Krewinkel
2424-- @license MIT
2525local M = {
26− _VERSION = "0.3.0"
26+ _VERSION = "0.4.0"
2727}
2828
2929local List = require 'pandoc.List'
@@ -868,41 +868,6 @@ M.LowerAlpha = "LowerAlpha"
868868-- @see OrderedList
869869M.UpperAlpha = "UpperAlpha"
870870
871−
872−------------------------------------------------------------------------
873−-- Helper Functions
874−-- @section helpers
875−
876−--- Use functions defined in the global namespace to create a pandoc filter.
877−-- All globally defined functions which have names of pandoc elements are
878−-- collected into a new table.
879−-- @return A list of filter functions
880−-- @usage
881−-- -- within a file defining a pandoc filter:
882−-- text = require 'text'
883−-- function Str(elem)
884−-- return pandoc.Str(text.upper(elem.text))
885−-- end
886−--
887−-- return {pandoc.global_filter()}
888−-- -- the above is equivalent to
889−-- -- return {{Str = Str}}
890−function M.global_filter()
891− local res = {}
892− function is_filter_function(k)
893− return M.Inline.constructor[k] or
894− M.Block.constructor[k] or
895− k == "Meta" or k == "Doc" or k == "Pandoc" or
896− k == "Block" or k == "Inline"
897− end
898− for k, v in pairs(_G) do
899− if is_filter_function(k) then
900− res[k] = v
901− end
902− end
903− return res
904−end
905−
906871------------------------------------------------------------------------
907872-- Functions which have moved to different modules
908873local utils = require 'pandoc.utils'
doc/lua-filters.md−19
@@ -1325,25 +1325,6 @@ Lua functions for pandoc scripts.
13251325
13261326## Helper Functions
13271327
1328−[`global_filter ()`]{#global_filter}
1329−
1330−: Use functions defined in the global namespace to create a
1331− pandoc filter.
1332−
1333− Returns: A list of filter functions
1334−
1335− Usage:
1336−
1337− -- within a file defining a pandoc filter:
1338− text = require 'text'
1339− function Str(elem)
1340− return pandoc.Str(text.upper(elem.text))
1341− end
1342−
1343− return {pandoc.global_filter()}
1344− -- the above is equivalent to
1345− -- return {{Str = Str}}
1346−
13471328[`pipe (command, args, input)`]{#pipe}
13481329
13491330: Runs command with arguments, passing it some input, and
src/Text/Pandoc/Lua.hs+7−11
@@ -32,7 +32,7 @@ module Text.Pandoc.Lua
3232 , pushPandocModule
3333 ) where
3434
35−import Control.Monad (when, (>=>))
35+import Control.Monad ((>=>))
3636import Foreign.Lua (FromLuaStack (peek), Lua, LuaException (..),
3737 Status (OK), ToLuaStack (push))
3838import Text.Pandoc.Class (PandocIO)
@@ -40,6 +40,7 @@ import Text.Pandoc.Definition (Pandoc)
4040import Text.Pandoc.Lua.Filter (LuaFilter, walkMWithLuaFilter)
4141import Text.Pandoc.Lua.Init (runPandocLua)
4242import Text.Pandoc.Lua.Module.Pandoc (pushModule) -- TODO: remove
43+import Text.Pandoc.Lua.Util (popValue)
4344import qualified Foreign.Lua as Lua
4445
4546-- | Run the Lua filter in @filterPath@ for a transformation to target
@@ -63,22 +64,17 @@ runLuaFilter' filterPath format pd = do
6364 Lua.throwLuaError luaErrMsg
6465 else do
6566 newtop <- Lua.gettop
66− -- Use the implicitly defined global filter if nothing was returned
67− when (newtop - top < 1) pushGlobalFilter
68− luaFilters <- peek (-1)
67+ -- Use the returned filters, or the implicitly defined global filter if
68+ -- nothing was returned.
69+ luaFilters <- if (newtop - top >= 1)
70+ then peek (-1)
71+ else Lua.getglobal "_G" *> fmap (:[]) popValue
6972 runAll luaFilters pd
7073 where
7174 registerFormat = do
7275 push format
7376 Lua.setglobal "FORMAT"
7477
75−pushGlobalFilter :: Lua ()
76−pushGlobalFilter = do
77− Lua.newtable
78− Lua.getglobal' "pandoc.global_filter"
79− Lua.call 0 1
80− Lua.rawseti (-2) 1
81−
8278runAll :: [LuaFilter] -> Pandoc -> Lua Pandoc
8379runAll = foldr ((>=>) . walkMWithLuaFilter) return
8480
src/Text/Pandoc/Lua/Filter.hs−2
@@ -164,5 +164,3 @@ singleElement x = do
164164 Lua.throwLuaError $
165165 "Error while trying to get a filter's return " ++
166166 "value from lua stack.\n" ++ err
167−
168−
src/Text/Pandoc/Lua/Util.hs+1
@@ -37,6 +37,7 @@ module Text.Pandoc.Lua.Util
3737 , setRawInt
3838 , addRawInt
3939 , raiseError
40+ , popValue
4041 , OrNil (..)
4142 , PushViaCall
4243 , pushViaCall