From 6ad74815f66cb36ec4039c597b38473db853eb6c Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Thu, 29 Jun 2017 14:00:26 +0200
Subject: [PATCH] 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?
---
 src/Text/Pandoc/Lua.hs | 35 +++--------------------------------
 1 file changed, 3 insertions(+), 32 deletions(-)

diff --git a/src/Text/Pandoc/Lua.hs b/src/Text/Pandoc/Lua.hs
index 2ee8d0847..85a080277 100644
--- a/src/Text/Pandoc/Lua.hs
+++ b/src/Text/Pandoc/Lua.hs
@@ -36,6 +36,7 @@ module Text.Pandoc.Lua ( LuaException(..),
 import Control.Exception
 import Control.Monad (unless, when, (>=>))
 import Control.Monad.Trans (MonadIO (..))
+import Data.Data (toConstr)
 import Data.Map (Map)
 import Data.Typeable (Typeable)
 import Scripting.Lua (LuaState, StackValue (..))
@@ -129,21 +130,7 @@ execBlockLuaFilter lua fnMap x = do
         case Map.lookup filterFnName fnMap of
           Nothing -> return x
           Just fn -> runFilterFunction lua fn x
-  case x of
-    BlockQuote{}     -> tryFilter "BlockQuote"
-    BulletList{}     -> tryFilter "BulletList"
-    CodeBlock{}      -> tryFilter "CodeBlock"
-    DefinitionList{} -> tryFilter "DefinitionList"
-    Div{}            -> tryFilter "Div"
-    Header{}         -> tryFilter "Header"
-    HorizontalRule   -> tryFilter "HorizontalRule"
-    LineBlock{}      -> tryFilter "LineBlock"
-    Null             -> tryFilter "Null"
-    Para{}           -> tryFilter "Para"
-    Plain{}          -> tryFilter "Plain"
-    RawBlock{}       -> tryFilter "RawBlock"
-    OrderedList{}    -> tryFilter "OrderedList"
-    Table{}          -> tryFilter "Table"
+  tryFilter (show (toConstr x))
 
 execInlineLuaFilter :: LuaState
                     -> FunctionMap
@@ -161,27 +148,11 @@ execInlineLuaFilter lua fnMap x = do
           Nothing -> tryFilterAlternatives alternatives
           Just fn -> runFilterFunction lua fn x
   case x of
-    Cite{}               -> tryFilter "Cite"
-    Code{}               -> tryFilter "Code"
-    Emph{}               -> tryFilter "Emph"
-    Image{}              -> tryFilter "Image"
-    LineBreak            -> tryFilter "LineBreak"
-    Link{}               -> tryFilter "Link"
     Math DisplayMath _   -> tryFilterAlternatives ["DisplayMath", "Math"]
     Math InlineMath _    -> tryFilterAlternatives ["InlineMath", "Math"]
-    Note{}               -> tryFilter "Note"
     Quoted DoubleQuote _ -> tryFilterAlternatives ["DoubleQuoted", "Quoted"]
     Quoted SingleQuote _ -> tryFilterAlternatives ["SingleQuoted", "Quoted"]
-    RawInline{}          -> tryFilter "RawInline"
-    SmallCaps{}          -> tryFilter "SmallCaps"
-    SoftBreak            -> tryFilter "SoftBreak"
-    Space                -> tryFilter "Space"
-    Span{}               -> tryFilter "Span"
-    Str{}                -> tryFilter "Str"
-    Strikeout{}          -> tryFilter "Strikeout"
-    Strong{}             -> tryFilter "Strong"
-    Subscript{}          -> tryFilter "Subscript"
-    Superscript{}        -> tryFilter "Superscript"
+    _                    -> tryFilter (show (toConstr x))
 
 instance StackValue LuaFilter where
   valuetype _ = Lua.TTABLE