git.iko.soy/typeable/pandoc
on master help sign in
Text.Pandoc.Lua: refactored to remove duplicated code.
commit5c80aca0e20492eaa31b9280fb5524d76f5e8098 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 29 Jun 2017, 14:31 (9 years ago) parent6ad7481
1 file changed+25−34
src/Text/Pandoc/Lua.hs+25−34
src/Text/Pandoc/Lua.hs+25−34
@@ -104,55 +104,46 @@ newtype LuaFilterFunction = LuaFilterFunction { functionIndex :: Int }
104104execDocLuaFilter :: LuaState
105105 -> FunctionMap
106106 -> Pandoc -> IO Pandoc
107−execDocLuaFilter lua fnMap x = do
108− let docFnName = "Doc"
109− case Map.lookup docFnName fnMap of
110− Nothing -> return x
111− Just fn -> runFilterFunction lua fn x
107+execDocLuaFilter lua fnMap = tryFilter lua fnMap "Doc"
112108
113109execMetaLuaFilter :: LuaState
114110 -> FunctionMap
115111 -> Pandoc -> IO Pandoc
116−execMetaLuaFilter lua fnMap pd@(Pandoc meta blks) = do
117− let metaFnName = "Meta"
118− case Map.lookup metaFnName fnMap of
119− Nothing -> return pd
120− Just fn -> do
121− meta' <- runFilterFunction lua fn meta
122− return $ Pandoc meta' blks
112+execMetaLuaFilter lua fnMap (Pandoc meta blks) = do
113+ meta' <- tryFilter lua fnMap "Meta" meta
114+ return $ Pandoc meta' blks
123115
124116execBlockLuaFilter :: LuaState
125117 -> FunctionMap
126118 -> Block -> IO Block
127119execBlockLuaFilter lua fnMap x = do
128− let tryFilter :: String -> IO Block
129− tryFilter filterFnName =
130− case Map.lookup filterFnName fnMap of
131− Nothing -> return x
132− Just fn -> runFilterFunction lua fn x
133− tryFilter (show (toConstr x))
120+ tryFilter lua fnMap (show (toConstr x)) x
121+
122+tryFilter :: StackValue a => LuaState -> FunctionMap -> String -> a -> IO a
123+tryFilter lua fnMap filterFnName x =
124+ case Map.lookup filterFnName fnMap of
125+ Nothing -> return x
126+ Just fn -> runFilterFunction lua fn x
127+
128+tryFilterAlternatives :: StackValue a
129+ => LuaState -> FunctionMap -> [String] -> a -> IO a
130+tryFilterAlternatives _ _ [] x = return x
131+tryFilterAlternatives lua fnMap (fnName : alternatives) x =
132+ case Map.lookup fnName fnMap of
133+ Nothing -> tryFilterAlternatives lua fnMap alternatives x
134+ Just fn -> runFilterFunction lua fn x
134135
135136execInlineLuaFilter :: LuaState
136137 -> FunctionMap
137138 -> Inline -> IO Inline
138139execInlineLuaFilter lua fnMap x = do
139− let tryFilter :: String -> IO Inline
140− tryFilter filterFnName =
141− case Map.lookup filterFnName fnMap of
142− Nothing -> return x
143− Just fn -> runFilterFunction lua fn x
144− let tryFilterAlternatives :: [String] -> IO Inline
145− tryFilterAlternatives [] = return x
146− tryFilterAlternatives (fnName : alternatives) =
147− case Map.lookup fnName fnMap of
148− Nothing -> tryFilterAlternatives alternatives
149− Just fn -> runFilterFunction lua fn x
140+ let tryAlt = tryFilterAlternatives lua fnMap
150141 case x of
151− Math DisplayMath _ -> tryFilterAlternatives ["DisplayMath", "Math"]
152− Math InlineMath _ -> tryFilterAlternatives ["InlineMath", "Math"]
153− Quoted DoubleQuote _ -> tryFilterAlternatives ["DoubleQuoted", "Quoted"]
154− Quoted SingleQuote _ -> tryFilterAlternatives ["SingleQuoted", "Quoted"]
155− _ -> tryFilter (show (toConstr x))
142+ Math DisplayMath _ -> tryAlt ["DisplayMath", "Math"] x
143+ Math InlineMath _ -> tryAlt ["InlineMath", "Math"] x
144+ Quoted DoubleQuote _ -> tryAlt ["DoubleQuoted", "Quoted"] x
145+ Quoted SingleQuote _ -> tryAlt ["SingleQuoted", "Quoted"] x
146+ _ -> tryFilter lua fnMap (show (toConstr x)) x
156147
157148instance StackValue LuaFilter where
158149 valuetype _ = Lua.TTABLE