pax_global_header00006660000000000000000000000064147456761660014540gustar00rootroot0000000000000052 comment=21d47691f2448bd7a870891daf85afa091af9bca golang-github-johanneskaufmann-dom-0.2.0/000077500000000000000000000000001474567616600203675ustar00rootroot00000000000000golang-github-johanneskaufmann-dom-0.2.0/.github/000077500000000000000000000000001474567616600217275ustar00rootroot00000000000000golang-github-johanneskaufmann-dom-0.2.0/.github/dependabot.yml000066400000000000000000000004251474567616600245600ustar00rootroot00000000000000# Please see the documentation for all configuration options: # https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates version: 2 updates: - package-ecosystem: "gomod" directory: "/" schedule: interval: "weekly" golang-github-johanneskaufmann-dom-0.2.0/.github/workflows/000077500000000000000000000000001474567616600237645ustar00rootroot00000000000000golang-github-johanneskaufmann-dom-0.2.0/.github/workflows/go.yml000066400000000000000000000021271474567616600251160ustar00rootroot00000000000000name: Go on: push: branches: [ main ] pull_request: branches: [ main ] jobs: # Test the latest go version # and upload the test coverage. test_latest: name: Go latest stable runs-on: ubuntu-latest steps: - name: Setup Go uses: actions/setup-go@v5 with: go-version: 'stable' check-latest: true - name: Checkout code uses: actions/checkout@v4 - name: Build run: go build -v . - name: Test run: go test ./... -v -race -coverprofile=coverage.txt -covermode=atomic # Test the latest three golang version # on different operating systems. test_versions: strategy: matrix: go: ['1.22'] os: [ubuntu-latest, macos-latest, windows-latest] name: Go ${{ matrix.go }} on ${{ matrix.os }} runs-on: ${{ matrix.os }} steps: - name: Setup Go uses: actions/setup-go@v5 with: go-version: ${{ matrix.go }} - name: Checkout code uses: actions/checkout@v4 - name: Test run: go test ./... -v -race -cover golang-github-johanneskaufmann-dom-0.2.0/.gitignore000066400000000000000000000007521474567616600223630ustar00rootroot00000000000000# If you prefer the allow list template instead of the deny list, see community template: # https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore # # Binaries for programs and plugins *.exe *.exe~ *.dll *.so *.dylib # Test binary, built with `go test -c` *.test # Output of the go coverage tool, specifically when used with LiteIDE *.out # Dependency directories (remove the comment below to include it) # vendor/ # Go workspace file go.work go.work.sum golang-github-johanneskaufmann-dom-0.2.0/LICENSE000066400000000000000000000020621474567616600213740ustar00rootroot00000000000000MIT License Copyright (c) 2024 Johannes Kaufmann Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. golang-github-johanneskaufmann-dom-0.2.0/README.md000066400000000000000000000153571474567616600216610ustar00rootroot00000000000000# dom [![Go Reference](https://pkg.go.dev/badge/github.com/JohannesKaufmann/dom.svg)](https://pkg.go.dev/github.com/JohannesKaufmann/dom) Helper functions for "net/html" that make it easier to interact with `*html.Node`. πŸš€ [Getting Started](#getting-started) - πŸ“š [Documentation](#documentation) - πŸ§‘β€πŸ’» [Examples](/examples/) ## Installation ```bash go get -u github.com/JohannesKaufmann/dom ``` > [!NOTE] > This "dom" libary was developed for the needs of the [html-to-markdown](https://github.com/JohannesKaufmann/html-to-markdown) library. > That beeing said, please submit any functions that you need. ## Getting Started ```go package main import ( "fmt" "log" "strings" "github.com/JohannesKaufmann/dom" "golang.org/x/net/html" ) func main() { input := ` ` doc, err := html.Parse(strings.NewReader(input)) if err != nil { log.Fatal(err) } // - - - // firstLink := dom.FindFirstNode(doc, func(node *html.Node) bool { return dom.NodeName(node) == "a" }) fmt.Println("href:", dom.GetAttributeOr(firstLink, "href", "")) } ``` ## Node vs Element The naming scheme in this library is: - "Node" means `*html.Node{}` - This means _any_ node in the tree of nodes. - "Element" means `*html.Node{Type: html.ElementNode}` - This means _only_ nodes with the type of `ElementNode`. For example `

`, ``, ``, ... but not `#text`, ``, ... For most functions, there are two versions. For example: - `FirstChildNode()` and `FirstChildElement()` - `AllChildNodes()` and `AllChildElements()` - ... ## Documentation [![Go Reference](https://pkg.go.dev/badge/github.com/JohannesKaufmann/dom.svg)](https://pkg.go.dev/github.com/JohannesKaufmann/dom) ### Attributes & Content You can get the attributes of a node using `GetAttribute`, `GetAttributeOr` or the more specialized `GetClasses` that returns a slice of strings. For matching nodes, `HasID` and `HasClass` can be used. If you want to collect the #text of all the child nodes, you can call `CollectText`. ```go name := dom.NodeName(node) // "h2" href := dom.GetAttributeOr(node, "href", "") // "github.com" isHeading := dom.HasClass(node, "repo__name") // `true` content := dom.CollectText(node) // "Lorem ipsum" ``` --- ### Children & Siblings You can already use `node.FirstChild` to get the first child _node_. For the convenience we added `FirstChildNode()` and `FirstChildElement()` which returns `*html.Node`. To get all direct children, use `AllChildNodes` and `AllChildElements` which returns `[]*html.Node`. - `PrevSiblingNode` and `PrevSiblingElement` - `NextSiblingNode` and `NextSiblingElement` ### Find Nodes Searching for nodes deep in the tree is made easier with: ```go firstParagraph := dom.FindFirstNode(doc, func(node *html.Node) bool { return dom.NodeName(node) == "p" }) // *html.Node allParagraphs := dom.FindAllNodes(doc, func(node *html.Node) bool { return dom.NodeName(node) == "p" }) // []*html.Node ``` - πŸ§‘β€πŸ’» [Example code, find](/examples/find/main.go) - πŸ§‘β€πŸ’» [Example code, selectors](/examples/selectors/main.go) --- ### Get next/previous neighbors What is special about this? The order! If you are somewhere in the DOM, you can call `GetNextNeighborNode` to get the next node, even if it is _further up_ the tree. The order is the same as you would see the elements in the DOM. ```go node := startNode for node != nil { fmt.Println(dom.NodeName(node)) node = dom.GetNextNeighborNode(node) } ``` If we start the `for` loop at the `

heading

description