Repository navigation
Expand file tree
/
Copy pathdocument.go
More file actions
76 lines (63 loc) · 1.47 KB
/
Copy pathdocument.go
File metadata and controls
76 lines (63 loc) · 1.47 KB
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
package publiccode
import (
"bytes"
"github.com/goccy/go-yaml/ast"
)
// notMappingError reports a document whose body is not a mapping of
// keys, or nil when it is one. body is the document body, b the content
// it was parsed from.
func notMappingError(body ast.Node, b []byte) error {
node := body
for {
switch n := node.(type) {
case *ast.TagNode:
node = n.Value
continue
case *ast.AnchorNode:
node = n.Value
continue
case ast.MapNode:
return nil
}
break
}
if err := htmlPageError(b); err != nil {
return err
}
tok := body.GetToken()
return ValidationError{
Key: "",
Description: "the file is not a YAML mapping of publiccode.yml keys",
Line: tok.Position.Line,
Column: tok.Position.Column,
}
}
// htmlPageError reports content that starts with an HTML tag, such as
// the page a code hosting site shows for a file, or nil.
func htmlPageError(b []byte) error {
line, column := 1, 1
for i, c := range b {
switch c {
case '\n':
line++
column = 1
case ' ', '\t', '\r':
column++
case '<':
// "<<" is a merge key, not the start of a tag.
if bytes.HasPrefix(b[i:], []byte("<<")) {
return nil
}
return ValidationError{
Key: "",
Description: "the file is not a YAML mapping of publiccode.yml keys but looks like an HTML page: " +
"use the URL of the raw file, not of the page that shows it",
Line: line,
Column: column,
}
default:
return nil
}
}
return nil
}