File tree Expand file tree Collapse file tree 4 files changed +42
-9
lines changed
src/Text/Pandoc/Lua/Marshal Expand file tree Collapse file tree 4 files changed +42
-9
lines changed Original file line number Diff line number Diff line change @@ -128,7 +128,7 @@ peekBlockMetamethod idx = do
128
128
liftLua (pop 1 ) -- drop "__toblock" field
129
129
failPeek " __toblock metafield does not contain a function"
130
130
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
132
132
-- bare strings as @Str@ values.
133
133
peekBlockFuzzy :: LuaError e
134
134
=> Peeker e Block
@@ -141,11 +141,12 @@ peekBlockFuzzy idx =
141
141
{-# INLINABLE peekBlockFuzzy #-}
142
142
143
143
-- | Try extra-hard to return the value at the given index as a list of
144
- -- inlines .
144
+ -- 'Block's .
145
145
peekBlocksFuzzy :: LuaError e
146
146
=> Peeker e [Block ]
147
147
peekBlocksFuzzy idx =
148
- peekList peekBlockFuzzy idx
148
+ ((: [] ) <$> peekBlockMetamethod idx)
149
+ <|> peekList peekBlockFuzzy idx
149
150
<|> (pure <$!> peekBlockFuzzy idx)
150
151
<|> (failPeek =<<
151
152
typeMismatchMessage " Block, list of Blocks, or compatible element" idx)
Original file line number Diff line number Diff line change @@ -128,19 +128,22 @@ peekInlineFuzzy :: LuaError e => Peeker e Inline
128
128
peekInlineFuzzy idx = retrieving " Inline" $ liftLua (ltype idx) >>= \ case
129
129
TypeString -> Str <$!> peekText idx
130
130
TypeTable -> peekInlineMetamethod idx <|> peekInline idx
131
- _ -> peekInline idx <|> peekInlineMetamethod idx
131
+ TypeUserdata -> peekInline idx <|> peekInlineMetamethod idx
132
+ _type -> failPeek =<<
133
+ typeMismatchMessage " Inline-ish" idx
132
134
{-# INLINABLE peekInlineFuzzy #-}
133
135
134
136
-- | Try extra-hard to return the value at the given index as a list of
135
137
-- inlines.
136
138
peekInlinesFuzzy :: LuaError e
137
139
=> Peeker e [Inline ]
138
140
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
144
147
{-# INLINABLE peekInlinesFuzzy #-}
145
148
146
149
-- | Inline object type.
Original file line number Diff line number Diff line change @@ -710,6 +710,21 @@ return {
710
710
Blocks {Plain {' b' }, bad_block }
711
711
)
712
712
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
+
713
728
}
714
729
}
715
730
}
Original file line number Diff line number Diff line change @@ -497,6 +497,20 @@ return {
497
497
Inlines (bad_inline )
498
498
)
499
499
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 )
500
514
}
501
515
}
502
516
}
You can’t perform that action at this time.
0 commit comments