File: 1_select.go

package info (click to toggle)
golang-github-johanneskaufmann-html-to-markdown 2.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,080 kB
  • sloc: makefile: 3
file content (50 lines) | stat: -rw-r--r-- 1,181 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package table

import (
	"github.com/JohannesKaufmann/dom"
	"golang.org/x/net/html"
)

func selectHeaderRowNode(node *html.Node) *html.Node {
	thead := dom.FindFirstNode(node, func(n *html.Node) bool {
		return dom.NodeName(n) == "thead"
	})
	if thead != nil {
		firstTr := dom.FindFirstNode(thead, func(n *html.Node) bool {
			return dom.NodeName(n) == "tr"
		})
		if firstTr != nil {
			// YEAH we found the "tr" inside the "thead"
			return firstTr
		}
	}

	firstTh := dom.FindFirstNode(node, func(n *html.Node) bool {
		return dom.NodeName(n) == "th"
	})
	if firstTh != nil {
		// YEAH we found the "th"
		return firstTh.Parent
	}

	return nil
}
func selectNormalRowNodes(tableNode *html.Node, selectedHeaderRowNode *html.Node) []*html.Node {
	var collected []*html.Node

	var finder func(node *html.Node)
	finder = func(node *html.Node) {
		name := dom.NodeName(node)
		if name == "tr" && node != selectedHeaderRowNode {
			// We want to make sure to not select the header row a *second* time.
			collected = append(collected, node)
		}

		for child := node.FirstChild; child != nil; child = child.NextSibling {
			finder(child)
		}
	}
	finder(tableNode)

	return collected
}