Lua filter: revert to non-destructuring filters

We want to provide an interface familiar to users of other filtering
libraries.
This commit is contained in:
Albert Krewinkel 2017-04-15 21:40:48 +02:00
parent 291e4f39e8
commit e6a536befc
No known key found for this signature in database
GPG key ID: 388DC0B21F631124
5 changed files with 58 additions and 77 deletions

View file

@ -99,84 +99,64 @@ execBlockLuaFilter :: LuaState
-> Map String (LuaFilterFunction Block) -> Map String (LuaFilterFunction Block)
-> Block -> IO Block -> Block -> IO Block
execBlockLuaFilter lua fnMap x = do execBlockLuaFilter lua fnMap x = do
let runFn :: PushViaFilterFunction Block a => LuaFilterFunction Block -> a let tryFilter :: String -> IO Block
runFn fn = runLuaFilterFunction lua fn tryFilter filterFnName =
let tryFilter :: String -> (LuaFilterFunction Block -> IO Block) -> IO Block case Map.lookup filterFnName fnMap of
tryFilter fnName callFilterFn =
case Map.lookup fnName fnMap of
Nothing -> return x Nothing -> return x
Just fn -> callFilterFn fn Just fn -> runLuaFilterFunction lua fn x
case x of case x of
HorizontalRule -> tryFilter "HorizontalRule" runFn BlockQuote _ -> tryFilter "BlockQuote"
Null -> tryFilter "Null" runFn BulletList _ -> tryFilter "BulletList"
BlockQuote blcks -> tryFilter "BlockQuote" $ \fn -> runFn fn blcks CodeBlock _ _ -> tryFilter "CodeBlock"
BulletList items -> tryFilter "BulletList" $ \fn -> runFn fn items DefinitionList _ -> tryFilter "DefinitionList"
CodeBlock attr code -> tryFilter "CodeBlock" $ \fn -> runFn fn attr code Div _ _ -> tryFilter "Div"
DefinitionList lst -> tryFilter "DefinitionList" $ \fn -> runFn fn lst Header _ _ _ -> tryFilter "Header"
Div attr content -> tryFilter "Div" $ \fn -> runFn fn content attr HorizontalRule -> tryFilter "HorizontalRule"
Header lvl attr inlns -> tryFilter "Header" $ \fn -> runFn fn lvl inlns attr LineBlock _ -> tryFilter "LineBlock"
LineBlock inlns -> tryFilter "LineBlock" $ \fn -> runFn fn inlns Null -> tryFilter "Null"
Para inlns -> tryFilter "Para" $ \fn -> runFn fn inlns Para _ -> tryFilter "Para"
Plain inlns -> tryFilter "Plain" $ \fn -> runFn fn inlns Plain _ -> tryFilter "Plain"
RawBlock format str -> tryFilter "RawBlock" $ \fn -> runFn fn format str RawBlock _ _ -> tryFilter "RawBlock"
OrderedList (num,sty,delim) items -> OrderedList _ _ -> tryFilter "OrderedList"
tryFilter "OrderedList" $ \fn -> runFn fn items (num,sty,delim) Table _ _ _ _ _ -> tryFilter "Table"
Table capt aligns widths headers rows ->
tryFilter "Table" $ \fn -> runFn fn capt aligns widths headers rows
execInlineLuaFilter :: LuaState execInlineLuaFilter :: LuaState
-> Map String (LuaFilterFunction Inline) -> Map String (LuaFilterFunction Inline)
-> Inline -> IO Inline -> Inline -> IO Inline
execInlineLuaFilter lua fnMap x = do execInlineLuaFilter lua fnMap x = do
let runFn :: PushViaFilterFunction Inline a => LuaFilterFunction Inline -> a let tryFilter :: String -> IO Inline
runFn fn = runLuaFilterFunction lua fn tryFilter filterFnName =
let tryFilter :: String -> (LuaFilterFunction Inline -> IO Inline) -> IO Inline case Map.lookup filterFnName fnMap of
tryFilter fnName callFilterFn =
case Map.lookup fnName fnMap of
Nothing -> return x Nothing -> return x
Just fn -> callFilterFn fn Just fn -> runLuaFilterFunction lua fn x
let tryFilterAlternatives :: [(String, LuaFilterFunction Inline -> IO Inline)] -> IO Inline let tryFilterAlternatives :: [String] -> IO Inline
tryFilterAlternatives [] = return x tryFilterAlternatives [] = return x
tryFilterAlternatives ((fnName, callFilterFn) : alternatives) = tryFilterAlternatives (fnName : alternatives) =
case Map.lookup fnName fnMap of case Map.lookup fnName fnMap of
Nothing -> tryFilterAlternatives alternatives Nothing -> tryFilterAlternatives alternatives
Just fn -> callFilterFn fn Just fn -> runLuaFilterFunction lua fn x
case x of case x of
LineBreak -> tryFilter "LineBreak" runFn Cite _ _ -> tryFilter "Cite"
SoftBreak -> tryFilter "SoftBreak" runFn Code _ _ -> tryFilter "Code"
Space -> tryFilter "Space" runFn Emph _ -> tryFilter "Emph"
Cite cs lst -> tryFilter "Cite" $ \fn -> runFn fn lst cs Image _ _ _ -> tryFilter "Image"
Code attr str -> tryFilter "Code" $ \fn -> runFn fn str attr LineBreak -> tryFilter "LineBreak"
Emph lst -> tryFilter "Emph" $ \fn -> runFn fn lst Link _ _ _ -> tryFilter "Link"
Note blks -> tryFilter "Note" $ \fn -> runFn fn blks Math DisplayMath _ -> tryFilterAlternatives ["DisplayMath", "Math"]
RawInline f str -> tryFilter "RawInline" $ \fn -> runFn fn f str Math InlineMath _ -> tryFilterAlternatives ["InlineMath", "Math"]
SmallCaps lst -> tryFilter "SmallCaps" $ \fn -> runFn fn lst Note _ -> tryFilter "Note"
Span attr lst -> tryFilter "Span" $ \fn -> runFn fn lst attr Quoted DoubleQuote _ -> tryFilterAlternatives ["DoubleQuoted", "Quoted"]
Str str -> tryFilter "Str" $ \fn -> runFn fn str Quoted SingleQuote _ -> tryFilterAlternatives ["SingleQuoted", "Quoted"]
Strikeout lst -> tryFilter "Strikeout" $ \fn -> runFn fn lst RawInline _ _ -> tryFilter "RawInline"
Strong lst -> tryFilter "Strong" $ \fn -> runFn fn lst SmallCaps _ -> tryFilter "SmallCaps"
Subscript lst -> tryFilter "Subscript" $ \fn -> runFn fn lst SoftBreak -> tryFilter "SoftBreak"
Superscript lst -> tryFilter "Superscript" $ \fn -> runFn fn lst Space -> tryFilter "Space"
Math DisplayMath lst -> tryFilterAlternatives Span _ _ -> tryFilter "Span"
[ ("DisplayMath", \fn -> runFn fn lst) Str _ -> tryFilter "Str"
, ("Math", \fn -> runFn fn DisplayMath lst) Strikeout _ -> tryFilter "Strikeout"
] Strong _ -> tryFilter "Strong"
Math InlineMath lst -> tryFilterAlternatives Subscript _ -> tryFilter "Subscript"
[ ("InlineMath", \fn -> runFn fn lst) Superscript _ -> tryFilter "Superscript"
, ("Math", \fn -> runFn fn InlineMath lst)
]
Quoted SingleQuote lst -> tryFilterAlternatives
[ ("SingleQuoted", \fn -> runFn fn lst)
, ("Quoted", \fn -> runFn fn SingleQuote lst)
]
Quoted DoubleQuote lst -> tryFilterAlternatives
[ ("DoubleQuoted", \fn -> runFn fn lst)
, ("Quoted", \fn -> runFn fn DoubleQuote lst)
]
Link attr txt (src, tit) -> tryFilter "Link" $
\fn -> runFn fn txt src tit attr
Image attr alt (src, tit) -> tryFilter "Image" $
\fn -> runFn fn alt src tit attr
instance StackValue LuaFilter where instance StackValue LuaFilter where
valuetype _ = Lua.TTABLE valuetype _ = Lua.TTABLE

View file

@ -1,10 +1,10 @@
return { return {
{ {
RawBlock = function (format, content) RawBlock = function (elem)
if format == "markdown" then if elem.format == "markdown" then
return pandoc.reader.markdown.read_block(content) return pandoc.reader.markdown.read_block(elem.text)
else else
return blk return elem
end end
end, end,
} }

View file

@ -1,6 +1,6 @@
return { return {
{ Plain = function (content) { Plain = function (elem)
return pandoc.Para(content) return pandoc.Para(elem.content)
end, end,
} }
} }

View file

@ -1,7 +1,8 @@
return { return {
{ {
SingleQuoted = function (content) SingleQuoted = function (elem)
return pandoc.Quoted("DoubleQuote", content) elem.quotetype = "DoubleQuote"
return elem
end, end,
} }
} }

View file

@ -1,10 +1,10 @@
return { return {
{ {
Str = function (str) Str = function (elem)
if str == "{{helloworld}}" then if elem.text == "{{helloworld}}" then
return pandoc.Emph {pandoc.Str "Hello, World"} return pandoc.Emph {pandoc.Str "Hello, World"}
else else
return pandoc.Str(str) return elem
end end
end, end,
} }