conneroisu / seltabl

golang library for parsing html tables into stucts leveraging goquery to be configurable with useful developer tooling including a language server.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

seltabl

seltabl logo

go.dev Build Status Go Report Card

A golang library with accompanying cli and language server 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 cli containing the language server operating over the lsp protocol and package command line utilities with:

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

Recording of Language Server

Usage

package main

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

type TableStruct struct {
	A string `json:"a" hSel:"tr:nth-child(1) td:nth-child(1)" dSel:"tr td:nth-child(1)" ctl:"text"`
	B string `json:"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

Types of ctl selectors:

  • text (default) (queries the text of the selected element)
  • spaces (queries the text of the selected element split by spaces)
  • query (queries the attributes of the selected elemente)

About

golang library for parsing html tables into stucts leveraging goquery to be configurable with useful developer tooling including a language server.

License:MIT License


Languages

Language:Go 91.6%Language:Shell 7.9%Language:Makefile 0.5%