• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / modo / 12777767016

14 Jan 2025 10:46PM CUT coverage: 34.058% (+0.2%) from 33.894%
12777767016

Pull #39

github

web-flow
Merge 12e54bf0f into efc472466
Pull Request #39: Move the binary package to module root

0 of 60 new or added lines in 3 files covered. (0.0%)

282 of 828 relevant lines covered (34.06%)

2.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

30.28
/document/links.go
1
package document
2

3
import (
4
        "fmt"
5
        "log"
6
        "path"
7
        "regexp"
8
        "strings"
9
)
10

11
const regexString = `(?s)(?:(` + "```.*?```)|(`.*?`" + `))|(\[.*?\])`
12

13
func findLinks(text string) ([]int, error) {
2✔
14
        re, err := regexp.Compile(regexString)
2✔
15
        if err != nil {
2✔
16
                return nil, err
×
17
        }
×
18
        links := []int{}
2✔
19
        results := re.FindAllStringSubmatchIndex(text, -1)
2✔
20
        for _, r := range results {
12✔
21
                if r[6] >= 0 {
18✔
22
                        if len(text) > r[7] && string(text[r[7]]) == "(" {
9✔
23
                                continue
1✔
24
                        }
25
                        links = append(links, r[6], r[7])
7✔
26
                }
27
        }
28

29
        return links, nil
2✔
30
}
31

32
func (proc *Processor) ProcessLinks(doc *Docs) error {
×
33
        lookup := proc.collectPaths(doc)
×
34
        //for k, v := range lookup {
×
35
        //        fmt.Println(k, v.Elements)
×
36
        //}
×
37
        return proc.processLinksPackage(doc.Decl, []string{}, lookup)
×
38
}
×
39

40
func (proc *Processor) processLinksPackage(p *Package, elems []string, lookup map[string]elemPath) error {
×
41
        newElems := appendNew(elems, p.GetName())
×
42

×
43
        var err error
×
44
        p.Summary, err = proc.replaceLinks(p.Summary, newElems, len(newElems), lookup)
×
45
        if err != nil {
×
46
                return err
×
47
        }
×
48
        p.Description, err = proc.replaceLinks(p.Description, newElems, len(newElems), lookup)
×
49
        if err != nil {
×
50
                return err
×
51
        }
×
52

53
        for _, pkg := range p.Packages {
×
54
                proc.processLinksPackage(pkg, newElems, lookup)
×
55
        }
×
56
        for _, mod := range p.Modules {
×
57
                proc.processLinksModule(mod, newElems, lookup)
×
58
        }
×
59

60
        return nil
×
61
}
62

63
func (proc *Processor) processLinksModule(m *Module, elems []string, lookup map[string]elemPath) error {
×
64
        newElems := appendNew(elems, m.GetName())
×
65

×
66
        var err error
×
67
        m.Summary, err = proc.replaceLinks(m.Summary, newElems, len(newElems), lookup)
×
68
        if err != nil {
×
69
                return err
×
70
        }
×
71
        m.Description, err = proc.replaceLinks(m.Description, newElems, len(newElems), lookup)
×
72
        if err != nil {
×
73
                return err
×
74
        }
×
75

76
        for _, f := range m.Functions {
×
77
                err := proc.processLinksFunction(f, newElems, lookup)
×
78
                if err != nil {
×
79
                        return err
×
80
                }
×
81
        }
82
        for _, s := range m.Structs {
×
83
                err := proc.processLinksStruct(s, newElems, lookup)
×
84
                if err != nil {
×
85
                        return err
×
86
                }
×
87
        }
88
        for _, tr := range m.Traits {
×
89
                err := proc.processLinksTrait(tr, newElems, lookup)
×
90
                if err != nil {
×
91
                        return err
×
92
                }
×
93
        }
94

95
        return nil
×
96
}
97

98
func (proc *Processor) processLinksStruct(s *Struct, elems []string, lookup map[string]elemPath) error {
×
99
        newElems := appendNew(elems, s.GetName())
×
100

×
101
        var err error
×
102
        s.Summary, err = proc.replaceLinks(s.Summary, newElems, len(elems), lookup)
×
103
        if err != nil {
×
104
                return err
×
105
        }
×
106
        s.Description, err = proc.replaceLinks(s.Description, newElems, len(elems), lookup)
×
107
        if err != nil {
×
108
                return err
×
109
        }
×
110

111
        for _, p := range s.Parameters {
×
112
                p.Description, err = proc.replaceLinks(p.Description, newElems, len(elems), lookup)
×
113
                if err != nil {
×
114
                        return err
×
115
                }
×
116
        }
117
        for _, f := range s.Fields {
×
118
                f.Summary, err = proc.replaceLinks(f.Summary, newElems, len(elems), lookup)
×
119
                if err != nil {
×
120
                        return err
×
121
                }
×
122
                f.Description, err = proc.replaceLinks(f.Description, newElems, len(elems), lookup)
×
123
                if err != nil {
×
124
                        return err
×
125
                }
×
126
        }
127
        for _, f := range s.Functions {
×
128
                if err := proc.processLinksMethod(f, elems, lookup); err != nil {
×
129
                        return err
×
130
                }
×
131
        }
132

133
        return nil
×
134
}
135

