Skip to content

Commit 188b714

Browse files
jhillyerdNecoro
andauthored
Update tablewriter to 1.0 (#3)
* Make package work with updated version v1.0.0 of tablewriter. tablewriter has changed a lot of formatting behavior. This commit tries to emulate the old behavior as much as possible. It may not be complete though. * Move handling of all the pretty-table stuff into its own file. This declutters html2text.go * Add the possibility to bypass all the legacy tablewriter handling and do all the configuration directly. The place for the configuration function is slightly odd, but avoids larger refactoring and possible breaking changes for consumers. * Update deps Signed-off-by: James Hillyerd <james@hillyerd.com> --------- Signed-off-by: James Hillyerd <james@hillyerd.com> Co-authored-by: René 'Necoro' Neumann <necoro@necoro.eu>
1 parent 1f4e356 commit 188b714

File tree

5 files changed

+303
-71
lines changed

5 files changed

+303
-71
lines changed

go.mod

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ module github.com/inbucket/html2text
33
go 1.24.3
44

55
require (
6-
github.com/olekukonko/tablewriter v0.0.5
6+
github.com/olekukonko/tablewriter v1.0.7
77
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf
88
golang.org/x/net v0.41.0
99
)
1010

1111
require (
12+
github.com/fatih/color v1.18.0 // indirect
13+
github.com/mattn/go-colorable v0.1.14 // indirect
14+
github.com/mattn/go-isatty v0.0.20 // indirect
1215
github.com/mattn/go-runewidth v0.0.16 // indirect
13-
github.com/rivo/uniseg v0.2.0 // indirect
16+
github.com/olekukonko/errors v1.1.0 // indirect
17+
github.com/olekukonko/ll v0.0.9 // indirect
18+
github.com/rivo/uniseg v0.4.7 // indirect
19+
golang.org/x/sys v0.33.0 // indirect
1420
)

go.sum

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
1+
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
2+
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
3+
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
4+
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
5+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
6+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
27
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
38
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
4-
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
5-
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
6-
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
9+
github.com/olekukonko/errors v1.1.0 h1:RNuGIh15QdDenh+hNvKrJkmxxjV4hcS50Db478Ou5sM=
10+
github.com/olekukonko/errors v1.1.0/go.mod h1:ppzxA5jBKcO1vIpCXQ9ZqgDh8iwODz6OXIGKU8r5m4Y=
11+
github.com/olekukonko/ll v0.0.9 h1:Y+1YqDfVkqMWuEQMclsF9HUR5+a82+dxJuL1HHSRpxI=
12+
github.com/olekukonko/ll v0.0.9/go.mod h1:En+sEW0JNETl26+K8eZ6/W4UQ7CYSrrgg/EdIYT2H8g=
13+
github.com/olekukonko/tablewriter v1.0.7 h1:HCC2e3MM+2g72M81ZcJU11uciw6z/p82aEnm4/ySDGw=
14+
github.com/olekukonko/tablewriter v1.0.7/go.mod h1:H428M+HzoUXC6JU2Abj9IT9ooRmdq9CxuDmKMtrOCMs=
715
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
16+
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
17+
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
818
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf h1:pvbZ0lM0XWPBqUKqFU8cmavspvIl9nulOYwdy6IFRRo=
919
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf/go.mod h1:RJID2RhlZKId02nZ62WenDCkgHFerpIOmW0iT7GKmXM=
1020
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
1121
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
22+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
23+
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
24+
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=

html2text.go

Lines changed: 15 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -21,55 +21,18 @@ type Options struct {
2121
TextOnly bool // Returns only plain text
2222
}
2323

24-
// PrettyTablesOptions overrides tablewriter behaviors
25-
type PrettyTablesOptions struct {
26-
AutoFormatHeader bool
27-
AutoWrapText bool
28-
ReflowDuringAutoWrap bool
29-
ColWidth int
30-
ColumnSeparator string
31-
RowSeparator string
32-
CenterSeparator string
33-
HeaderAlignment int
34-
FooterAlignment int
35-
Alignment int
36-
ColumnAlignment []int
37-
NewLine string
38-
HeaderLine bool
39-
RowLine bool
40-
AutoMergeCells bool
41-
Borders tablewriter.Border
42-
}
43-
44-
// NewPrettyTablesOptions creates PrettyTablesOptions with default settings
45-
func NewPrettyTablesOptions() *PrettyTablesOptions {
46-
return &PrettyTablesOptions{
47-
AutoFormatHeader: true,
48-
AutoWrapText: true,
49-
ReflowDuringAutoWrap: true,
50-
ColWidth: tablewriter.MAX_ROW_WIDTH,
51-
ColumnSeparator: tablewriter.COLUMN,
52-
RowSeparator: tablewriter.ROW,
53-
CenterSeparator: tablewriter.CENTER,
54-
HeaderAlignment: tablewriter.ALIGN_DEFAULT,
55-
FooterAlignment: tablewriter.ALIGN_DEFAULT,
56-
Alignment: tablewriter.ALIGN_DEFAULT,
57-
ColumnAlignment: []int{},
58-
NewLine: tablewriter.NEWLINE,
59-
HeaderLine: true,
60-
RowLine: false,
61-
AutoMergeCells: false,
62-
Borders: tablewriter.Border{Left: true, Right: true, Bottom: true, Top: true},
63-
}
64-
}
65-
6624
// FromHTMLNode renders text output from a pre-parsed HTML document.
6725
func FromHTMLNode(doc *html.Node, o ...Options) (string, error) {
6826
var options Options
6927
if len(o) > 0 {
7028
options = o[0]
7129
}
7230

31+
if options.PrettyTables && options.PrettyTablesOptions == nil {
32+
// defaults need to make explicit as they are no longer identical with tablewriter
33+
options.PrettyTablesOptions = NewPrettyTablesOptions()
34+
}
35+
7336
ctx := textifyTraverseContext{
7437
buf: bytes.Buffer{},
7538
options: options,
@@ -333,31 +296,18 @@ func (ctx *textifyTraverseContext) handleTableElement(node *html.Node) error {
333296

334297
buf := &bytes.Buffer{}
335298
table := tablewriter.NewWriter(buf)
336-
if ctx.options.PrettyTablesOptions != nil {
337-
options := ctx.options.PrettyTablesOptions
338-
table.SetAutoFormatHeaders(options.AutoFormatHeader)
339-
table.SetAutoWrapText(options.AutoWrapText)
340-
table.SetReflowDuringAutoWrap(options.ReflowDuringAutoWrap)
341-
table.SetColWidth(options.ColWidth)
342-
table.SetColumnSeparator(options.ColumnSeparator)
343-
table.SetRowSeparator(options.RowSeparator)
344-
table.SetCenterSeparator(options.CenterSeparator)
345-
table.SetHeaderAlignment(options.HeaderAlignment)
346-
table.SetFooterAlignment(options.FooterAlignment)
347-
table.SetAlignment(options.Alignment)
348-
table.SetColumnAlignment(options.ColumnAlignment)
349-
table.SetNewLine(options.NewLine)
350-
table.SetHeaderLine(options.HeaderLine)
351-
table.SetRowLine(options.RowLine)
352-
table.SetAutoMergeCells(options.AutoMergeCells)
353-
table.SetBorders(options.Borders)
354-
}
355-
table.SetHeader(ctx.tableCtx.header)
356-
table.SetFooter(ctx.tableCtx.footer)
357-
table.AppendBulk(ctx.tableCtx.body)
299+
ctx.options.PrettyTablesOptions.configureTable(table)
300+
301+
table.Header(ctx.tableCtx.header)
302+
table.Footer(ctx.tableCtx.footer)
303+
if err := table.Bulk(ctx.tableCtx.body); err != nil {
304+
return err
305+
}
358306

359307
// Render the table using ASCII.
360-
table.Render()
308+
if err := table.Render(); err != nil {
309+
return err
310+
}
361311
if err := ctx.emit(buf.String()); err != nil {
362312
return err
363313
}

html2text_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
"regexp"
1010
"strings"
1111
"testing"
12+
13+
"github.com/olekukonko/tablewriter"
14+
"github.com/olekukonko/tablewriter/tw"
1215
)
1316

1417
const destPath = "testdata"
@@ -353,6 +356,54 @@ Table 2 Header 1 Table 2 Header 2 Table 2 Footer 1 Table 2 Footer 2 Table 2 Row
353356
}
354357
}
355358

359+
func TestTablesWithDirectConfiguration(t *testing.T) {
360+
testCases := []struct {
361+
input string
362+
tabularOutput string
363+
}{
364+
{
365+
`<table>
366+
<thead>
367+
<tr><th>Header 1</th><th>Header 2</th></tr>
368+
</thead>
369+
<tfoot>
370+
<tr><td>Footer 1</td><td>Footer 2</td></tr>
371+
</tfoot>
372+
<tbody>
373+
<tr><td>Row 1 Col 1</td><td>Row 1 Col 2</td></tr>
374+
<tr><td>Row 2 Col 1</td><td>Row 2 Col 2</td></tr>
375+
</tbody>
376+
</table>`,
377+
`┌─────────────┬─────────────┐
378+
│ HEADER 1 │ HEADER 2 │
379+
├─────────────┼─────────────┤
380+
│ Row 1 Col 1 │ Row 1 Col 2 │
381+
│ Row 2 Col 1 │ Row 2 Col 2 │
382+
└─────────────┴─────────────┘`,
383+
},
384+
}
385+
386+
for _, testCase := range testCases {
387+
options := Options{
388+
PrettyTables: true,
389+
PrettyTablesOptions: &PrettyTablesOptions{
390+
Configuration: func(table *tablewriter.Table) {
391+
table.Options(
392+
tablewriter.WithHeaderAlignment(tw.AlignRight),
393+
tablewriter.WithFooterControl(tw.Control{Hide: tw.On}),
394+
)
395+
},
396+
},
397+
}
398+
// Check pretty tabular ASCII version.
399+
if msg, err := wantString(testCase.input, testCase.tabularOutput, options); err != nil {
400+
t.Error(err)
401+
} else if len(msg) > 0 {
402+
t.Log(msg)
403+
}
404+
}
405+
}
406+
356407
func TestStrippingLists(t *testing.T) {
357408
testCases := []struct {
358409
input string

0 commit comments

Comments
 (0)