@@ -21,7 +21,6 @@ import Foreign.Lua (Lua, Peekable, Pushable, StackIndex)
2121import Text.Pandoc.Definition
2222import Text.Pandoc.Lua.Util (defineHowTo, pushViaConstructor)
2323import Text.Pandoc.Lua.Marshaling.CommonState ()
24−import Text.Pandoc.Writers.Shared (toLegacyTable)
2524
2625import qualified Foreign.Lua as Lua
2726import qualified Text.Pandoc.Lua.Util as LuaUtil
@@ -151,7 +150,7 @@ peekMetaValue idx = defineHowTo "get MetaValue" $ do
151150 <|> (MetaList <$> Lua.peek idx)
152151 _ -> Lua.throwException "could not get meta value"
153152
154−-- | Push an block element to the top of the lua stack.
153+-- | Push a block element to the top of the Lua stack.
155154pushBlock :: Block -> Lua ()
156155pushBlock = \case
157156 BlockQuote blcks -> pushViaConstructor "BlockQuote" blcks
@@ -168,9 +167,8 @@ pushBlock = \case
168167 Para blcks -> pushViaConstructor "Para" blcks
169168 Plain blcks -> pushViaConstructor "Plain" blcks
170169 RawBlock f cs -> pushViaConstructor "RawBlock" f cs
171− Table _ blkCapt specs thead tbody tfoot ->
172− let (capt, aligns, widths, headers, rows) = toLegacyTable blkCapt specs thead tbody tfoot
173− in pushViaConstructor "Table" capt aligns widths headers rows
170+ Table attr blkCapt specs thead tbody tfoot ->
171+ pushViaConstructor "Table" attr blkCapt specs thead tbody tfoot
174172
175173-- | Return the value at the given index as block if possible.
176174peekBlock :: StackIndex -> Lua Block
@@ -193,13 +191,13 @@ peekBlock idx = defineHowTo "get Block value" $ do
193191 "Para" -> Para <$> elementContent
194192 "Plain" -> Plain <$> elementContent
195193 "RawBlock" -> uncurry RawBlock <$> elementContent
196− "Table" -> (\(capt, aligns, widths, headers, body) ->
197− Table nullAttr
198− (Caption Nothing $ maybePlain capt)
199− (zip aligns (map strictPos widths))
200− (TableHead nullAttr $ toHeaderRow headers)
201− [TableBody nullAttr 0 [] (map toRow body)]
202− (TableFoot nullAttr []))
194+ "Table" -> (\(attr, capt, colSpecs, thead, tbodies, tfoot) ->
195+ Table (fromLuaAttr attr)
196+ capt
197+ colSpecs
198+ thead
199+ tbodies
200+ tfoot)
203201 <$> elementContent
204202 _ -> Lua.throwException ("Unknown block type: " <> tag)
205203 where
@@ -207,11 +205,95 @@ peekBlock idx = defineHowTo "get Block value" $ do
207205 elementContent :: Peekable a => Lua a
208206 elementContent = LuaUtil.rawField idx "c"
209207
210− strictPos w = if w > 0 then ColWidth w else ColWidthDefault
211− maybePlain [] = []
212− maybePlain x = [Plain x]
213− toRow = Row nullAttr . map (\blk -> Cell nullAttr AlignDefault 1 1 blk)
214− toHeaderRow l = if null l then [] else [toRow l]
208+instance Pushable Caption where
209+ push = pushCaption
210+
211+instance Peekable Caption where
212+ peek = peekCaption
213+
214+-- | Push Caption element
215+pushCaption :: Caption -> Lua ()
216+pushCaption (Caption shortCaption longCaption) = do
217+ Lua.newtable
218+ LuaUtil.addField "short" (Lua.Optional shortCaption)
219+ LuaUtil.addField "long" longCaption
220+
221+-- | Peek Caption element
222+peekCaption :: StackIndex -> Lua Caption
223+peekCaption idx = do
224+ short <- Lua.fromOptional <$> LuaUtil.rawField idx "short"
225+ long <- LuaUtil.rawField idx "long"
226+ return $ Caption short long
227+
228+instance Peekable ColWidth where
229+ peek idx = do
230+ width <- Lua.fromOptional <$> Lua.peek idx
231+ return $ case width of
232+ Nothing -> ColWidthDefault
233+ Just w -> ColWidth w
234+
235+instance Pushable ColWidth where
236+ push = \case
237+ (ColWidth w) -> Lua.push w
238+ ColWidthDefault -> Lua.pushnil
239+
240+instance Pushable Row where
241+ push (Row attr cells) = Lua.push (attr, cells)
242+
243+instance Peekable Row where
244+ peek = fmap (uncurry Row) . Lua.peek
245+
246+instance Pushable TableBody where
247+ push (TableBody attr (RowHeadColumns rowHeadColumns) head' body) = do
248+ Lua.newtable
249+ LuaUtil.addField "attr" attr
250+ LuaUtil.addField "row_head_columns" rowHeadColumns
251+ LuaUtil.addField "head" head'
252+ LuaUtil.addField "body" body
253+
254+instance Peekable TableBody where
255+ peek idx = do
256+ attr <- LuaUtil.rawField idx "attr"
257+ rowHeadColumns <- LuaUtil.rawField idx "row_head_columns"
258+ head' <- LuaUtil.rawField idx "head"
259+ body <- LuaUtil.rawField idx "body"
260+ return $ TableBody attr (RowHeadColumns rowHeadColumns) head' body
261+
262+instance Pushable TableHead where
263+ push (TableHead attr cells) = Lua.push (attr, cells)
264+
265+instance Peekable TableHead where
266+ peek = fmap (uncurry TableHead) . Lua.peek
267+
268+instance Pushable TableFoot where
269+ push (TableFoot attr cells) = Lua.push (attr, cells)
270+
271+instance Peekable TableFoot where
272+ peek = fmap (uncurry TableFoot) . Lua.peek
273+
274+instance Pushable Cell where
275+ push = pushCell
276+
277+instance Peekable Cell where
278+ peek = peekCell
279+
280+pushCell :: Cell -> Lua ()
281+pushCell (Cell attr align (RowSpan rowSpan) (ColSpan colSpan) contents) = do
282+ Lua.newtable
283+ LuaUtil.addField "attr" attr
284+ LuaUtil.addField "alignment" align
285+ LuaUtil.addField "row_span" rowSpan
286+ LuaUtil.addField "col_span" colSpan
287+ LuaUtil.addField "contents" contents
288+
289+peekCell :: StackIndex -> Lua Cell
290+peekCell idx = do
291+ attr <- fromLuaAttr <$> LuaUtil.rawField idx "attr"
292+ align <- LuaUtil.rawField idx "alignment"
293+ rowSpan <- LuaUtil.rawField idx "row_span"
294+ colSpan <- LuaUtil.rawField idx "col_span"
295+ contents <- LuaUtil.rawField idx "contents"
296+ return $ Cell attr align (RowSpan rowSpan) (ColSpan colSpan) contents
215297
216298-- | Push an inline element to the top of the lua stack.
217299pushInline :: Inline -> Lua ()