-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinks.go
More file actions
207 lines (196 loc) · 4.87 KB
/
Copy pathlinks.go
File metadata and controls
207 lines (196 loc) · 4.87 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package cli
import (
"fmt"
"slices"
"strings"
"unicode"
"unicode/utf8"
)
// link turns the plain text s into markdown, applying [Doc.Linkify] and
// then [Doc.Terms], and making a link of any bare URL either leaves
// behind.
func (d *Doc) link(s string) string {
if s == "" {
return s
}
if d.Linkify != nil {
s = d.Linkify(s)
}
return linkText(s, d.terms())
}
type term struct {
text string
url string
}
// terms is [Doc.Terms] with its URLs resolved and ordered so that the
// longest term matches first.
func (d *Doc) terms() []term {
res := make([]term, 0, len(d.Terms))
for text, url := range d.Terms {
if text == "" {
continue
}
res = append(res, term{text: text, url: d.url(url)})
}
slices.SortFunc(res, func(a, b term) int {
if n := len(b.text) - len(a.text); n != 0 {
return n
}
return strings.Compare(a.text, b.text)
})
return res
}
// url resolves u against [Doc.Site]: a u which is already absolute, or a
// fragment, or a Site which is empty, leaves u alone, and anything else
// is a path under Site.
//
// Site is a prefix and not a base in the sense of RFC 3986: the leading
// '/' of a "/schema/" is dropped rather than resolving to the root of
// the host, since a Site is often a project page such as
// "https://acme.github.io/thing/" whose root belongs to somebody else.
func (d *Doc) url(u string) string {
switch {
case d.Site == "", hasScheme(u), strings.HasPrefix(u, "//"), strings.HasPrefix(u, "#"):
return u
}
return strings.TrimSuffix(d.Site, "/") + "/" + strings.TrimPrefix(u, "/")
}
// hasScheme reports whether u begins with a URL scheme, as in
// "https:" or "mailto:".
func hasScheme(u string) bool {
for i, r := range u {
switch {
case r == ':':
return i > 0
case unicode.IsLetter(r):
case i > 0 && (unicode.IsDigit(r) || r == '+' || r == '-' || r == '.'):
default:
return false
}
}
return false
}
// linkText links the first occurrence of each term in s, along with any
// bare URL, leaving alone whatever is inside backticks or inside a link
// which is already there.
func linkText(s string, terms []term) string {
b := &strings.Builder{}
used := make(map[string]bool, len(terms))
i := 0
for i < len(s) {
if j := skipProtected(s, i); j > i {
b.WriteString(s[i:j])
i = j
continue
}
if j := bareURL(s, i); j > i {
fmt.Fprintf(b, "<%s>", s[i:j])
i = j
continue
}
if t, j := matchTerm(s, i, terms, used); j > i {
fmt.Fprintf(b, "[%s](%s)", t.text, t.url)
used[t.text] = true
i = j
continue
}
b.WriteByte(s[i])
i++
}
return b.String()
}
// matchTerm returns the term occurring at s[i:] and the end of the
// occurrence, or the zero term and i if there is none.
func matchTerm(s string, i int, terms []term, used map[string]bool) (term, int) {
for _, t := range terms {
if used[t.text] || !strings.HasPrefix(s[i:], t.text) {
continue
}
end := i + len(t.text)
if isWordAt(s[:i], true) || isWordAt(s[end:], false) {
continue
}
return t, end
}
return term{}, i
}
// isWordAt reports whether the rune at the near end of s -- its last if
// last, else its first -- is one which a term may not abut.
func isWordAt(s string, last bool) bool {
if s == "" {
return false
}
r, _ := utf8.DecodeRuneInString(s)
if last {
r, _ = utf8.DecodeLastRuneInString(s)
}
return unicode.IsLetter(r) || unicode.IsDigit(r) || r == '-' || r == '_'
}
// skipProtected returns the end of the span at s[i:] whose contents must
// be left as they are -- a code span, a link, or an autolink -- or i if
// there is no such span there.
func skipProtected(s string, i int) int {
switch s[i] {
case '`':
n := 1
for i+n < len(s) && s[i+n] == '`' {
n++
}
j := strings.Index(s[i+n:], strings.Repeat("`", n))
if j < 0 {
// an unclosed run of backticks is just text, but it
// is text no term may be found inside of.
return i + n
}
return i + n + j + n
case '[':
j := strings.Index(s[i:], "]")
if j < 0 || i+j+1 == len(s) || s[i+j+1] != '(' {
// brackets which are not a link are just text.
return i
}
k := strings.Index(s[i+j+1:], ")")
if k < 0 {
return i
}
return i + j + 1 + k + 1
case '<':
j := strings.Index(s[i:], ">")
if j < 0 || strings.ContainsAny(s[i:i+j], " \t") {
return i
}
return i + j + 1
}
return i
}
// bareURL returns the end of the URL written out at s[i:], or i if there
// is none.
func bareURL(s string, i int) int {
if !strings.HasPrefix(s[i:], "http://") && !strings.HasPrefix(s[i:], "https://") {
return i
}
if isWordAt(s[:i], true) {
return i
}
end := len(s)
for j, r := range s[i:] {
if unicode.IsSpace(r) || r == '<' || r == '>' {
end = i + j
break
}
}
// trailing punctuation ends the sentence, not the URL.
for end > i {
switch s[end-1] {
case '.', ',', ';', ':', '!', '?', '\'', '"':
case ')':
if strings.Contains(s[i:end], "(") {
return end
}
default:
return end
}
end--
}
return end
}