File: list_end_comment.go

package info (click to toggle)
golang-github-johanneskaufmann-html-to-markdown 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,084 kB
  • sloc: makefile: 3
file content (74 lines) | stat: -rw-r--r-- 1,451 bytes parent folder | download | duplicates (2)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package domutils

import (
	"context"

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

var ListEndCommentData = "THE END"

func AddListEndComments(ctx context.Context, doc *html.Node) {
	node := doc
	for node != nil {
		if nameIsList(node) && nextNameIsList(node) {
			insertComment(node)
		}

		node = dom.GetNextNeighborElement(node)
	}
}

func nameIsList(node *html.Node) bool {
	name := dom.NodeName(node)
	return name == "ul" || name == "ol"
}

func insertComment(listNode *html.Node) {
	comment := &html.Node{
		Type: html.CommentNode,
		Data: ListEndCommentData,
	}
	listNode.Parent.InsertBefore(comment, listNode.NextSibling)
}

func nextNameIsList(startNode *html.Node) bool {
	node := dom.GetNextNeighborNodeExcludingOwnChild(startNode)

	for node != nil {
		name := dom.NodeName(node)
		if name == "ul" || name == "ol" {
			return true
		}
		if name == "li" {
			return false
		}
		if name == "#comment" && node.Data == ListEndCommentData {
			return false
		}

		// If there is any text between two lists
		// they are automatically not connected anymore.
		if node.Type == html.TextNode {
			return false
		}

		// - - - - //

		if name == "hr" {
			// A divider already seperates two lists...
			return false
		}

		// TODO: RunContext.Render()
		// -> get acess to keepRemoveMap

		// TODO: look in the KeepRemoveMap?
		// e.g. ul then script then ul

		node = dom.GetNextNeighborNode(node)
		continue
	}
	return false
}