136
func (proc *Processor) processLinksTrait(tr *Trait, elems []string, lookup map[string]elemPath) error {
×
137
        newElems := appendNew(elems, tr.GetName())
×
138

×
139
        var err error
×
140
        tr.Summary, err = proc.replaceLinks(tr.Summary, newElems, len(elems), lookup)
×
141
        if err != nil {
×
142
                return err
×
143
        }
×
144
        tr.Description, err = proc.replaceLinks(tr.Description, newElems, len(elems), lookup)
×
145
        if err != nil {
×
146
                return err
×
147
        }
×
148

149
        // TODO: add when traits support parameters
150
        /*for _, p := range tr.Parameters {
151
                p.Description, err = replaceLinks(p.Description, newElems, len(elems), lookup, t)
152
                if err != nil {
153
                        return err
154
                }
155
        }*/
156
        for _, f := range tr.Fields {
×
157
                f.Summary, err = proc.replaceLinks(f.Summary, newElems, len(elems), lookup)
×
158
                if err != nil {
×
159
                        return err
×
160
                }
×
161
                f.Description, err = proc.replaceLinks(f.Description, newElems, len(elems), lookup)
×
162
                if err != nil {
×
163
                        return err
×
164
                }
×
165
        }
166
        for _, f := range tr.Functions {
×
167
                if err := proc.processLinksMethod(f, elems, lookup); err != nil {
×
168
                        return err
×
169
                }
×
170
        }
171

172
        return nil
×
173
}
174

175
func (proc *Processor) processLinksFunction(f *Function, elems []string, lookup map[string]elemPath) error {
×
176
        newElems := appendNew(elems, f.GetName())
×
177

×
178
        var err error
×
179
        f.Summary, err = proc.replaceLinks(f.Summary, newElems, len(elems), lookup)
×
180
        if err != nil {
×
181
                return err
×
182
        }
×
183
        f.Description, err = proc.replaceLinks(f.Description, newElems, len(elems), lookup)
×
184
        if err != nil {
×
185
                return err
×
186
        }
×
187
        f.ReturnsDoc, err = proc.replaceLinks(f.ReturnsDoc, newElems, len(elems), lookup)
×
188
        if err != nil {
×
189
                return err
×
190
        }
×
191
        f.RaisesDoc, err = proc.replaceLinks(f.RaisesDoc, newElems, len(elems), lookup)
×
192
        if err != nil {
×
193
                return err
×
194
        }
×
195

196
        for _, a := range f.Args {
×
197
                a.Description, err = proc.replaceLinks(a.Description, newElems, len(elems), lookup)
×
198
                if err != nil {
×
199
                        return err
×
200
                }
×
201
        }
202
        for _, p := range f.Parameters {
×
203
                p.Description, err = proc.replaceLinks(p.Description, newElems, len(elems), lookup)
×
204
                if err != nil {
×
205
                        return err
×
206
                }
×
207
        }
208

209
        for _, o := range f.Overloads {
×
210
                err := proc.processLinksFunction(o, elems, lookup)
×
211
                if err != nil {
×
212
                        return err
×
213
                }
×
214
        }
215

216
        return nil
×
217
}
218

219
func (proc *Processor) processLinksMethod(f *Function, elems []string, lookup map[string]elemPath) error {
×
220
        var err error
×
221
        f.Summary, err = proc.replaceLinks(f.Summary, elems, len(elems), lookup)
×
222
        if err != nil {
×
223
                return err
×
224
        }
×
225
        f.Description, err = proc.replaceLinks(f.Description, elems, len(elems), lookup)
×
226
        if err != nil {
×
227
                return err
×
228
        }
×
229
        f.ReturnsDoc, err = proc.replaceLinks(f.ReturnsDoc, elems, len(elems), lookup)
×
230
        if err != nil {
×
231
                return err
×
232
        }
×
233
        f.RaisesDoc, err = proc.replaceLinks(f.RaisesDoc, elems, len(elems), lookup)
×
234
        if err != nil {
×
235
                return err
×
236
        }
×
237

238
        for _, a := range f.Args {
×
239
                a.Description, err = proc.replaceLinks(a.Description, elems, len(elems), lookup)
×
240
                if err != nil {
×
241
                        return err
×
242
                }
×
243
        }
244
        for _, p := range f.Parameters {
×
245
                p.Description, err = proc.replaceLinks(p.Description, elems, len(elems), lookup)
×
246
                if err != nil {
×
247
                        return err
×
248
                }
×
249
        }
250

251
        for _, o := range f.Overloads {
×
252
                err := proc.processLinksMethod(o, elems, lookup)
×
253
                if err != nil {
×
254
                        return err
×
255
                }
×
256
        }
257

258
        return nil
×
259
}
260

