git.iko.soy/typeable/pandoc
on master help sign in
Text.Pandoc.Lua: support Inline and Block catch-alls

Try function `Inline`/`Block` if no other filter function of the respective type matches an element. Closes: #3859

commit41baaff32737e57dd9ec0a1153416ca24a12dca1 authorAlbert Krewinkel <albert@zeitkraut.de> dateTue 22 Aug 2017, 23:12 (9 years ago) parent56fb854
4 files changed+27−7
data/pandoc.lua+2−1 src/Text/Pandoc/Lua.hs+8−6 test/Tests/Lua.hs+6 test/lua/block-count.lua+11
data/pandoc.lua+2−1
@@ -808,7 +808,8 @@ function M.global_filter()
808808 function is_filter_function(k)
809809 return M.Inline.constructor[k] or
810810 M.Block.constructor[k] or
811− k == "Meta" or k == "Doc" or k == "Pandoc"
811+ k == "Meta" or k == "Doc" or k == "Pandoc" or
812+ k == "Block" or k == "Inline"
812813 end
813814 for k, v in pairs(_G) do
814815 if is_filter_function(k) then
src/Text/Pandoc/Lua.hs+8−6
@@ -35,7 +35,7 @@ module Text.Pandoc.Lua (LuaException (..), pushPandocModule, runLuaFilter) where
3535import Control.Monad (mplus, unless, when, (>=>))
3636import Control.Monad.Trans (MonadIO (..))
3737import Data.Data (DataType, Data, toConstr, showConstr, dataTypeOf,
38− dataTypeConstrs)
38+ dataTypeConstrs, dataTypeName)
3939import Data.Foldable (foldrM)
4040import Data.Map (Map)
4141import Data.Maybe (isJust)
@@ -108,10 +108,10 @@ constructorsFor :: DataType -> [String]
108108constructorsFor x = map show (dataTypeConstrs x)
109109
110110inlineFilterNames :: [String]
111−inlineFilterNames = constructorsFor (dataTypeOf (Str []))
111+inlineFilterNames = "Inline" : constructorsFor (dataTypeOf (Str []))
112112
113113blockFilterNames :: [String]
114−blockFilterNames = constructorsFor (dataTypeOf (Para []))
114+blockFilterNames = "Block" : constructorsFor (dataTypeOf (Para []))
115115
116116metaFilterName :: String
117117metaFilterName = "Meta"
@@ -126,10 +126,12 @@ newtype LuaFilterFunction = LuaFilterFunction { functionIndex :: Int }
126126-- | Try running a filter for the given element
127127tryFilter :: (Data a, FromLuaStack a, ToLuaStack a) => FunctionMap -> a -> Lua a
128128tryFilter fnMap x =
129− let filterFnName = showConstr (toConstr x) in
130− case Map.lookup filterFnName fnMap of
131− Nothing -> return x
129+ let filterFnName = showConstr (toConstr x)
130+ catchAllName = dataTypeName (dataTypeOf x)
131+ in
132+ case Map.lookup filterFnName fnMap `mplus` Map.lookup catchAllName fnMap of
132133 Just fn -> runFilterFunction fn x
134+ Nothing -> return x
133135
134136instance FromLuaStack LuaFilter where
135137 peek idx =
test/Tests/Lua.hs+6
@@ -70,6 +70,12 @@ tests = map (localOption (QuickCheckTests 20))
7070 "metatable-catch-all.lua"
7171 (doc . para $ "four words, three spaces")
7272 (doc . para $ str "7")
73+
74+ , testCase "Count blocks via Block-specific catch-all" $
75+ assertFilterConversion "filtering with Block catch-all failed"
76+ "block-count.lua"
77+ (doc $ para "one" <> para "two")
78+ (doc $ para "2")
7379 ]
7480
7581assertFilterConversion :: String -> FilePath -> Pandoc -> Pandoc -> Assertion
test/lua/block-count.luanew file+11
@@ -0,0 +1,11 @@
1+local num_blocks = 0
2+
3+function Block(el)
4+ num_blocks = num_blocks + 1
5+end
6+
7+function Pandoc(blocks, meta)
8+ return pandoc.Pandoc {
9+ pandoc.Para{pandoc.Str(num_blocks)}
10+ }
11+end