git.iko.soy/typeable/pandoc
on master help sign in
Org reader: stop padding short table rows
d5e4bc1Albert Krewinkel
src/Text/Pandoc/Readers/Org.hs+20−24
@@ -774,9 +774,13 @@ data OrgTableRow = OrgContentRow (F [Blocks])
774 | OrgAlignRow [Alignment]
774 | OrgAlignRow [Alignment]
775 | OrgHlineRow
775 | OrgHlineRow
776
776
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.
777data OrgTable = OrgTable
782data OrgTable = OrgTable
778− { orgTableColumns :: Int
779− , orgTableAlignments :: [Alignment]
783+ { orgTableAlignments :: [Alignment]
780 , orgTableHeader :: [Blocks]
784 , orgTableHeader :: [Blocks]
781 , orgTableRows :: [[Blocks]]
785 , orgTableRows :: [[Blocks]]
782 }
786 }
@@ -792,7 +796,7 @@ table = try $ do
792orgToPandocTable :: OrgTable
796orgToPandocTable :: OrgTable
793 -> Inlines
797 -> Inlines
794 -> Blocks
798 -> Blocks
795−orgToPandocTable (OrgTable _ aligns heads lns) caption =
799+orgToPandocTable (OrgTable aligns heads lns) caption =
796 B.table caption (zip aligns $ repeat 0) heads lns
800 B.table caption (zip aligns $ repeat 0) heads lns
797
801
798tableStart :: OrgParser Char
802tableStart :: OrgParser Char
@@ -840,20 +844,18 @@ tableHline = try $
840
844
841rowsToTable :: [OrgTableRow]
845rowsToTable :: [OrgTableRow]
842 -> F OrgTable
846 -> F OrgTable
843−rowsToTable = foldM (flip rowToContent) zeroTable
847+rowsToTable = foldM (flip rowToContent) emptyTable
844− where zeroTable = OrgTable 0 mempty mempty mempty
848+ where emptyTable = OrgTable 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'
856
849
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
857
859
858-- One or more horizontal rules after the first content line mark the previous
860-- One or more horizontal rules after the first content line mark the previous
859-- line as a header. All other horizontal lines are discarded.
861-- line as a header. All other horizontal lines are discarded.
@@ -861,16 +863,10 @@ rowToContent :: OrgTableRow
861 -> OrgTable
863 -> OrgTable
862 -> F OrgTable
864 -> F OrgTable
863rowToContent OrgHlineRow t = maybeBodyToHeader t
865rowToContent OrgHlineRow t = maybeBodyToHeader t
864−rowToContent (OrgAlignRow as) t = setLongestRow as =<< setAligns as t
866+rowToContent (OrgAlignRow as) t = setAligns as t
865rowToContent (OrgContentRow rf) t = do
867rowToContent (OrgContentRow rf) t = do
866 rs <- rf
868 rs <- rf
867− setLongestRow rs =<< appendToBody rs t
869+ 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) }
874
870
875maybeBodyToHeader :: OrgTable
871maybeBodyToHeader :: OrgTable
876 -> F OrgTable
872 -> F OrgTable
tests/Tests/Readers/Org.hs+4−4
@@ -1024,10 +1024,10 @@ tests =
1024 , "| 1 | One | foo |"
1024 , "| 1 | One | foo |"
1025 , "| 2"
1025 , "| 2"
1026 ] =?>
1026 ] =?>
1027− table "" (zip [AlignCenter, AlignRight, AlignDefault] [0, 0, 0])
1027+ table "" (zip [AlignCenter, AlignRight] [0, 0])
1028− [ plain "Numbers", plain "Text" , plain mempty ]
1028+ [ plain "Numbers", plain "Text" ]
1029− [ [ plain "1" , plain "One" , plain "foo" ]
1029+ [ [ plain "1" , plain "One" , plain "foo" ]
1030− , [ plain "2" , plain mempty , plain mempty ]
1030+ , [ plain "2" ]
1031 ]
1031 ]
1032
1032
1033 , "Table with caption" =:
1033 , "Table with caption" =: