forked from PaulSonOfLars/gotg_md2html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
69 lines (56 loc) · 1.34 KB
/
common.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
package main
import (
"regexp"
"unicode"
)
func validStart(pos int, input []rune) bool {
// Last char is not a valid start char.
// If the next char is a space, it isn't a valid start either.
if pos == len(input)-1 || unicode.IsSpace(input[pos+1]) {
return false
}
// First char is always a valid start.
if pos == 0 {
return true
}
// If the previous char is alphanumeric, it is an invalid start char.
return !(unicode.IsLetter(input[pos-1]) || unicode.IsDigit(input[pos-1]))
}
func validEnd(pos int, input []rune) bool {
// First char is not a valid end char.
// If the end char has a space before it, its not valid either.
if pos == 0 || unicode.IsSpace(input[pos-1]) {
return false
}
// Last char is always a valid end char;
if pos == len(input)-1 {
return true
}
// If the next char is alphanumeric, it is an invalid end char.
return !(unicode.IsLetter(input[pos+1]) || unicode.IsDigit(input[pos+1]))
}
func contains(r rune, rr []rune) bool {
for _, x := range rr {
if r == x {
return true
}
}
return false
}
var (
link = regexp.MustCompile(`a href="(.*)"`)
customEmoji = regexp.MustCompile(`tg-emoji emoji-id="(.*)"`)
)
func IsEscaped(input []rune, pos int) bool {
if pos == 0 {
return false
}
i := pos - 1
for ; i >= 0; i-- {
if input[i] == '\\' {
continue
}
break
}
return (pos-i)%2 == 0
}