git.iko.soy/typeable/pandoc
on master help sign in
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.

commita51e4e82156d8d6f0d3dc616c4f38c70b25be616 authorAlbert Krewinkel <albert@zeitkraut.de> dateWed 4 May 2016, 00:03 (10 years ago) parentd5e4bc1
1 file changed+25−25
src/Text/Pandoc/Readers/Org.hs+25−25
src/Text/Pandoc/Readers/Org.hs+25−25
@@ -844,7 +844,7 @@ tableHline = try $
844844
845845rowsToTable :: [OrgTableRow]
846846 -> F OrgTable
847−rowsToTable = foldM (flip rowToContent) emptyTable
847+rowsToTable = foldM rowToContent emptyTable
848848 where emptyTable = OrgTable mempty mempty mempty
849849
850850normalizeTable :: OrgTable -> OrgTable
@@ -859,31 +859,31 @@ normalizeTable (OrgTable aligns heads rows) = OrgTable aligns' heads rows
859859
860860-- One or more horizontal rules after the first content line mark the previous
861861-- line as a header. All other horizontal lines are discarded.
862−rowToContent :: OrgTableRow
863− -> OrgTable
862+rowToContent :: OrgTable
863+ -> OrgTableRow
864864 -> F OrgTable
865−rowToContent OrgHlineRow t = maybeBodyToHeader t
866−rowToContent (OrgAlignRow as) t = setAligns as t
867−rowToContent (OrgContentRow rf) t = do
868− rs <- rf
869− appendToBody rs t
870−
871−maybeBodyToHeader :: OrgTable
872− -> F OrgTable
873−maybeBodyToHeader t = case t of
874− OrgTable{ orgTableHeader = [], orgTableRows = b:[] } ->
875− return t{ orgTableHeader = b , orgTableRows = [] }
876− _ -> return t
877−
878−appendToBody :: [Blocks]
879− -> OrgTable
880− -> F OrgTable
881−appendToBody r t = return t{ orgTableRows = orgTableRows t ++ [r] }
882−
883−setAligns :: [Alignment]
884− -> OrgTable
885− -> F OrgTable
886−setAligns aligns t = return $ t{ orgTableAlignments = aligns }
865+rowToContent orgTable row =
866+ case row of
867+ OrgHlineRow -> return singleRowPromotedToHeader
868+ OrgAlignRow as -> return . setAligns $ as
869+ OrgContentRow cs -> appendToBody cs
870+ where
871+ singleRowPromotedToHeader :: OrgTable
872+ singleRowPromotedToHeader = case orgTable of
873+ OrgTable{ orgTableHeader = [], orgTableRows = b:[] } ->
874+ orgTable{ orgTableHeader = b , orgTableRows = [] }
875+ _ -> orgTable
876+
877+ setAligns :: [Alignment] -> OrgTable
878+ setAligns aligns = orgTable{ orgTableAlignments = aligns }
879+
880+ appendToBody :: F [Blocks] -> F OrgTable
881+ appendToBody frow = do
882+ newRow <- frow
883+ let oldRows = orgTableRows orgTable
884+ -- NOTE: This is an inefficient O(n) operation. This should be changed
885+ -- if performance ever becomes a problem.
886+ return orgTable{ orgTableRows = oldRows ++ [newRow] }
887887
888888
889889--