git.iko.soy/typeable/pandoc
on master help sign in
Text.Pandoc.Lua: use generics to reduce boilerplate.

I tested this with the str.lua filter on MANUAL.txt, and I could see no significant performance degradation. Doing things this way will ease maintenance, as we won't have to manually modify this module when types change. @tarleb, do we really need special cases for things like DoubleQuoted and InlineMath?

commit6ad74815f66cb36ec4039c597b38473db853eb6c authorJohn MacFarlane <jgm@berkeley.edu> dateThu 29 Jun 2017, 14:00 (9 years ago) parentc505de5
1 file changed+3−32
src/Text/Pandoc/Lua.hs+3−32
src/Text/Pandoc/Lua.hs+3−32
@@ -36,6 +36,7 @@ module Text.Pandoc.Lua ( LuaException(..),
3636import Control.Exception
3737import Control.Monad (unless, when, (>=>))
3838import Control.Monad.Trans (MonadIO (..))
39+import Data.Data (toConstr)
3940import Data.Map (Map)
4041import Data.Typeable (Typeable)
4142import Scripting.Lua (LuaState, StackValue (..))
@@ -129,21 +130,7 @@ execBlockLuaFilter lua fnMap x = do
129130 case Map.lookup filterFnName fnMap of
130131 Nothing -> return x
131132 Just fn -> runFilterFunction lua fn x
132− case x of
133− BlockQuote{} -> tryFilter "BlockQuote"
134− BulletList{} -> tryFilter "BulletList"
135− CodeBlock{} -> tryFilter "CodeBlock"
136− DefinitionList{} -> tryFilter "DefinitionList"
137− Div{} -> tryFilter "Div"
138− Header{} -> tryFilter "Header"
139− HorizontalRule -> tryFilter "HorizontalRule"
140− LineBlock{} -> tryFilter "LineBlock"
141− Null -> tryFilter "Null"
142− Para{} -> tryFilter "Para"
143− Plain{} -> tryFilter "Plain"
144− RawBlock{} -> tryFilter "RawBlock"
145− OrderedList{} -> tryFilter "OrderedList"
146− Table{} -> tryFilter "Table"
133+ tryFilter (show (toConstr x))
147134
148135execInlineLuaFilter :: LuaState
149136 -> FunctionMap
@@ -161,27 +148,11 @@ execInlineLuaFilter lua fnMap x = do
161148 Nothing -> tryFilterAlternatives alternatives
162149 Just fn -> runFilterFunction lua fn x
163150 case x of
164− Cite{} -> tryFilter "Cite"
165− Code{} -> tryFilter "Code"
166− Emph{} -> tryFilter "Emph"
167− Image{} -> tryFilter "Image"
168− LineBreak -> tryFilter "LineBreak"
169− Link{} -> tryFilter "Link"
170151 Math DisplayMath _ -> tryFilterAlternatives ["DisplayMath", "Math"]
171152 Math InlineMath _ -> tryFilterAlternatives ["InlineMath", "Math"]
172− Note{} -> tryFilter "Note"
173153 Quoted DoubleQuote _ -> tryFilterAlternatives ["DoubleQuoted", "Quoted"]
174154 Quoted SingleQuote _ -> tryFilterAlternatives ["SingleQuoted", "Quoted"]
175− RawInline{} -> tryFilter "RawInline"
176− SmallCaps{} -> tryFilter "SmallCaps"
177− SoftBreak -> tryFilter "SoftBreak"
178− Space -> tryFilter "Space"
179− Span{} -> tryFilter "Span"
180− Str{} -> tryFilter "Str"
181− Strikeout{} -> tryFilter "Strikeout"
182− Strong{} -> tryFilter "Strong"
183− Subscript{} -> tryFilter "Subscript"
184− Superscript{} -> tryFilter "Superscript"
155+ _ -> tryFilter (show (toConstr x))
185156
186157instance StackValue LuaFilter where
187158 valuetype _ = Lua.TTABLE