Compare commits

...

4 commits

3 changed files with 74 additions and 1 deletions

9
LICENSE Normal file
View file

@ -0,0 +1,9 @@
Copyright (c) 2023 ptrcnull
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

42
README.md Normal file
View file

@ -0,0 +1,42 @@
# ptrcnull/html
library wrapping `net/html` with a bunch of convenience functions
example usage:
```go
package main
import (
"log"
"os"
"git.ddd.rip/ptrcnull/html"
)
func main() {
htmlText := getSomeHtmlPage()
document, err := html.ParseDocument(htmlText)
if err != nil {
log.Fatalf("parse document: %w\n", err)
}
table := document.QuerySelector("table")
if table == nil {
log.Fatalln("table not found")
}
if os.Getenv("DEBUG") == "1" {
log.Println("table:", table.Render())
}
for _, row := range table.QuerySelectorAll("tr") {
if strings.Contains(row.Text(), "a thing you're looking for") {
cell := row.FindOne(func(n *html.Node) {
return n.HasAttr("aria-label") && n.GetAttr("aria-label")[0] == "Value"
})
log.Println("value:", cell.TrimmedText())
}
}
}
```

24
html.go
View file

@ -12,7 +12,7 @@ type Node struct {
*html.Node
}
func Parse(r io.Reader) (*Node, error) {
func ParseDocument(r io.Reader) (*Node, error) {
n, err := html.Parse(r)
if err != nil {
return nil, err
@ -20,6 +20,24 @@ func Parse(r io.Reader) (*Node, error) {
return &Node{n}, nil
}
func ParseNode(r io.Reader) (*Node, error) {
n, err := html.ParseFragment(r, nil)
if err != nil {
return nil, err
}
return &Node{n[0]}, nil
}
func NewTextNode(text string) *Node {
return &Node{
Node: &html.Node{
Type: html.TextNode,
DataAtom: 0x0,
Data: text,
},
}
}
func (n *Node) QuerySelector(selector string) *Node {
if strings.HasPrefix(selector, "#") {
return n.GetElementById(selector[1:])
@ -167,6 +185,10 @@ func (n *Node) Text() string {
return res
}
func (n *Node) TrimmedText() string {
return strings.Trim(n.Text(), " \n\t")
}
func (n *Node) Render() (string, error) {
w := bytes.NewBuffer([]byte{})
err := html.Render(w, n.Node)