Skip to content

go-xlan/go-webpage

Repository files navigation

GitHub Workflow Status (branch) GoDoc Coverage Status Supported Go Versions GitHub Release Go Report Card

go-webpage

Convert Go struct slice to HTML table with Firefox/w3m output.


CHINESE README

中文说明

Main Features

🎯 Struct to HTML Table: Convert Go struct slices to formatted HTML tables with custom tag support ⚡ HTML Output: Open generated HTML in Firefox; display via w3m console 🔄 HTTP Test Host: Built-in HTTP test service supporting rapid webpage development and testing 🌍 Flexible Options: Customizable table headers, tag names, and rendering configurations 📋 XSS Protection: HTML escaping built-in to prevent XSS vulnerabilities

Installation

go get github.com/go-xlan/go-webpage

Quick Start

Basic Table Generation

package main

import (
	"fmt"
	"github.com/go-xlan/go-webpage/slice2table"
)

type Account struct {
	Name string `table:"姓名"`
	Age  int    `table:"年龄"`
}

func main() {
	accounts := []*Account{
		{Name: "Alice", Age: 25},
		{Name: "Bob", Age: 30},
	}

	page := slice2table.NewTable(accounts)
	fmt.Println(page)
}

Firefox Output

package main

import (
	"fmt"
	"time"

	"github.com/brianvoe/gofakeit/v7"
	"github.com/go-xlan/go-webpage/firefoxview"
	"github.com/go-xlan/go-webpage/slice2table"
	"github.com/yyle88/must"
)

type AdminInfo struct {
	Name string
	From string
}

type GuestInfo struct {
	Name string
	From string
}

func main() {
	admins := []*AdminInfo{
		newAdminInfo(),
		newAdminInfo(),
		newAdminInfo(),
	}
	page1 := slice2table.NewTable(admins)

	guests := []*GuestInfo{
		newGuestInfo(),
		newGuestInfo(),
		newGuestInfo(),
	}
	page2 := slice2table.NewTable(guests)

	pane := firefoxview.NewPane()
	defer pane.Close(time.Minute)
	pane.ShowInNewTabs(page1, page2)
	fmt.Println("done")
}

func newAdminInfo() *AdminInfo {
	admin := &AdminInfo{}
	must.Done(gofakeit.Struct(admin))
	return admin
}

func newGuestInfo() *GuestInfo {
	guest := &GuestInfo{}
	must.Done(gofakeit.Struct(guest))
	return guest
}

⬆️ Source: Source

Console Output

package main

import (
	"fmt"
	"time"

	"github.com/brianvoe/gofakeit/v7"
	"github.com/go-xlan/go-webpage/slice2table"
	"github.com/go-xlan/go-webpage/w3mview"
	"github.com/yyle88/must"
	"github.com/yyle88/osexec"
)

type Account struct {
	ID        string    // 账户 ID
	Username  string    // 用户名
	Mailbox   string    // 邮箱
	Role      string    // 角色(如 admin, guest, student)
	IsActive  bool      // 是否激活
	CreatedAt time.Time // 创建时间
}

type Invoice struct {
	ID        string    // 账单 ID
	AccountID string    // 账户 ID
	Amount    float64   // 金额
	Status    string    // 状态(如 pending, shipped)
	IssuedAt  time.Time // 签发时间
}

func main() {
	accounts := []*Account{
		newAccount(),
		newAccount(),
		newAccount(),
	}
	page1 := slice2table.NewTable(accounts)

	invoices := []*Invoice{
		newInvoice(),
		newInvoice(),
		newInvoice(),
	}
	page2 := slice2table.NewTable(invoices)

	execConfig := osexec.NewOsCommand().WithDebug()
	w3mview.Show(execConfig, page1)
	w3mview.Show(execConfig, page2)
	fmt.Println("done")
}

func newAccount() *Account {
	account := &Account{}
	must.Done(gofakeit.Struct(account))
	return account
}

func newInvoice() *Invoice {
	invoice := &Invoice{}
	must.Done(gofakeit.Struct(invoice))
	return invoice
}

⬆️ Source: Source

Advanced Usage

Custom Tag Names

type Student struct {
	Name string `custom:"Student Name"`
	Grade int   `custom:"Grade Rank"`
}

students := []*Student{
	{Name: "John", Grade: 95},
}

options := slice2table.NewOptions().WithTagName("custom")
table := slice2table.GenTable(students, options)

Single Struct Table

import "github.com/go-xlan/go-webpage/slice2table/struct2table"

account := &Account{Name: "Alice", Age: 25}
table := struct2table.NewTable(account)

API Reference

