This repository was archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathcheck.go
118 lines (96 loc) · 2.33 KB
/
check.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
//
// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"errors"
"fmt"
)
// checkLink checks the validity of the specified link. If checkOtherDoc is
// true and the link is an external one, validate the link by considering the
// external document too.
func (d *Doc) checkLink(address string, link Link, checkOtherDoc bool) error {
if address == "" {
return errors.New("link address not set")
}
switch link.Type {
case externalFile:
fallthrough
case externalLink:
// Check to ensure that referenced file actually exists
var file string
if link.ResolvedPath != "" {
file = link.ResolvedPath
} else {
file, _, err := splitLink(address)
if err != nil {
return err
}
file, err = d.linkAddrToPath(file)
if err != nil {
return err
}
if !fileExists(file) {
return d.Errorf("link type %v invalid: %q does not exist",
link.Type,
file)
}
}
if link.Type == externalFile {
break
}
// Check the other document
other, err := getDoc(file, d.Logger)
if err != nil {
return err
}
if !checkOtherDoc {
break
}
_, section, err := splitLink(address)
if err != nil {
return err
}
if section == "" {
break
}
if !other.hasHeading(section) {
return other.Errorf("invalid link %v", address)
}
case internalLink:
// must be a link to an existing heading
// search for a heading whose LinkName == name
found := d.headingByLinkName(address)
if found == nil {
msg := fmt.Sprintf("failed to find heading for link %q (%+v)", address, link)
// There is a chance the link description matches the
// correct heading the link address refers to. In
// which case, we can derive the correct link address!
suggestion, err2 := createHeadingID(link.Description)
if err2 == nil && suggestion != link.Address {
found = d.headingByLinkName(suggestion)
if found != nil {
msg = fmt.Sprintf("%s - correct link name is %q", msg, suggestion)
}
}
return d.Errorf("%s", msg)
}
case urlLink:
// NOP - handled by xurls
}
return nil
}
// check performs all checks on the document.
func (d *Doc) check() error {
for name, linkList := range d.Links {
for _, link := range linkList {
err := d.checkLink(name, link, false)
if err != nil {
return err
}
}
}
return nil
}