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
executable file
·170 lines (149 loc) · 5.1 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
// Package links provides a data type for links.
package links
import (
"fmt"
"path"
"strings"
"github.com/bouncepaw/mycomarkup/util"
)
// LinkType tells what type the given link is.
type LinkType int
const (
// LinkInvalid is an error state for LinkType.
LinkInvalid LinkType = iota
// LinkLocalRoot is a link like "/list", "/user-list", etc.
LinkLocalRoot
// LinkLocalHypha is a link like "test", "../test", etc.
LinkLocalHypha
// LinkExternal is an external link with specified protocol.
LinkExternal
// LinkInterwiki is currently left unused. In the future it will be used for interwiki links.
LinkInterwiki
)
// Link is an abstraction for universal representation of links, be they links in mycomarkup links or whatever.
type Link struct {
// Known stuff
srcAddress string
srcDisplay string
srcHypha string
// Parsed stuff
anchor string
address string
display string
kind LinkType
protocol string
// Settable stuff
DestinationKnown bool
}
// From makes a link from the given source address and display text on the given hypha.
func From(srcAddress, srcDisplay, srcHypha string) *Link {
link := Link{
srcAddress: strings.TrimSpace(srcAddress),
srcDisplay: strings.TrimSpace(srcDisplay),
srcHypha: strings.TrimSpace(srcHypha),
DestinationKnown: false,
}
link.address = link.srcAddress
// If there is a hash sign in the address, move everything starting from the sign to the end of the address to the anchor field and truncate the address.
if pos := strings.IndexRune(link.srcAddress, '#'); pos != -1 {
link.anchor = link.srcAddress[pos:]
link.address = link.address[:pos]
}
// NOTE: This part will need some extending with introduction of interwiki.
switch {
// If is an external link
case strings.ContainsRune(link.address, ':'):
pos := strings.IndexRune(link.address, ':')
link.kind = LinkExternal
link.protocol = link.address[:pos+1]
link.address = link.address[pos+1:]
if strings.HasPrefix(link.address, "//") && len(link.address) > 2 {
link.protocol += "//"
link.address = link.address[2:]
}
case strings.HasPrefix(link.address, "/"):
link.kind = LinkLocalRoot
case strings.HasPrefix(link.address, "./"):
link.kind = LinkLocalHypha
link.address = util.CanonicalName(path.Join(link.srcHypha, link.address[2:]))
case link.address == "..":
link.address = util.CanonicalName(path.Dir(link.srcHypha))
case strings.HasPrefix(link.address, "../"):
link.kind = LinkLocalHypha
link.address = util.CanonicalName(path.Join(path.Dir(link.srcHypha), link.address[3:]))
case strings.HasPrefix(link.address, "#"):
link.kind = LinkLocalHypha
link.anchor = link.address
link.address = util.CanonicalName(link.srcHypha)
default:
link.kind = LinkLocalHypha
link.address = util.CanonicalName(link.address)
}
// If no display text is given, copy the address there.
if link.srcDisplay == "" {
// Drop the protocol if there is any.
link.display = strings.TrimPrefix(link.srcAddress, link.protocol)
} else {
link.display = link.srcDisplay
}
return &link
}
// Exists is true if the link should be blue, not red. Red links are links to hyphae that do not exist, all other links are blue.
func (link *Link) Exists() bool {
return (link.OfKind(LinkExternal)) || (link.OfKind(LinkLocalRoot)) || (link.OfKind(LinkLocalHypha) && !link.DestinationKnown)
}
// ItExists notes that the destination makes sense, exists.
func (link *Link) ItExists() *Link {
link.DestinationKnown = true
return link
}
// Classes returns CSS class string for given link. It is not wrapped in any quotes, wrap yourself.
func (link *Link) Classes() (classes string) {
classes = "wikilink"
switch link.kind {
case LinkLocalRoot, LinkLocalHypha:
classes += " wikilink_internal"
if link.DestinationKnown {
classes += " wikilink_new"
}
case LinkInterwiki:
classes += " wikilink_interwiki"
case LinkExternal:
classes += fmt.Sprintf(" wikilink_external wikilink_%s", strings.TrimSuffix(strings.TrimSuffix(link.protocol, "://"), ":"))
}
return classes
}
// Href returns content for the href attrubute for hyperlink. You should always use it.
func (link *Link) Href() string {
switch link.kind {
case LinkExternal, LinkLocalRoot:
return link.protocol + link.address + link.anchor
default:
return "/hypha/" + link.address + link.anchor
}
}
// ImgSrc returns content for src attribute of img tag. Used with `img{}`.
func (link *Link) ImgSrc() string {
switch link.kind {
case LinkExternal, LinkLocalRoot:
return link.protocol + link.address + link.anchor
default:
return "/binary/" + link.address
}
}
// String returns a debugging string representation of the given link.
func (link *Link) String() string {
return fmt.Sprintf(`Link("%s", "%s", "%s")`, link.Href(), link.Display(), link.srcHypha)
}
// Display returns the display text of the given link.
func (link *Link) Display() string {
return link.display
}
// Address returns the address of the given link. Why would you need that?
func (link *Link) Address() string {
return link.address
}
// OfKind returns if the given link is of the given kind.
func (link *Link) OfKind(kind LinkType) bool {
return link.kind == kind
}