Skip to content

Commit 005891f

Browse files
committed
Allow treatment of custom elements as singleton lists
1 parent 2dc58d4 commit 005891f

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

src/Text/Pandoc/Lua/Marshal/Block.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ peekBlockMetamethod idx = do
128128
liftLua (pop 1) -- drop "__toblock" field
129129
failPeek "__toblock metafield does not contain a function"
130130

131-
-- | Try extra hard to retrieve an Block value from the stack. Treats
131+
-- | Try extra hard to retrieve a Block value from the stack. Treats
132132
-- bare strings as @Str@ values.
133133
peekBlockFuzzy :: LuaError e
134134
=> Peeker e Block
@@ -141,11 +141,12 @@ peekBlockFuzzy idx =
141141
{-# INLINABLE peekBlockFuzzy #-}
142142

143143
-- | Try extra-hard to return the value at the given index as a list of
144-
-- inlines.
144+
-- 'Block's.
145145
peekBlocksFuzzy :: LuaError e
146146
=> Peeker e [Block]
147147
peekBlocksFuzzy idx =
148-
peekList peekBlockFuzzy idx
148+
((:[]) <$> peekBlockMetamethod idx)
149+
<|> peekList peekBlockFuzzy idx
149150
<|> (pure <$!> peekBlockFuzzy idx)
150151
<|> (failPeek =<<
151152
typeMismatchMessage "Block, list of Blocks, or compatible element" idx)

src/Text/Pandoc/Lua/Marshal/Inline.hs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,22 @@ peekInlineFuzzy :: LuaError e => Peeker e Inline
128128
peekInlineFuzzy idx = retrieving "Inline" $ liftLua (ltype idx) >>= \case
129129
TypeString -> Str <$!> peekText idx
130130
TypeTable -> peekInlineMetamethod idx <|> peekInline idx
131-
_ -> peekInline idx <|> peekInlineMetamethod idx
131+
TypeUserdata -> peekInline idx <|> peekInlineMetamethod idx
132+
_type -> failPeek =<<
133+
typeMismatchMessage "Inline-ish" idx
132134
{-# INLINABLE peekInlineFuzzy #-}
133135

134136
-- | Try extra-hard to return the value at the given index as a list of
135137
-- inlines.
136138
peekInlinesFuzzy :: LuaError e
137139
=> Peeker e [Inline]
138140
peekInlinesFuzzy idx = liftLua (ltype idx) >>= \case
139-
TypeString -> B.toList . B.text <$> peekText idx
140-
_ -> peekList peekInlineFuzzy idx
141-
<|> (pure <$> peekInlineFuzzy idx)
142-
<|> (failPeek =<<
143-
typeMismatchMessage "Inline, list of Inlines, or string" idx)
141+
TypeString -> B.toList . B.text <$> peekText idx
142+
TypeTable -> ((:[]) <$> peekInlineMetamethod idx)
143+
<|> peekList peekInlineFuzzy idx
144+
TypeUserdata -> ((:[]) <$> peekInlineFuzzy idx)
145+
_type -> failPeek =<<
146+
typeMismatchMessage "Inline, list of Inlines, or string" idx
144147
{-# INLINABLE peekInlinesFuzzy #-}
145148

146149
-- | Inline object type.

test/test-block.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,21 @@ return {
710710
Blocks{Plain{'b'}, bad_block}
711711
)
712712
end),
713+
714+
test('object with metamethod can be used as singleton list', function ()
715+
local function toblock (t)
716+
return CodeBlock(t.code, {id = t.id, class = t.class})
717+
end
718+
local my_code = setmetatable(
719+
{code = 'open access', id='opn'},
720+
{__toblock = toblock}
721+
)
722+
assert.are_equal(
723+
Div(CodeBlock('open access', {'opn'})),
724+
Div(my_code)
725+
)
726+
end),
727+
713728
}
714729
}
715730
}

test/test-inline.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,20 @@ return {
497497
Inlines(bad_inline)
498498
)
499499
end),
500+
501+
test("objects can be used as singleton lists", function ()
502+
local function toinline (t)
503+
return Code(t.code, {id = t.id, class = t.class})
504+
end
505+
local my_code = setmetatable(
506+
{code = 'open access', id='opn'},
507+
{__toinline = toinline}
508+
)
509+
assert.are_equal(
510+
Inlines(Code('open access', {'opn'})),
511+
Inlines(my_code)
512+
)
513+
end)
500514
}
501515
}
502516
}

0 commit comments

Comments
 (0)