From 41baaff32737e57dd9ec0a1153416ca24a12dca1 Mon Sep 17 00:00:00 2001 From: Albert Krewinkel Date: Tue, 22 Aug 2017 23:12:39 +0200 Subject: 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 --- src/Text/Pandoc/Lua.hs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'src/Text/Pandoc/Lua.hs') diff --git a/src/Text/Pandoc/Lua.hs b/src/Text/Pandoc/Lua.hs index db028d325..6c6676e4f 100644 --- a/src/Text/Pandoc/Lua.hs +++ b/src/Text/Pandoc/Lua.hs @@ -35,7 +35,7 @@ module Text.Pandoc.Lua (LuaException (..), pushPandocModule, runLuaFilter) where import Control.Monad (mplus, unless, when, (>=>)) import Control.Monad.Trans (MonadIO (..)) import Data.Data (DataType, Data, toConstr, showConstr, dataTypeOf, - dataTypeConstrs) + dataTypeConstrs, dataTypeName) import Data.Foldable (foldrM) import Data.Map (Map) import Data.Maybe (isJust) @@ -108,10 +108,10 @@ constructorsFor :: DataType -> [String] constructorsFor x = map show (dataTypeConstrs x) inlineFilterNames :: [String] -inlineFilterNames = constructorsFor (dataTypeOf (Str [])) +inlineFilterNames = "Inline" : constructorsFor (dataTypeOf (Str [])) blockFilterNames :: [String] -blockFilterNames = constructorsFor (dataTypeOf (Para [])) +blockFilterNames = "Block" : constructorsFor (dataTypeOf (Para [])) metaFilterName :: String metaFilterName = "Meta" @@ -126,10 +126,12 @@ newtype LuaFilterFunction = LuaFilterFunction { functionIndex :: Int } -- | Try running a filter for the given element tryFilter :: (Data a, FromLuaStack a, ToLuaStack a) => FunctionMap -> a -> Lua a tryFilter fnMap x = - let filterFnName = showConstr (toConstr x) in - case Map.lookup filterFnName fnMap of - Nothing -> return x + let filterFnName = showConstr (toConstr x) + catchAllName = dataTypeName (dataTypeOf x) + in + case Map.lookup filterFnName fnMap `mplus` Map.lookup catchAllName fnMap of Just fn -> runFilterFunction fn x + Nothing -> return x instance FromLuaStack LuaFilter where peek idx = -- cgit v1.2.3