File: collapse.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 (174 lines) | stat: -rw-r--r-- 4,900 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*

The function to collapse whitespace was adapted from the "turndown" library by Dom Christie,
which was adapted from the "collapse-whitespace" library by Luc Thevenard.

It was ported from Javascript to Golang by Johannes Kaufmann for the use in the "html-to-markdown" library.
To increase performance the use of regex was replaced by custom code.

https://github.com/wooorm/collapse-white-space
https://github.com/mixmark-io/turndown
https://github.com/JohannesKaufmann/html-to-markdown

-----------

MIT License

Copyright (c) 2017 Dom Christie
Copyright (c) 2014 Luc Thevenard <lucthevenard@gmail.com>
Copyright (c) 2018 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.

*/

// collapse can collapse whitespace in html elements.
//
// It is a port from the Javascript library "turndown" to Golang.
package collapse

import (
	"strings"

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

func nextNode(prev *html.Node, current *html.Node, domFuncs *DomFuncs) *html.Node {
	if (prev != nil && prev.Parent == current) || domFuncs.IsPreformattedNode(current) {
		if current.NextSibling != nil {
			return current.NextSibling
		}

		return current.Parent
	}

	if current.FirstChild != nil {
		return current.FirstChild
	}
	if current.NextSibling != nil {
		return current.NextSibling
	}

	return current.Parent
}

func removeNode(node *html.Node) *html.Node {
	next := node.NextSibling
	if next == nil {
		next = node.Parent
	}

	node.Parent.RemoveChild(node)

	return next

}

type DomFuncs struct {
	IsBlockNode        func(node *html.Node) bool
	IsVoidNode         func(node *html.Node) bool
	IsPreformattedNode func(node *html.Node) bool
}

func fillDefaultDomFuncs(domFuncs *DomFuncs) *DomFuncs {
	if domFuncs == nil {
		domFuncs = &DomFuncs{}
	}
	if domFuncs.IsBlockNode == nil {
		domFuncs.IsBlockNode = defaultIsBlockNode
	}
	if domFuncs.IsVoidNode == nil {
		domFuncs.IsVoidNode = defaultIsVoidNode
	}
	if domFuncs.IsPreformattedNode == nil {
		domFuncs.IsPreformattedNode = defaultIsPreformattedNode
	}
	return domFuncs

}
func Collapse(element *html.Node, domFuncs *DomFuncs) {
	domFuncs = fillDefaultDomFuncs(domFuncs)
	// - - - - - - - - - - - - - - - - - - //

	if element.FirstChild == nil || domFuncs.IsPreformattedNode(element) {
		return
	}

	var prevText *html.Node = nil
	var keepLeadingWs = false

	var prev *html.Node = nil
	var node = nextNode(prev, element, domFuncs)

	for node != element {
		if node.Type == html.TextNode /* node.nodeType == 4 */ { // Node.TEXT_NODE or Node.CDATA_SECTION_NODE
			var text = replaceAnyWhitespaceWithSpace(node.Data)

			if (prevText == nil || strings.HasSuffix(prevText.Data, " ")) &&
				!keepLeadingWs && text[0] == ' ' {
				text = text[1:]
			}

			// `text` might be empty at this point.
			if text == "" {
				node = removeNode(node)
				continue
			}

			node.Data = text

			prevText = node
		} else if node.Type == html.ElementNode { // Node.ELEMENT_NODE
			if domFuncs.IsBlockNode(node) || dom.NodeName(node) == "br" {
				if prevText != nil {
					prevText.Data = strings.TrimSuffix(prevText.Data, " ")
				}

				prevText = nil
				keepLeadingWs = false
			} else if domFuncs.IsVoidNode(node) || domFuncs.IsPreformattedNode(node) || dom.NodeName(node) == "code" {
				// Avoid trimming space around non-block, non-BR void elements and inline PRE.
				prevText = nil
				keepLeadingWs = true
			} else if prevText != nil {
				// Drop protection if set previously.
				keepLeadingWs = false
			}
		} else if node.Type == html.CommentNode {
			// TODO: Is this enough to keep the comments? Does this cause other problems?
		} else {
			// E.g. DoctypeNode

			node = removeNode(node)
			continue
		}

		var nextNode = nextNode(prev, node, domFuncs)
		prev = node
		node = nextNode
	}

	if prevText != nil {
		prevText.Data = strings.TrimSuffix(prevText.Data, " ")
		if prevText.Data == "" {
			removeNode(prevText)
		}
	}
}