git.iko.soy/typeable/pandoc
on master help sign in
Lua filter: allow shorthand functions for math and quoted

Allow to use functions named `SingleQuoted`, `DoubleQuoted`, `DisplayMath`, and `InlineMath` in filters.

commit3aeed816e163b1ad3925caff0496fa05a63d1369 authorAlbert Krewinkel <albert@zeitkraut.de> dateFri 14 Apr 2017, 23:24 (10 years ago) parenteb8de65
4 files changed+52−21
pandoc.cabal+1−4 src/Text/Pandoc/Lua.hs+23−3 test/Tests/Lua.hs+21−14 test/lua/single-to-double-quoted.lua+7
pandoc.cabal+1−4
@@ -247,10 +247,7 @@ Extra-Source-Files:
247247 test/odt/odt/*.odt
248248 test/odt/markdown/*.md
249249 test/odt/native/*.native
250− test/lua/hello-world-doc.lua
251− test/lua/markdown-reader.lua
252− test/lua/plain-to-para.lua
253− test/lua/strmacro.lua
250+ test/lua/*.lua
254251Source-repository head
255252 type: git
256253 location: git://github.com/jgm/pandoc.git
src/Text/Pandoc/Lua.hs+23−3
@@ -34,7 +34,7 @@ import Control.Monad ( (>=>), when )
3434import Control.Monad.Trans ( MonadIO(..) )
3535import Data.Map ( Map )
3636import Scripting.Lua ( LuaState, StackValue(..) )
37−import Text.Pandoc.Definition ( Block(..), Inline(..), Pandoc(..) )
37+import Text.Pandoc.Definition
3838import Text.Pandoc.Lua.PandocModule ( pushPandocModule )
3939import Text.Pandoc.Lua.StackInstances ()
4040import Text.Pandoc.Walk
@@ -135,6 +135,12 @@ execInlineLuaFilter lua fnMap x = do
135135 case Map.lookup fnName fnMap of
136136 Nothing -> return x
137137 Just fn -> callFilterFn fn
138+ let tryFilterAlternatives :: [(String, LuaFilterFunction Inline -> IO Inline)] -> IO Inline
139+ tryFilterAlternatives [] = return x
140+ tryFilterAlternatives ((fnName, callFilterFn) : alternatives) =
141+ case Map.lookup fnName fnMap of
142+ Nothing -> tryFilterAlternatives alternatives
143+ Just fn -> callFilterFn fn
138144 case x of
139145 LineBreak -> tryFilter "LineBreak" runFn
140146 SoftBreak -> tryFilter "SoftBreak" runFn
@@ -142,9 +148,7 @@ execInlineLuaFilter lua fnMap x = do
142148 Cite cs lst -> tryFilter "Cite" $ \fn -> runFn fn lst cs
143149 Code attr str -> tryFilter "Code" $ \fn -> runFn fn str attr
144150 Emph lst -> tryFilter "Emph" $ \fn -> runFn fn lst
145− Math mt lst -> tryFilter "Math" $ \fn -> runFn fn lst mt
146151 Note blks -> tryFilter "Note" $ \fn -> runFn fn blks
147− Quoted qt lst -> tryFilter "Quoted" $ \fn -> runFn fn qt lst
148152 RawInline f str -> tryFilter "RawInline" $ \fn -> runFn fn f str
149153 SmallCaps lst -> tryFilter "SmallCaps" $ \fn -> runFn fn lst
150154 Span attr lst -> tryFilter "Span" $ \fn -> runFn fn lst attr
@@ -153,6 +157,22 @@ execInlineLuaFilter lua fnMap x = do
153157 Strong lst -> tryFilter "Strong" $ \fn -> runFn fn lst
154158 Subscript lst -> tryFilter "Subscript" $ \fn -> runFn fn lst
155159 Superscript lst -> tryFilter "Superscript" $ \fn -> runFn fn lst
160+ Math DisplayMath lst -> tryFilterAlternatives
161+ [ ("DisplayMath", \fn -> runFn fn lst)
162+ , ("Math", \fn -> runFn fn DisplayMath lst)
163+ ]
164+ Math InlineMath lst -> tryFilterAlternatives
165+ [ ("InlineMath", \fn -> runFn fn lst)
166+ , ("Math", \fn -> runFn fn InlineMath lst)
167+ ]
168+ Quoted SingleQuote lst -> tryFilterAlternatives
169+ [ ("SingleQuoted", \fn -> runFn fn lst)
170+ , ("Quoted", \fn -> runFn fn SingleQuote lst)
171+ ]
172+ Quoted DoubleQuote lst -> tryFilterAlternatives
173+ [ ("DoubleQuoted", \fn -> runFn fn lst)
174+ , ("Quoted", \fn -> runFn fn DoubleQuote lst)
175+ ]
156176 Link attr txt (src, tit) -> tryFilter "Link" $
157177 \fn -> runFn fn txt src tit attr
158178 Image attr alt (src, tit) -> tryFilter "Image" $
test/Tests/Lua.hs+21−14
@@ -8,15 +8,28 @@ import Test.Tasty.HUnit (Assertion, assertEqual, testCase)
88import Test.Tasty.QuickCheck (ioProperty, testProperty)
99import Text.Pandoc.Arbitrary ()
1010import Text.Pandoc.Definition (Block, Inline, Meta, Pandoc)
11−import Text.Pandoc.Builder ( (<>), bulletList, doc, emph, linebreak, rawBlock
12− , para, plain, space, str, strong)
11+import Text.Pandoc.Builder ( (<>), bulletList, doc, doubleQuoted, emph
12+ , linebreak, rawBlock, singleQuoted, para, plain
13+ , space, str, strong)
1314import Text.Pandoc.Lua
1415
1516import qualified Scripting.Lua as Lua
1617
1718tests :: [TestTree]
1819tests =
19− [ testCase "macro expansion via filter" $
20+ [ testProperty "inline elements can be round-tripped through the lua stack" $
21+ \x -> ioProperty (roundtripEqual (x::Inline))
22+
23+ , testProperty "block elements can be round-tripped through the lua stack" $
24+ \x -> ioProperty (roundtripEqual (x::Block))
25+
26+ , testProperty "meta blocks can be round-tripped through the lua stack" $
27+ \x -> ioProperty (roundtripEqual (x::Meta))
28+
29+ , testProperty "documents can be round-tripped through the lua stack" $
30+ \x -> ioProperty (roundtripEqual (x::Pandoc))
31+
32+ , testCase "macro expansion via filter" $
2033 assertFilterConversion "a '{{helloworld}}' string is expanded"
2134 "strmacro.lua"
2235 (doc . para $ str "{{helloworld}}")
@@ -40,17 +53,11 @@ tests =
4053 (doc $ rawBlock "markdown" "*charly* **delta**")
4154 (doc . para $ emph "charly" <> space <> strong "delta")
4255
43− , testProperty "inline elements can be round-tripped through the lua stack" $
44− \x -> ioProperty (roundtripEqual (x::Inline))
45−
46− , testProperty "block elements can be round-tripped through the lua stack" $
47− \x -> ioProperty (roundtripEqual (x::Block))
48−
49− , testProperty "meta blocks can be round-tripped through the lua stack" $
50− \x -> ioProperty (roundtripEqual (x::Meta))
51−
52− , testProperty "documents can be round-tripped through the lua stack" $
53− \x -> ioProperty (roundtripEqual (x::Pandoc))
56+ , testCase "allow shorthand functions for quote types" $
57+ assertFilterConversion "single quoted becomes double quoted string"
58+ "single-to-double-quoted.lua"
59+ (doc . para . singleQuoted $ str "simple")
60+ (doc . para . doubleQuoted $ str "simple")
5461 ]
5562
5663assertFilterConversion :: String -> FilePath -> Pandoc -> Pandoc -> Assertion
test/lua/single-to-double-quoted.luanew file+7
@@ -0,0 +1,7 @@
1+return {
2+ {
3+ SingleQuoted = function (content)
4+ return pandoc.Quoted("DoubleQuote", content)
5+ end,
6+ }
7+}