Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding functionality to render a table from a HTML file #208

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/olekukonko/tablewriter

go 1.12

require github.com/mattn/go-runewidth v0.0.10
require (
github.com/mattn/go-runewidth v0.0.10
golang.org/x/net v0.0.0-20221004154528-8021a29435af
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@ github.com/mattn/go-runewidth v0.0.10 h1:CoZ3S2P7pvtP45xOtBw+/mDL2z0RKI576gSkzRR
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/rivo/uniseg v0.1.0 h1:+2KBaVoUmb9XzDsrx/Ct0W/EYOSFf/nWTauy++DprtY=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
golang.org/x/net v0.0.0-20221004154528-8021a29435af h1:wv66FM3rLZGPdxpYL+ApnDe2HzHcTFta3z5nsc13wI4=
golang.org/x/net v0.0.0-20221004154528-8021a29435af/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
56 changes: 56 additions & 0 deletions html.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package tablewriter

import (
"io"
"os"
"golang.org/x/net/html"
)

func NewHTML(writer io.Writer, fileName string, columnNum int) (*Table, error) {
file, err := os.Open(fileName)

if err != nil {
return &Table{}, err
}
defer file.Close()

htmlParser := html.NewTokenizer(file)

var isTh bool
var isTd bool
var depth int
var headings []string
var cellData []string
table := NewWriter(writer)

for {
tt := htmlParser.Next()
switch {
case tt == html.ErrorToken:
return table, nil
case tt == html.StartTagToken:
t := htmlParser.Token()
isTd = t.Data == "td"
isTh = t.Data == "th"
case tt == html.TextToken:
t := htmlParser.Token()
if isTd {
cellData = append(cellData, t.Data)
depth++
}
if isTh {
headings = append(headings, t.Data)
depth++
}
if isTd && depth % columnNum == 0 {
table.Append(cellData)
cellData = nil
}
if isTh && depth % columnNum == 0 {
table.SetHeader(headings)
}
isTd = false
isTh = false
}
}
}
103 changes: 103 additions & 0 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,32 @@ func ExampleShort() {
// +------+-----------------------+--------+
}

func ExampleWithArray() {
data := [][]string{
{"A", "The Good", "500"},
{"B", "The Very very Bad Man", "288"},
{"C", "The Ugly", "120"},
{"D", "The Gopher", "800"},
}

table := NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Sign", "Rating"})

for _, v := range data {
table.Append(v)
}
table.Render()

// Output: +------+-----------------------+--------+
// | NAME | SIGN | RATING |
// +------+-----------------------+--------+
// | A | The Good | 500 |
// | B | The Very very Bad Man | 288 |
// | C | The Ugly | 120 |
// | D | The Gopher | 800 |
// +------+-----------------------+--------+
}

func ExampleTable() {
data := [][]string{
{"Learn East has computers with adapted keyboards with enlarged print etc", " Some Data ", " Another Data"},
Expand Down Expand Up @@ -98,6 +124,83 @@ func ExampleNewCSV() {
// *============*===========*=========*
}

func ExampleNewHTMLWithHeadings() {
table, _ := NewHTML(os.Stdout, "testdata/test1.html", 4)
table.SetCenterSeparator("*")
table.SetRowSeparator("=")

table.Render()

// Output: *=====*========*=====*================================*
// | S/N | NAME | AGE | HOBBIES |
// *=====*========*=====*================================*
// | 1 | Sydney | 20 | Coding, Partying and trolling |
// | | | | on Twitter |
// | 2 | Harith | 21 | Coding, Praying and learning |
// | | | | new things |
// | 3 | Iyanu | 20 | Coding, Arguing and listening |
// | | | | to weird Music |
// | 4 | Seun | 20 | Watching movie and making |
// | | | | money |
// *=====*========*=====*================================*
}

func ExampleNewHTMLWithoutHeadings() {
table, _ := NewHTML(os.Stdout, "testdata/test3.html", 4)
table.SetCenterSeparator("*")
table.SetRowSeparator("=")

table.Render()

//Output: *===*========*====*================================*
// | 1 | Sydney | 20 | Coding, Partying and trolling |
// | | | | on Twitter |
// | 2 | Harith | 21 | Coding, Praying and learning |
// | | | | new things |
// | 3 | Iyanu | 20 | Coding, Arguing and listening |
// | | | | to weird Music |
// | 4 | Seun | 20 | Watching movie and making |
// | | | | money |
// *===*========*====*================================*
}

func ExampleNewMalHTML() {
// html has some unclosed tags
table, _ := NewHTML(os.Stdout, "testdata/test2.html", 4)
table.SetCenterSeparator("*")
table.SetRowSeparator("=")

table.Render()

// Output: *==========*==========*==========*================================*
// | S/N | NAME | AGE | HOBBIES |
// | | | | |
// *==========*==========*==========*================================*
// | 1 | Sydney | 20 | Coding, Partying and trolling |
// | | | | on Twitter |
// | 2 | Harith | 21 | Coding, Praying and learning |
// | | | | new things |
// | 3 | Iyanu | 20 | Coding, Arguing and listening |
// | | | | to weird Music |
// | 4 | Seun | 20 | Watching movie and making |
// | | | | money |
// *==========*==========*==========*================================*
}

func ExampleNewMalHTML2() {
table, _ := NewHTML(os.Stdout, "testdata/test4.html", 1)
table.SetCenterSeparator("*")
table.SetRowSeparator("=")

table.Render()

// Output: *=====*
// | BAR |
// *=====*
// | foo |
// *=====*
}

func ExampleTable_SetUnicodeHV() {
data := [][]string{
{"Regular", "regular line", "1"},
Expand Down
32 changes: 32 additions & 0 deletions testdata/test1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<table>
<tr>
<th>S/N</th>
<th>Name</th>
<th>Age</th>
<th>Hobbies</th>
</tr>
<tr>
<td>1</td>
<td>Sydney</td>
<td>20</td>
<td>Coding, Partying and trolling on Twitter</td>
</tr>
<tr>
<td>2</td>
<td>Harith</td>
<td>21</td>
<td>Coding, Praying and learning new things</td>
</tr>
<tr>
<td>3</td>
<td>Iyanu</td>
<td>20</td>
<td>Coding, Arguing and listening to weird Music</td>
</tr>
<tr>
<td>4</td>
<td>Seun</td>
<td>20</td>
<td>Watching movie and making money</td>
</tr>
</table>
27 changes: 27 additions & 0 deletions testdata/test2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<table>
<tr>
<th>S/N
<th>Name
<th>Age
<th>Hobbies
<tr>
<td>1
<td>Sydney
<td>20
<td>Coding, Partying and trolling on Twitter
<tr>
<td>2</td>
<td>Harith</td>
<td>21</td>
<td>Coding, Praying and learning new things</td>
<tr>
<td>3</td>
<td>Iyanu</td>
<td>20</td>
<td>Coding, Arguing and listening to weird Music</td>
<tr>
<td>4</td>
<td>Seun</td>
<td>20</td>
<td>Watching movie and making money</td>
</table>
26 changes: 26 additions & 0 deletions testdata/test3.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<table>
<tr>
<td>1</td>
<td>Sydney</td>
<td>20</td>
<td>Coding, Partying and trolling on Twitter</td>
</tr>
<tr>
<td>2</td>
<td>Harith</td>
<td>21</td>
<td>Coding, Praying and learning new things</td>
</tr>
<tr>
<td>3</td>
<td>Iyanu</td>
<td>20</td>
<td>Coding, Arguing and listening to weird Music</td>
</tr>
<tr>
<td>4</td>
<td>Seun</td>
<td>20</td>
<td>Watching movie and making money</td>
</tr>
</table>
7 changes: 7 additions & 0 deletions testdata/test4.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<tr>
<td>foo</td>
</tr>
<tr>
<th>bar</th>
</tr>