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
/
rocket.go
80 lines (66 loc) · 1.96 KB
/
rocket.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
package blocks
import (
"fmt"
"github.com/bouncepaw/mycomarkup/v2/globals"
"github.com/bouncepaw/mycomarkup/v2/util"
"strings"
"github.com/bouncepaw/mycomarkup/v2/links"
)
// LaunchPad is a container for RocketLinks.
type LaunchPad struct {
Rockets []RocketLink
}
// ID returns the launchpad's id which is rocket- and a number. Note that it does not say launchpad.
func (lp LaunchPad) ID(counter *IDCounter) string {
counter.launchpads++
return fmt.Sprintf("rocket-%d", counter.launchpads)
}
func (lp LaunchPad) isBlock() {}
// MakeLaunchPad returns an empty launchpad. Add rocket links there using AddRocket.
func MakeLaunchPad() LaunchPad {
return LaunchPad{[]RocketLink{}}
}
// AddRocket stores the rocket link in the launchpad.
func (lp *LaunchPad) AddRocket(rl RocketLink) {
lp.Rockets = append(lp.Rockets, rl)
}
// ColorRockets marks links to existing hyphae as existing.
func (lp *LaunchPad) ColorRockets() {
globals.HyphaIterate(func(hyphaName string) {
for i, rocket := range lp.Rockets {
// TODO: do not canonize every time
if util.CanonicalName(rocket.TargetHypha()) == hyphaName {
rocket.Link.MarkAsExisting()
}
lp.Rockets[i] = rocket
}
})
}
// RocketLink is a rocket link which is meant to be nested inside LaunchPad.
type RocketLink struct {
IsEmpty bool
links.Link
}
func (r RocketLink) isBlock() {}
// ID returns an empty string because rocket links do not have ids on their own.
func (r RocketLink) ID(_ *IDCounter) string {
return ""
}
// MakeRocketLink parses the rocket link on the given line and returns it.
func MakeRocketLink(line, hyphaName string) RocketLink {
line = strings.TrimSpace(line[2:])
if line == "" {
return RocketLink{IsEmpty: true}
}
var (
// Address is text after => till first whitespace
addr = strings.Fields(line)[0]
// Display is what is left
display = strings.TrimPrefix(line, addr)
rl = RocketLink{
IsEmpty: false,
Link: *links.From(addr, display, hyphaName),
}
)
return rl
}