slice2table Package

  • NewTable[T any](objects []*T) string - Convert struct slice to HTML table
  • GenTable[T any](objects []*T, options *Options) string - Generate table with custom options
  • NewOptions() *Options - Create default options
  • (opts *Options) WithTagName(tagName string) *Options - Set custom tag name

struct2table Package

  • NewTable[T any](object *T) string - Convert single struct to HTML table
  • GenTable[T any](object *T, options *slice2table.Options) string - Generate table with custom options

firefoxview Package

  • Open(execConfig *osexec.OsCommand, urls []string, mode string) - Open URLs in Firefox with mode
  • OpenInNewWindows(execConfig *osexec.OsCommand, urls ...string) - Open URLs in new Firefox windows
  • OpenInNewTabs(execConfig *osexec.OsCommand, urls ...string) - Open URLs in new Firefox tabs
  • NewPane() *Pane - Create Firefox output pane with default config
  • NewPaneWith(pageSite *mocksite.PageSite, execConfig *osexec.OsCommand) *Pane - Create pane with custom config
  • (p *Pane) Close(waitTime time.Duration) - Close pane when wait time elapses
  • (p *Pane) ShowInNewWindows(pages ...string) - Show pages in new windows
  • (p *Pane) ShowInNewTabs(pages ...string) - Show pages in new tabs
  • (p *Pane) Show(pages []string, mode string) - Show pages with mode
  • (p *Pane) Open(pages []string, mode string) - Alias of Show

w3mview Package

  • Show(execConfig *osexec.OsCommand, page string) - Display HTML page via w3m console
  • Open(execConfig *osexec.OsCommand, link string) - Open URL via w3m console
  • DumpPage(execConfig *osexec.OsCommand, page string) string - Process HTML and get text output
  • DumpLink(execConfig *osexec.OsCommand, link string) string - Fetch URL and return text
  • NewPane() *Pane - Create w3m output pane with default config
  • NewPaneWith(pageSite *mocksite.PageSite, execConfig *osexec.OsCommand) *Pane - Create pane with custom config
  • (p *Pane) Close() - Close pane and release resources
  • (p *Pane) Show(page string) - Show single page via pane
  • (p *Pane) ShowPages(pages ...string) - Show multiple pages via pane

mocksite Package

  • NewPageSite() *PageSite - Create HTTP test instance
  • (s *PageSite) Close() - Close test instance and release resources
  • (s *PageSite) SetPage(path string, page []byte) string - Set page and get access link
  • (s *PageSite) GetLink(path string) string - Get URL of page path

Examples

Check out the examples path with more usage patterns:

  • demo1x - Mock site service with Firefox automation
  • demo2x - Multiple tables in Firefox tabs
  • demo3x - Console output with w3m
  • demo4x - W3m page output with multiple datasets

📄 License

MIT License - see LICENSE.


💬 Contact & Feedback

Contributions are welcome! Report bugs, suggest features, and contribute code:

  • 🐛 Mistake reports? Open an issue on GitHub with reproduction steps
  • 💡 Fresh ideas? Create an issue to discuss
  • 📖 Documentation confusing? Report it so we can enhance it
  • 🚀 Need new features? Share the use cases to help us understand requirements
  • Performance issue? Help us optimize via reporting slow operations
  • 🔧 Configuration problem? Ask questions about complex setups
  • 📢 Track project progress? Watch the repo to get new releases and features
  • 🌟 Success stories? Share how this package enhanced the workflow
  • 💬 Feedback? We welcome suggestions and comments

🔧 Development

New code contributions, follow this process:

  1. Fork: Fork the repo on GitHub (using the webpage UI).
  2. Clone: Clone the forked project (git clone https://github.com/yourname/repo-name.git).
  3. Navigate: Navigate to the cloned project (cd repo-name)
  4. Branch: Create a feature branch (git checkout -b feature/xxx).
  5. Code: Implement the changes with comprehensive tests
  6. Testing: (Golang project) Ensure tests pass (go test ./...) and adhere to Go code style conventions
  7. Documentation: Update documentation to support client-facing changes
  8. Stage: Stage changes (git add .)
  9. Commit: Commit changes (git commit -m "Add feature xxx") ensuring backward compatible code
  10. Push: Push to the branch (git push origin feature/xxx).
  11. PR: Open a merge request on GitHub (on the GitHub webpage) with detailed description.

Please ensure tests pass and include relevant documentation updates.


🌟 Support

Welcome to contribute to this project via submitting merge requests and reporting issues.

Project Support:

  • Give GitHub stars if this project helps you
  • 🤝 Share with teammates and (golang) programming friends
  • 📝 Write tech blogs about development tools and workflows - we provide content writing support
  • 🌟 Join the ecosystem - committed to supporting open source and the (golang) development scene

Have Fun Coding with this package! 🎉🎉🎉

About

convert golang struct-slice to web table

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •