This repository has been archived by the owner on Aug 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
link.go
272 lines (233 loc) · 6.35 KB
/
link.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package links
import (
"fmt"
"github.com/bouncepaw/mycomarkup/v4/mycocontext"
"github.com/bouncepaw/mycomarkup/v4/options"
"path"
"strings"
)
// Link is a link of some kind.
type Link interface {
// Classes returns a string to put into the class attr in HTML.
Classes(ctx mycocontext.Context) string
// LinkHref returns a string to put into the href attr of <a>.
LinkHref(ctx mycocontext.Context) string
// ImgSrc returns a string to put into the src attr of <img>.
ImgSrc(ctx mycocontext.Context) string
// DisplayedText returns a string to put inside <a>.
DisplayedText() string
// HyphaProbe returns a function that captures the Link. Probes are checked against all existing hyphae. This is Mycorrhiza-specific. If it is nil, do not check this link for existence. TODO: make it optional.
HyphaProbe() func(string)
}
func LinkFrom(ctx mycocontext.Context, target, display string) Link {
target, display = strings.TrimSpace(target), strings.TrimSpace(display)
switch {
case strings.ContainsRune(target, ':'):
return &URLLink{
target: target,
display: display,
}
case strings.HasPrefix(target, "/"):
return &LocalRootedLink{
target: target,
display: display,
}
case strings.ContainsRune(target, '>'):
gtpos := strings.IndexRune(target, '>')
if gtpos == 0 {
return &LocalLink{
target: target[1:],
display: display,
}
}
return &InterwikiLink{
prefix: target[:gtpos],
target: target[gtpos+1:],
display: display,
}
case target == "..":
return &LocalLink{
target: path.Dir(ctx.HyphaName()),
display: display,
}
case strings.HasPrefix(target, "./"):
var anchor string
if hashPos := strings.IndexRune(target, '#'); hashPos != -1 {
anchor = target[hashPos+1:]
target = target[:hashPos]
}
return &LocalLink{
target: path.Join(ctx.HyphaName(), target[2:]),
display: display,
anchor: anchor,
}
case strings.HasPrefix(target, "../"):
var anchor string
if hashPos := strings.IndexRune(target, '#'); hashPos != -1 {
anchor = target[hashPos+1:]
target = target[:hashPos]
}
return &LocalLink{
target: path.Join(path.Dir(ctx.HyphaName()), target[3:]),
display: display,
anchor: anchor,
}
case strings.ContainsRune(target, '#'):
hashPos := strings.IndexRune(target, '#')
anchor := target[hashPos+1:]
target = target[:hashPos]
return &LocalLink{
target: target,
display: display,
anchor: anchor,
}
default:
return &LocalLink{
target: target,
display: display,
}
}
}
type LocalLink struct {
target string
display string
anchor string
existing bool
}
func (l *LocalLink) Classes(ctx mycocontext.Context) string {
res := "wikilink wikilink_internal"
if !l.existing && mycocontext.Options(ctx).RedLinksSupported {
res += " wikilink_new"
}
return res
}
func (l *LocalLink) LinkHref(ctx mycocontext.Context) string {
if l.anchor != "" {
return mycocontext.Options(ctx).LocalLinkHref(l.target + "#" + l.anchor)
}
return mycocontext.Options(ctx).LocalLinkHref(l.target)
}
func (l *LocalLink) ImgSrc(ctx mycocontext.Context) string {
if l.anchor != "" {
return mycocontext.Options(ctx).LocalImgSrc(l.target + "#" + l.anchor)
}
return mycocontext.Options(ctx).LocalImgSrc(l.target)
}
func (l *LocalLink) DisplayedText() string {
if l.display == "" && l.anchor != "" {
return l.target + "#" + l.anchor
}
if l.display == "" {
return l.target
}
return l.display
}
func (l *LocalLink) HyphaProbe() func(string) {
if l.target == "" {
return nil
}
done := false
return func(docName string) {
if done {
return
}
if docName == l.target {
l.existing = true
done = true
}
}
}
type LocalRootedLink struct {
target, display string
}
func (l *LocalRootedLink) Classes(ctx mycocontext.Context) string {
return "wikilink wikilink_internal"
}
func (l *LocalRootedLink) LinkHref(ctx mycocontext.Context) string {
return l.target
}
func (l *LocalRootedLink) ImgSrc(ctx mycocontext.Context) string {
return l.target
}
func (l *LocalRootedLink) DisplayedText() string {
if l.display == "" {
return l.target
}
return l.display
}
func (l *LocalRootedLink) HyphaProbe() func(string) {
return nil
}
type URLLink struct {
target string
display string
}
func (l *URLLink) protocol() string {
return l.target[:strings.IndexRune(l.target, ':')]
}
func (l *URLLink) Classes(ctx mycocontext.Context) string {
return fmt.Sprintf(
"wikilink wikilink_external wikilink_%s",
l.protocol(),
)
}
func (l *URLLink) LinkHref(ctx mycocontext.Context) string {
return l.target
}
func (l *URLLink) ImgSrc(ctx mycocontext.Context) string {
return l.target
}
func (l *URLLink) DisplayedText() string {
if l.display == "" {
return l.target
}
return l.display
}
func (l *URLLink) HyphaProbe() func(string) {
return nil
}
/*
InterwikiLink in Mycomarkup has this syntax:
[[prefix>target]]
[[prefix>target|display]]
For every prefix, there is a known link format. A link format is a format string, that might resemble Go's format strings, but they are actually not. This is DSL for link formats. It is inspired by DokuWiki's interwiki link format: https://www.dokuwiki.org/interwiki.
https://example.org/view/{NAME}
Supported instructions are (more will be added):
{NAME} is the document name without any encoding.
*/
type InterwikiLink struct {
prefix, target, display string
err options.InterwikiError
}
func (l *InterwikiLink) TryToGetError(ctx mycocontext.Context) bool {
switch {
case !mycocontext.Options(ctx).InterwikiSupported:
l.err = options.NotSetUp
default:
_, l.err = mycocontext.Options(ctx).LinkHrefFormatForInterwikiPrefix(l.prefix)
}
return l.err != options.Ok
}
func (l *InterwikiLink) Err() error {
return l.err
}
func (l *InterwikiLink) Classes(ctx mycocontext.Context) string {
return "wikilink wikilink_interwiki"
}
func (l *InterwikiLink) LinkHref(ctx mycocontext.Context) string {
format, _ := mycocontext.Options(ctx).LinkHrefFormatForInterwikiPrefix(l.prefix)
return strings.ReplaceAll(format, "{NAME}", l.target)
}
func (l *InterwikiLink) ImgSrc(ctx mycocontext.Context) string {
format, _ := mycocontext.Options(ctx).ImgSrcFormatForInterwikiPrefix(l.prefix)
return strings.ReplaceAll(format, "{NAME}", l.target)
}
func (l *InterwikiLink) DisplayedText() string {
if l.display == "" {
return l.prefix + ">" + l.target
}
return l.display
}
func (l *InterwikiLink) HyphaProbe() func(string) {
return nil
}