261
func (proc *Processor) replaceLinks(text string, elems []string, modElems int, lookup map[string]elemPath) (string, error) {
1✔
262
        indices, err := findLinks(text)
1✔
263
        if err != nil {
1✔
264
                return "", err
×
265
        }
×
266
        if len(indices) == 0 {
1✔
267
                return text, nil
×
268
        }
×
269
        for i := len(indices) - 2; i >= 0; i -= 2 {
6✔
270
                start, end := indices[i], indices[i+1]
5✔
271
                link := text[start+1 : end-1]
5✔
272

5✔
273
                entry, linkText, parts, ok := toLink(link, elems, modElems, lookup)
5✔
274
                if !ok {
5✔
275
                        continue
×
276
                }
277

278
                var basePath string
5✔
279
                if entry.IsSection {
6✔
280
                        basePath = path.Join(parts[:len(parts)-1]...)
1✔
281
                } else {
5✔
282
                        basePath = path.Join(parts...)
4✔
283
                }
4✔
284
                pathStr, err := proc.Formatter.ToLinkPath(basePath, entry.Kind)
5✔
285
                if err != nil {
5✔
286
                        return "", err
×
287
                }
×
288
                if entry.IsSection {
6✔
289
                        pathStr += parts[len(parts)-1]
1✔
290
                }
1✔
291
                text = fmt.Sprintf("%s[%s](%s)%s", text[:start], linkText, pathStr, text[end:])
5✔
292
        }
293
        return text, nil
1✔
294
}
295

296
func toLink(link string, elems []string, modElems int, lookup map[string]elemPath) (entry *elemPath, text string, parts []string, ok bool) {
16✔
297
        linkParts := strings.SplitN(link, " ", 2)
16✔
298
        if strings.HasPrefix(link, ".") {
25✔
299
                entry, text, parts, ok = toRelLink(linkParts[0], elems, modElems, lookup)
9✔
300
        } else {
16✔
301
                entry, text, parts, ok = toAbsLink(linkParts[0], elems, modElems, lookup)
7✔
302
        }
7✔
303
        if len(linkParts) > 1 {
17✔
304
                text = linkParts[1]
1✔
305
        } else {
16✔
306
                text = fmt.Sprintf("`%s`", text)
15✔
307
        }
15✔
308
        return
16✔
309
}
310

311
func toRelLink(link string, elems []string, modElems int, lookup map[string]elemPath) (*elemPath, string, []string, bool) {
9✔
312
        dots := 0
9✔
313
        fullPath := []string{}
9✔
314
        for strings.HasPrefix(link[dots:], ".") {
21✔
315
                if dots > 0 {
15✔
316
                        fullPath = append(fullPath, "..")
3✔
317
                }
3✔
318
                dots++
12✔
319
        }
320
        if dots > modElems {
9✔
321
                log.Printf("WARNING: Too many leading dots in cross ref '%s' in %s", link, strings.Join(elems, "."))
×
322
                return nil, "", nil, false
×
323
        }
×
324
        linkText := link[dots:]
9✔
325
        subElems := elems[:modElems-(dots-1)]
9✔
326
        var fullLink string
9✔
327
        if len(subElems) == 0 {
9✔
328
                fullLink = linkText
×
329
        } else {
9✔
330
                fullLink = strings.Join(subElems, ".") + "." + linkText
9✔
331
        }
9✔
332

333
        elemPath, ok := lookup[fullLink]
9✔
334
        if !ok {
9✔
335
                log.Printf("WARNING: Can't resolve cross ref '%s' (%s) in %s", link, fullLink, strings.Join(elems, "."))
×
336
                return nil, "", nil, false
×
337
        }
×
338

339
        fullPath = append(fullPath, elemPath.Elements[len(subElems):]...)
9✔
340
        return &elemPath, link[dots:], fullPath, true
9✔
341
}
342

343
func toAbsLink(link string, elems []string, modElems int, lookup map[string]elemPath) (*elemPath, string, []string, bool) {
7✔
344
        elemPath, ok := lookup[link]
7✔
345
        if !ok {
8✔
346
                log.Printf("WARNING: Can't resolve cross ref '%s' in %s", link, strings.Join(elems, "."))
1✔
347
                return nil, "", nil, false
1✔
348
        }
1✔
349
        skip := 0
6✔
350
        for range modElems {
18✔
351
                if len(elemPath.Elements) <= skip {
12✔
352
                        break
×
353
                }
354
                if elemPath.Elements[skip] == elems[skip] {
21✔
355
                        skip++
9✔
356
                } else {
12✔
357
                        break
3✔
358
                }
359
        }
360
        fullPath := []string{}
6✔
361
        for range modElems - skip {
9✔
362
                fullPath = append(fullPath, "..")
3✔
363
        }
3✔
364
        fullPath = append(fullPath, elemPath.Elements[skip:]...)
6✔
365
        return &elemPath, link, fullPath, true
6✔
366
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc