@@ -217,13 +217,18 @@ data OrgTableRow = OrgContentRow [Blocks]
217217 | OrgHlineRow
218218 deriving (Eq, Show)
219219
220−type OrgTableContent = (Int, [Alignment], [Double], [Blocks], [[Blocks]])
220+data OrgTable = OrgTable
221+ { orgTableColumns :: Int
222+ , orgTableAlignments :: [Alignment]
223+ , orgTableHeader :: [Blocks]
224+ , orgTableRows :: [[Blocks]]
225+ } deriving (Eq, Show)
221226
222227table :: OrgParser Blocks
223228table = try $ do
224229 lookAhead tableStart
225− (_, aligns, widths, heads, lns) <- normalizeTable . tableContent <$> tableRows
226− return $ B.table "" (zip aligns widths) heads lns
230+ OrgTable _ aligns heads lns <- normalizeTable . rowsToTable <$> tableRows
231+ return $ B.table "" (zip aligns $ repeat 0) heads lns
227232
228233tableStart :: OrgParser Char
229234tableStart = try $ skipSpaces *> char '|'
@@ -237,10 +242,9 @@ tableContentRow = try $
237242
238243tableContentCell :: OrgParser Blocks
239244tableContentCell = try $
240− B.plain . trimInlines . mconcat <$> many1Till inline (try endOfCell)
245+ B.plain . trimInlines . mconcat <$> many1Till inline endOfCell
241246
242247endOfCell :: OrgParser Char
243−-- endOfCell = char '|' <|> newline
244248endOfCell = try $ char '|' <|> lookAhead newline
245249
246250tableAlignRow :: OrgParser OrgTableRow
@@ -269,54 +273,53 @@ tableHline :: OrgParser OrgTableRow
269273tableHline = try $
270274 OrgHlineRow <$ (tableStart *> char '-' *> anyLine)
271275
272−tableContent :: [OrgTableRow]
273− -> OrgTableContent
274−tableContent = foldl' (flip rowToContent) (0, mempty, repeat 0, mempty, mempty)
276+rowsToTable :: [OrgTableRow]
277+ -> OrgTable
278+rowsToTable = foldl' (flip rowToContent) zeroTable
279+ where zeroTable = OrgTable 0 mempty mempty mempty
275280
276−normalizeTable :: OrgTableContent
277− -> OrgTableContent
278−normalizeTable (cols, aligns, widths, heads, lns) =
281+normalizeTable :: OrgTable
282+ -> OrgTable
283+normalizeTable (OrgTable cols aligns heads lns) =
279284 let aligns' = fillColumns aligns AlignDefault
280− widths' = fillColumns widths 0.0
281285 heads' = if heads == mempty
282− then heads
286+ then mempty
283287 else fillColumns heads (B.plain mempty)
284288 lns' = map (flip fillColumns (B.plain mempty)) lns
285289 fillColumns base padding = take cols $ base ++ repeat padding
286− in (cols, aligns', widths', heads', lns')
290+ in OrgTable cols aligns' heads' lns'
287291
288292
289293-- One or more horizontal rules after the first content line mark the previous
290294-- line as a header. All other horizontal lines are discarded.
291295rowToContent :: OrgTableRow
292− -> OrgTableContent
293− -> OrgTableContent
296+ -> OrgTable
297+ -> OrgTable
294298rowToContent OrgHlineRow = maybeBodyToHeader
295299rowToContent (OrgContentRow rs) = setLongestRow rs . appendToBody rs
296300rowToContent (OrgAlignRow as) = setLongestRow as . setAligns as
297301
298302setLongestRow :: [a]
299− -> OrgTableContent
300− -> OrgTableContent
301−setLongestRow r (cols, aligns, widths, heads, lns) =
302− (max cols (length r), aligns, widths, heads, lns)
303+ -> OrgTable
304+ -> OrgTable
305+setLongestRow rs t = t{ orgTableColumns = max (length rs) (orgTableColumns t) }
303306
304−maybeBodyToHeader :: OrgTableContent
305− -> OrgTableContent
306−maybeBodyToHeader (cols, aligns, widths, [], b:[]) = (cols, aligns, widths, b, [])
307−maybeBodyToHeader content = content
307+maybeBodyToHeader :: OrgTable
308+ -> OrgTable
309+maybeBodyToHeader t = case t of
310+ OrgTable{ orgTableHeader = [], orgTableRows = b:[] } ->
311+ t{ orgTableHeader = b , orgTableRows = [] }
312+ _ -> t
308313
309314appendToBody :: [Blocks]
310− -> OrgTableContent
311− -> OrgTableContent
312−appendToBody r (cols, aligns, widths, heads, lns) =
313− (cols, aligns, widths, heads, lns ++ [r])
315+ -> OrgTable
316+ -> OrgTable
317+appendToBody r t = t{ orgTableRows = orgTableRows t ++ [r] }
314318
315319setAligns :: [Alignment]
316− -> OrgTableContent
317− -> OrgTableContent
318−setAligns aligns (cols, _, widths, heads, lns) =
319− (cols, aligns, widths, heads, lns)
320+ -> OrgTable
321+ -> OrgTable
322+setAligns aligns t = t{ orgTableAlignments = aligns }
320323
321324-- Paragraphs or Plain text
322325paraOrPlain :: OrgParser Blocks
@@ -549,4 +552,3 @@ endsOnThisLine input c doOnOtherLines = do
549552 then return ()
550553 else endsOnThisLine rest c doOnOtherLines
551554 _ -> mzero
552−