git.iko.soy/typeable/pandoc
on master help sign in
Org reader: stop padding short table rows

Emacs Org-mode doesn't add any padding to table rows. The first row (header or first body row) is used to determine the column count, no other magic is performed. The org reader was padding rows to the length of the longest table row. This was done due to a misunderstanding of how Org handles tables. This feature reflected how Org-mode handles tables when pressing <TAB>. The Org exporter however, which is what the reader should implement, doesn't do any of this. So this was a mis-feature that made the reader more complex and reduced comparability. It was hence removed.

commitd5e4bc179c0517a5da84ed95d55e1f10f1f16a94 authorAlbert Krewinkel <albert@zeitkraut.de> dateTue 3 May 2016, 22:52 (11 years ago) parentcc9a9c7
2 files changed+24−28
src/Text/Pandoc/Readers/Org.hs+20−24 tests/Tests/Readers/Org.hs+4−4
src/Text/Pandoc/Readers/Org.hs+20−24
@@ -774,9 +774,13 @@ data OrgTableRow = OrgContentRow (F [Blocks])
774774 | OrgAlignRow [Alignment]
775775 | OrgHlineRow
776776
777+-- OrgTable is strongly related to the pandoc table ADT. Using the same
778+-- (i.e. pandoc-global) ADT would mean that the reader would break if the
779+-- global structure was to be changed, which would be bad. The final table
780+-- should be generated using a builder function. Column widths aren't
781+-- implemented yet, so they are not tracked here.
777782data OrgTable = OrgTable
778− { orgTableColumns :: Int
779− , orgTableAlignments :: [Alignment]
783+ { orgTableAlignments :: [Alignment]
780784 , orgTableHeader :: [Blocks]
781785 , orgTableRows :: [[Blocks]]
782786 }
@@ -792,7 +796,7 @@ table = try $ do
792796orgToPandocTable :: OrgTable
793797 -> Inlines
794798 -> Blocks
795−orgToPandocTable (OrgTable _ aligns heads lns) caption =
799+orgToPandocTable (OrgTable aligns heads lns) caption =
796800 B.table caption (zip aligns $ repeat 0) heads lns
797801
798802tableStart :: OrgParser Char
@@ -840,20 +844,18 @@ tableHline = try $
840844
841845rowsToTable :: [OrgTableRow]
842846 -> F OrgTable
843−rowsToTable = foldM (flip rowToContent) zeroTable
844− where zeroTable = OrgTable 0 mempty mempty mempty
845−
846−normalizeTable :: OrgTable
847− -> OrgTable
848−normalizeTable (OrgTable cols aligns heads lns) =
849− let aligns' = fillColumns aligns AlignDefault
850− heads' = if heads == mempty
851− then mempty
852− else fillColumns heads (B.plain mempty)
853− lns' = map (`fillColumns` B.plain mempty) lns
854− fillColumns base padding = take cols $ base ++ repeat padding
855− in OrgTable cols aligns' heads' lns'
847+rowsToTable = foldM (flip rowToContent) emptyTable
848+ where emptyTable = OrgTable mempty mempty mempty
856849
850+normalizeTable :: OrgTable -> OrgTable
851+normalizeTable (OrgTable aligns heads rows) = OrgTable aligns' heads rows
852+ where
853+ refRow = if heads /= mempty
854+ then heads
855+ else if rows == mempty then mempty else head rows
856+ cols = length refRow
857+ fillColumns base padding = take cols $ base ++ repeat padding
858+ aligns' = fillColumns aligns AlignDefault
857859
858860-- One or more horizontal rules after the first content line mark the previous
859861-- line as a header. All other horizontal lines are discarded.
@@ -861,16 +863,10 @@ rowToContent :: OrgTableRow
861863 -> OrgTable
862864 -> F OrgTable
863865rowToContent OrgHlineRow t = maybeBodyToHeader t
864−rowToContent (OrgAlignRow as) t = setLongestRow as =<< setAligns as t
866+rowToContent (OrgAlignRow as) t = setAligns as t
865867rowToContent (OrgContentRow rf) t = do
866868 rs <- rf
867− setLongestRow rs =<< appendToBody rs t
868−
869−setLongestRow :: [a]
870− -> OrgTable
871− -> F OrgTable
872−setLongestRow rs t =
873− return t{ orgTableColumns = max (length rs) (orgTableColumns t) }
869+ appendToBody rs t
874870
875871maybeBodyToHeader :: OrgTable
876872 -> F OrgTable
tests/Tests/Readers/Org.hs+4−4
@@ -1024,10 +1024,10 @@ tests =
10241024 , "| 1 | One | foo |"
10251025 , "| 2"
10261026 ] =?>
1027− table "" (zip [AlignCenter, AlignRight, AlignDefault] [0, 0, 0])
1028− [ plain "Numbers", plain "Text" , plain mempty ]
1029− [ [ plain "1" , plain "One" , plain "foo" ]
1030− , [ plain "2" , plain mempty , plain mempty ]
1027+ table "" (zip [AlignCenter, AlignRight] [0, 0])
1028+ [ plain "Numbers", plain "Text" ]
1029+ [ [ plain "1" , plain "One" , plain "foo" ]
1030+ , [ plain "2" ]
10311031 ]
10321032
10331033 , "Table with caption" =: