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:
parent
d5e4bc179c
commit
a51e4e8215
1 changed files with 23 additions and 23 deletions
|
@ -844,7 +844,7 @@ tableHline = try $
|
|||
|
||||
rowsToTable :: [OrgTableRow]
|
||||
-> F OrgTable
|
||||
rowsToTable = foldM (flip rowToContent) emptyTable
|
||||
rowsToTable = foldM rowToContent emptyTable
|
||||
where emptyTable = OrgTable mempty mempty mempty
|
||||
|
||||
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
|
||||
-- line as a header. All other horizontal lines are discarded.
|
||||
rowToContent :: OrgTableRow
|
||||
-> OrgTable
|
||||
rowToContent :: OrgTable
|
||||
-> OrgTableRow
|
||||
-> F OrgTable
|
||||
rowToContent OrgHlineRow t = maybeBodyToHeader t
|
||||
rowToContent (OrgAlignRow as) t = setAligns as t
|
||||
rowToContent (OrgContentRow rf) t = do
|
||||
rs <- rf
|
||||
appendToBody rs t
|
||||
|
||||
maybeBodyToHeader :: OrgTable
|
||||
-> F OrgTable
|
||||
maybeBodyToHeader t = case t of
|
||||
rowToContent orgTable row =
|
||||
case row of
|
||||
OrgHlineRow -> return singleRowPromotedToHeader
|
||||
OrgAlignRow as -> return . setAligns $ as
|
||||
OrgContentRow cs -> appendToBody cs
|
||||
where
|
||||
singleRowPromotedToHeader :: OrgTable
|
||||
singleRowPromotedToHeader = case orgTable of
|
||||
OrgTable{ orgTableHeader = [], orgTableRows = b:[] } ->
|
||||
return t{ orgTableHeader = b , orgTableRows = [] }
|
||||
_ -> return t
|
||||
orgTable{ orgTableHeader = b , orgTableRows = [] }
|
||||
_ -> orgTable
|
||||
|
||||
appendToBody :: [Blocks]
|
||||
-> OrgTable
|
||||
-> F OrgTable
|
||||
appendToBody r t = return t{ orgTableRows = orgTableRows t ++ [r] }
|
||||
setAligns :: [Alignment] -> OrgTable
|
||||
setAligns aligns = orgTable{ orgTableAlignments = aligns }
|
||||
|
||||
setAligns :: [Alignment]
|
||||
-> OrgTable
|
||||
-> F OrgTable
|
||||
setAligns aligns t = return $ t{ orgTableAlignments = aligns }
|
||||
appendToBody :: F [Blocks] -> F OrgTable
|
||||
appendToBody frow = do
|
||||
newRow <- frow
|
||||
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] }
|
||||
|
||||
|
||||
--
|
||||
|
|
Loading…
Reference in a new issue