-
Notifications
You must be signed in to change notification settings - Fork 2
/
truncatehtml.go
173 lines (146 loc) · 6.38 KB
/
truncatehtml.go
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
// Copyright (c) 2015 Matt Borgerson
//
// 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.
package truncatehtml
import (
"errors"
"fmt"
"regexp"
"unicode"
"unicode/utf8"
)
var UnbalancedTagsError = errors.New("unbalanced tags")
var TagExpr = regexp.MustCompile("<(/?)([A-Za-z0-9]+).*?>")
var EntityExpr = regexp.MustCompile("&#?[A-Za-z0-9]+;")
// TruncateHtml will truncate a given byte slice to a maximum of maxlen visible
// characters and optionally append ellipsis. HTML tags are automatically closed
// generating valid truncated HTML.
func TruncateHtml(buf []byte, maxlen int, ellipsis string) ([]byte, error) {
// Here's the gist: Scan the input bytestream. While scanning, count the
// number of visible characters--that is, characters which are not part of
// markup tags. When a start tag is encountered, push the tag name onto a
// stack. When visible character count >= maxlen, or the EOF is reached,
// stop counting. Copy from the input stream the bytes from the start to the
// current scanning pointer. Finally, pop each tag off the tag stack and
// append it to the output stream in the form of a closing tag.
// We will consider HTML or XHTML as valid input. The following elements,
// called "Void Elements" need not conform to the XHTML <tag /> convention
// of void elements and may appear simply as <tag>. Hence, if one of the
// following is picked up by the tag expression as a start tag, do not add
// it to the stack of tags that should be closed.
voidElementTags := []string{"area", "base", "br", "col", "embed", "hr",
"img", "input", "keygen", "link", "meta",
"param", "source", "track", "wbr"}
// Check to see if no input was provided.
if buf == nil || len(buf) == 0 || maxlen == 0 {
return []byte{}, nil
}
tagStack := []string{}
visible := 0
bufPtr := 0
for bufPtr < len(buf) && visible < maxlen {
// Move to nearest tag and count visible characters along the way.
offset := 0
visibleCharacterMaxReached := false
entityDetected := false
for localOffset, runeValue := range string(buf[bufPtr:]) {
offset = localOffset
if runeValue == '<' {
// Start of tag.
break
} else if runeValue == '&' {
// Possible start of HTML Entity
loc := EntityExpr.FindIndex(buf[bufPtr+localOffset:])
if loc != nil && loc[0] == 0 {
// Entity found!
entityDetected = true
offset += loc[1]-1 // Now pointing to ;
}
visible += 1
} else if unicode.IsPrint(runeValue) && !unicode.IsSpace(runeValue) {
// Printable, non-space character. Increment visible count.
visible += 1
}
// Check if the limit of visible characters has been reached.
if visible >= maxlen {
visibleCharacterMaxReached = true
break
}
if entityDetected {
break
}
}
// Increment bufPtr to end of scanned section
bufPtr += offset
// Stop scanning if the end of the buffer was reached or if the max
// desired visible characters was reached
if visibleCharacterMaxReached || bufPtr >= len(buf)-1 {
break
}
// If an entity was detected, continue scanning for next tag
if entityDetected {
// Advance past the ;
bufPtr += 1
continue
}
// Now find the expression sub-matches
matches := TagExpr.FindSubmatch(buf[bufPtr:])
tagName := string(matches[2])
// Advance pointer to the end of the tag
bufPtr += len(matches[0])
// If this is a void element, do not count it as a start tag
isVoidElement := false
for _, voidElementTagName := range voidElementTags {
if tagName == voidElementTagName {
isVoidElement = true
break
}
}
if isVoidElement {
continue
}
isStartTag := len(matches[1]) == 0
if isStartTag {
// This is a start tag. Push the tag to the stack.
tagStack = append(tagStack, tagName)
} else {
// This is an end tag. First, check to make sure the end tag is
// matches what's on top of the stack.
if len(tagStack) == 0 || tagStack[len(tagStack)-1] != tagName {
return nil, UnbalancedTagsError
}
// Now, pop the tag stack.
tagStack = tagStack[0:len(tagStack)-1]
}
}
// At this point, bufPtr points to the last rune that should be copied to
// the output stream. Increment bufPtr past this rune, turning bufPtr into
// the number of bytes that should be copied.
_, size := utf8.DecodeRune(buf[bufPtr:])
bufPtr += size
// Copy the desired input to the output buffer.
output := buf[0:bufPtr]
// Copy ellipsis
output = append(output, []byte(ellipsis)...)
// Finally, create a closing tag for each tag in the stack.
for i:=len(tagStack)-1; i >= 0; i-- {
output = append(output, []byte(fmt.Sprintf("</%s>", tagStack[i]))...)
}
return output, nil
}