44 "fmt"
55 "html"
66 "strings"
7+
8+ "github.com/jedib0t/go-pretty/v6/text"
79)
810
911const (
@@ -113,9 +115,9 @@ func (t *Table) htmlRenderColumn(out *strings.Builder, colStr string) {
113115 out .WriteString (colStr )
114116}
115117
116- func (t * Table ) htmlRenderColumnAttributes (out * strings.Builder , colIdx int , hint renderHint ) {
118+ func (t * Table ) htmlRenderColumnAttributes (out * strings.Builder , colIdx int , hint renderHint , alignOverride text. Align ) {
117119 // determine the HTML "align"/"valign" property values
118- align := t . getAlign ( colIdx , hint ) .HTMLProperty ()
120+ align := alignOverride .HTMLProperty ()
119121 vAlign := t .getVAlign (colIdx , hint ).HTMLProperty ()
120122 // determine the HTML "class" property values for the colors
121123 class := t .getColumnColors (colIdx , hint ).HTMLProperty ()
@@ -158,11 +160,31 @@ func (t *Table) htmlRenderRow(out *strings.Builder, row rowStr, hint renderHint)
158160 t .htmlRenderColumnAutoIndex (out , hint )
159161 }
160162
163+ align := t .getAlign (colIdx , hint )
164+ rowConfig := t .getRowConfig (hint )
165+ extraColumnsRendered := 0
166+ if rowConfig .AutoMerge && ! hint .isSeparatorRow {
167+ // get the real row to consider all lines in each column instead of just
168+ // looking at the current "line"
169+ rowUnwrapped := t .getRow (hint .rowNumber - 1 , hint )
170+ for idx := colIdx + 1 ; idx < len (rowUnwrapped ); idx ++ {
171+ if rowUnwrapped [colIdx ] != rowUnwrapped [idx ] {
172+ break
173+ }
174+ align = rowConfig .getAutoMergeAlign ()
175+ extraColumnsRendered ++
176+ }
177+ }
178+
161179 colStr , colTagName := t .htmlGetColStrAndTag (row , colIdx , hint )
162180 // write the row
163181 out .WriteString (" <" )
164182 out .WriteString (colTagName )
165- t .htmlRenderColumnAttributes (out , colIdx , hint )
183+ t .htmlRenderColumnAttributes (out , colIdx , hint , align )
184+ if extraColumnsRendered > 0 {
185+ out .WriteString (" colspan=" )
186+ out .WriteString (fmt .Sprint (extraColumnsRendered + 1 ))
187+ }
166188 out .WriteString (">" )
167189 if len (colStr ) == 0 {
168190 out .WriteString (t .style .HTML .EmptyColumn )
@@ -172,6 +194,7 @@ func (t *Table) htmlRenderRow(out *strings.Builder, row rowStr, hint renderHint)
172194 out .WriteString ("</" )
173195 out .WriteString (colTagName )
174196 out .WriteString (">\n " )
197+ colIdx += extraColumnsRendered
175198 }
176199 out .WriteString (" </tr>\n " )
177200}
0 commit comments