Skip to content

golang library and lsp for parsing html tables into stucts leveraging goquery to be configurable.

License

Notifications You must be signed in to change notification settings

conneroisu/seltabl

Repository files navigation

seltabl

go.dev Build Status Go Report Card

A golang library with accompanying lsp for configurably parsing html sequences into stucts originally built for html tables, but can be used for any html sequence.

Enables data binding to structs and provides a simple, but dynamic way to define a table schema.

Installation

Install the package in a project with:

go get github.com/conneroisu/seltabl

Install the lsp with:

go install github.com/conneroisu/seltabl/tools/seltabls@latest

Usage

package main

import (
	"fmt"
	"github.com/conneroisu/seltabl"
	"github.com/conneroisu/seltabl/testdata"
)

type TableStruct struct {
	A string `json:"a" seltabl:"a" hSel:"tr:nth-child(1) td:nth-child(1)" dSel:"tr td:nth-child(1)" ctl:"text"`
	B string `json:"b" seltabl:"b" hSel:"tr:nth-child(1) td:nth-child(2)" dSel:"tr td:nth-child(2)" ctl:"text"`
}

var fixture = `
<table>
	<tr>
		<td>a</td>
		<td>b</td>
	</tr>
	<tr>
		<td>1</td>
		<td>2</td>
	</tr>
	<tr>
		<td>3</td>
		<td>4</td>
	</tr>
	<tr>
		<td>5</td>
		<td>6</td>
	</tr>
	<tr>
		<td>7</td>
		<td>8</td>
	</tr>
</table>
`

func main() {
	fss, err := seltabl.NewFromString[TableStruct](fixture)
	if err != nil {
		panic(fmt.Errorf("failed to parse html: %w", err))
	}
	for _, fs := range fss {
		fmt.Printf("%+v\n", fs)
	}
}

Output:

{A:1 B:2}
{A:3 B:4}
{A:5 B:6}
{A:7 B:8}

Development

A makefile at the root of the project is provided to help with development.

Testing

One can run the tests with:

make test

Linting

One can run the linter with:

make lint

Formatting

One can run the formatter with:

make fmt

Generating documentation

One can run the documentation generator with:

make doc

License

MIT

About

golang library and lsp for parsing html tables into stucts leveraging goquery to be configurable.

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages