@@ -899,15 +899,37 @@ public struct MarkupFormatter: MarkupWalker {
899899 $0. formatIndependently ( options: cellFormattingOptions)
900900 } ) . ensuringCount ( atLeast: uniformColumnCount, filler: " " )
901901
902+ /// All of the column-span values from the head cells, adding cells as
903+ /// needed to meet the uniform `uniformColumnCount`.
904+ let headCellSpans = Array ( table. head. cells. map {
905+ $0. colspan
906+ } ) . ensuringCount ( atLeast: uniformColumnCount, filler: 1 )
907+
902908 /// All of the independently formatted body cells' text by row, adding
903909 /// cells to each row to meet the `uniformColumnCount`.
904910 let bodyRowTexts = Array ( table. body. rows. map { row -> [ String ] in
905911 return Array ( row. cells. map {
906- $0. formatIndependently ( options: cellFormattingOptions)
912+ if $0. rowspan == 0 {
913+ // If this cell is being spanned over, replace its text
914+ // (which should be the empty string anyway) with the
915+ // rowspan marker.
916+ return " ^ "
917+ } else {
918+ return $0. formatIndependently ( options: cellFormattingOptions)
919+ }
907920 } ) . ensuringCount ( atLeast: uniformColumnCount,
908921 filler: " " )
909922 } )
910923
924+ /// All of the column- and row-span information for the body cells,
925+ /// cells to each row to meet the `uniformColumnCount`.
926+ let bodyRowSpans = Array ( table. body. rows. map { row in
927+ return Array ( row. cells. map {
928+ ( colspan: $0. colspan, rowspan: $0. rowspan)
929+ } ) . ensuringCount ( atLeast: uniformColumnCount,
930+ filler: ( colspan: 1 , rowspan: 1 ) )
931+ } )
932+
911933 // Next, calculate the maximum width of each column.
912934
913935 /// The column alignments of the table, filled out to `uniformColumnCount`.
@@ -952,15 +974,32 @@ public struct MarkupFormatter: MarkupWalker {
952974 }
953975 }
954976
977+ /// Calculate the width of the given column and colspan.
978+ ///
979+ /// This adds up the appropriate column widths based on the given column span, including
980+ /// the default span of 1, where it will only return the `finalColumnWidths` value for the
981+ /// given `column`.
982+ func columnWidth( column: Int , colspan: Int ) -> Int {
983+ let lastColumn = column + colspan
984+ return ( column..< lastColumn) . map ( { finalColumnWidths [ $0] } ) . reduce ( 0 , { $0 + $1 } )
985+ }
986+
955987 // We now know the width that each printed column will be.
956988
957989 /// Each of the header cells expanded to the right dimensions by
958990 /// extending each line with spaces to fit the uniform column width.
959991 let expandedHeaderCellTexts = ( 0 ..< uniformColumnCount)
960992 . map { column -> String in
961- let minLineLength = finalColumnWidths [ column]
962- return headCellTexts [ column]
963- . ensuringCount ( atLeast: minLineLength, filler: " " )
993+ let colspan = headCellSpans [ column]
994+ if colspan == 0 {
995+ // If this cell is being spanned over, collapse it so it
996+ // can be filled with the spanning cell.
997+ return " "
998+ } else {
999+ let minLineLength = columnWidth ( column: column, colspan: Int ( colspan) )
1000+ return headCellTexts [ column]
1001+ . ensuringCount ( atLeast: minLineLength, filler: " " )
1002+ }
9641003 }
9651004
9661005 /// Rendered delimter row cells with the correct width.
@@ -988,10 +1027,18 @@ public struct MarkupFormatter: MarkupWalker {
9881027 /// appropriately for their row and column.
9891028 let expandedBodyRowTexts = bodyRowTexts. enumerated ( )
9901029 . map { ( row, rowCellTexts) -> [ String ] in
1030+ let rowSpans = bodyRowSpans [ row]
9911031 return ( 0 ..< uniformColumnCount) . map { column -> String in
992- let minLineLength = finalColumnWidths [ column]
993- return rowCellTexts [ column]
994- . ensuringCount ( atLeast: minLineLength, filler: " " )
1032+ let colspan = rowSpans [ column] . colspan
1033+ if colspan == 0 {
1034+ // If this cell is being spanned over, collapse it so it
1035+ // can be filled with the spanning cell.
1036+ return " "
1037+ } else {
1038+ let minLineLength = columnWidth ( column: column, colspan: Int ( colspan) )
1039+ return rowCellTexts [ column]
1040+ . ensuringCount ( atLeast: minLineLength, filler: " " )
1041+ }
9951042 }
9961043 }
9971044
0 commit comments