Org reader: refactor rows-to-table conversion

This refactores the codes conversing a list table lines to an org table
ADT.  The old code was simplified and is now slightly less ugly.
This commit is contained in:
Albert Krewinkel 2016-05-04 00:03:48 +02:00
parent d5e4bc179c
commit a51e4e8215

View file

@ -844,7 +844,7 @@ tableHline = try $
rowsToTable :: [OrgTableRow] rowsToTable :: [OrgTableRow]
-> F OrgTable -> F OrgTable
rowsToTable = foldM (flip rowToContent) emptyTable rowsToTable = foldM rowToContent emptyTable
where emptyTable = OrgTable mempty mempty mempty where emptyTable = OrgTable mempty mempty mempty
normalizeTable :: OrgTable -> OrgTable normalizeTable :: OrgTable -> OrgTable
@ -859,31 +859,31 @@ normalizeTable (OrgTable aligns heads rows) = OrgTable aligns' heads rows
-- One or more horizontal rules after the first content line mark the previous -- One or more horizontal rules after the first content line mark the previous
-- line as a header. All other horizontal lines are discarded. -- line as a header. All other horizontal lines are discarded.
rowToContent :: OrgTableRow rowToContent :: OrgTable
-> OrgTable -> OrgTableRow
-> F OrgTable -> F OrgTable
rowToContent OrgHlineRow t = maybeBodyToHeader t rowToContent orgTable row =
rowToContent (OrgAlignRow as) t = setAligns as t case row of
rowToContent (OrgContentRow rf) t = do OrgHlineRow -> return singleRowPromotedToHeader
rs <- rf OrgAlignRow as -> return . setAligns $ as
appendToBody rs t OrgContentRow cs -> appendToBody cs
where
maybeBodyToHeader :: OrgTable singleRowPromotedToHeader :: OrgTable
-> F OrgTable singleRowPromotedToHeader = case orgTable of
maybeBodyToHeader t = case t of
OrgTable{ orgTableHeader = [], orgTableRows = b:[] } -> OrgTable{ orgTableHeader = [], orgTableRows = b:[] } ->
return t{ orgTableHeader = b , orgTableRows = [] } orgTable{ orgTableHeader = b , orgTableRows = [] }
_ -> return t _ -> orgTable
appendToBody :: [Blocks] setAligns :: [Alignment] -> OrgTable
-> OrgTable setAligns aligns = orgTable{ orgTableAlignments = aligns }
-> F OrgTable
appendToBody r t = return t{ orgTableRows = orgTableRows t ++ [r] }
setAligns :: [Alignment] appendToBody :: F [Blocks] -> F OrgTable
-> OrgTable appendToBody frow = do
-> F OrgTable newRow <- frow
setAligns aligns t = return $ t{ orgTableAlignments = aligns } let oldRows = orgTableRows orgTable
-- NOTE: This is an inefficient O(n) operation. This should be changed
-- if performance ever becomes a problem.
return orgTable{ orgTableRows = oldRows ++ [newRow] }
-- --