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
/
img.go
82 lines (71 loc) · 2.37 KB
/
img.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
package blocks
import (
"fmt"
"github.com/bouncepaw/mycomarkup/v4/links"
"github.com/bouncepaw/mycomarkup/v4/mycocontext"
)
// Img is an image gallery, consisting of zero or more images.
type Img struct {
Entries []ImgEntry
layout ImgLayout
}
// ID returns the gallery's id which is img- and a number.
func (img Img) ID(counter *IDCounter) string {
counter.imgs++
return fmt.Sprintf("img-%d", counter.imgs)
}
// NewImg returns a new Img.
func NewImg(entries []ImgEntry, layout ImgLayout) Img {
return Img{
Entries: entries,
layout: layout,
}
}
// Layout returns Img's layout.
func (img Img) Layout() ImgLayout { return img.layout }
// HasOneImage returns true if img has exactly one image. The image may have a description.
//
// TODO: get rid of the concept.
func (img Img) HasOneImage() bool {
return len(img.Entries) == 1
}
// WithExistingTargetsMarked returns a new Img with its ImgEntries colored according to their existence.
//
// This functions iterates over hyphae once.
func (img Img) WithExistingTargetsMarked(ctx mycocontext.Context) Img {
// bouncepaw: I'm so sorry this function is this complex.
// We create this structure to keep track of what targets we have ‘ticked‘ ✅.
// We do not compare hypha names with ticked targets.
// Important: the structure retains the same order as the original img.Entries.
type check struct {
shouldCheck bool
target links.Link
}
var entryCheckList []check
for _, entry := range img.Entries {
entryCheckList = append(entryCheckList, check{
shouldCheck: entry.Target.OfKind(links.LinkLocalHypha), // Other kinds are blue by definition
target: entry.Target,
})
}
mycocontext.IterateHyphaNamesWith(ctx, func(hn string) {
// Go through every entry and mark them accordingly.
for i, entryCheck := range entryCheckList {
shouldCheck, target := entryCheck.shouldCheck, entryCheck.target
if shouldCheck && hn == target.TargetHypha() {
entryCheckList[i] = check{
shouldCheck: false,
target: target.CopyMarkedAsExisting(),
}
}
}
})
// Collect the results. Some entries are left unmarked. It means they are red.
var entries []ImgEntry
for i, entry := range img.Entries {
// Indices of entryCheckList and img.Entries are the same for the corresponding elements.
entry.Target = entryCheckList[i].target
entries = append(entries, entry)
}
return NewImg(entries, img.Layout())
}