-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck.go
More file actions
156 lines (144 loc) · 3.33 KB
/
Copy pathcheck.go
File metadata and controls
156 lines (144 loc) · 3.33 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strings"
)
const checkModeDefault = "default"
// Unclosed style blocks are scanned to EOF so edited or foreign files fail safe.
var inlineStyleRE = regexp.MustCompile(`(?is)<style\b[^>]*>(.*?)(?:</style\s*>|\z)`)
type CheckOptions struct {
Path string
Mode string
Stdout io.Writer
}
func CheckPath(opts CheckOptions) error {
mode, err := normalizeCheckMode(opts.Mode)
if err != nil {
return err
}
if opts.Path == "" {
return fmt.Errorf("check path is required")
}
if opts.Stdout == nil {
opts.Stdout = io.Discard
}
info, err := os.Stat(opts.Path)
if err != nil {
return err
}
checked := 0
failed := 0
var firstFailure error
checkFile := func(path string) error {
err := checkOneFile(path, mode)
checked++
if err != nil {
failed++
if firstFailure == nil {
firstFailure = err
}
fmt.Fprintf(opts.Stdout, "fail: %s: %s\n", cleanSlash(path), err)
return nil
}
fmt.Fprintf(opts.Stdout, "ok: %s\n", cleanSlash(path))
return nil
}
if !info.IsDir() {
if !isCheckableFile(opts.Path) {
return fmt.Errorf("unsupported file extension: %s", opts.Path)
}
if err := checkFile(opts.Path); err != nil {
return err
}
} else {
err = filepath.WalkDir(opts.Path, func(path string, entry os.DirEntry, err error) error {
if err != nil {
checked++
failed++
if firstFailure == nil {
firstFailure = err
}
fmt.Fprintf(opts.Stdout, "fail: %s: %s\n", cleanSlash(path), err)
return nil
}
if entry.IsDir() {
return nil
}
if !isCheckableFile(path) {
return nil
}
return checkFile(path)
})
if err != nil {
return err
}
}
if checked == 0 {
return fmt.Errorf("no checkable files found: %s", opts.Path)
}
if failed > 0 {
return fmt.Errorf("%d file(s) failed check; first failure: %w", failed, firstFailure)
}
return nil
}
func normalizeCheckMode(mode string) (string, error) {
if mode == "" || mode == checkModeDefault {
return checkModeDefault, nil
}
if mode == outputModeFlatXHTML || mode == outputModeFlatGemini {
return mode, nil
}
return "", fmt.Errorf("invalid check mode: %s", mode)
}
func isCheckableFile(path string) bool {
switch strings.ToLower(filepath.Ext(path)) {
case ".html", ".xhtml", ".gmi":
return true
default:
return false
}
}
func checkOneFile(path, mode string) error {
data, err := os.ReadFile(path)
if err != nil {
return err
}
ext := strings.ToLower(filepath.Ext(path))
if ext == ".gmi" {
return ValidateGemtext(string(data))
}
if mode == outputModeFlatGemini {
return fmt.Errorf("flat-gemini-v1 checks only .gmi files")
}
return CheckHTML(string(data), mode)
}
func CheckHTML(html, mode string) error {
if err := ValidateHTMLForMode(html, mode); err != nil {
if mode == checkModeDefault && ValidateHTMLForMode(html, outputModeFlatXHTML) == nil {
return fmt.Errorf("%w; use --mode flat-xhtml-v1 for flat XHTML output", err)
}
return err
}
for i, match := range inlineStyleRE.FindAllStringSubmatch(html, -1) {
if len(match) < 2 {
continue
}
if err := ValidateCSS(match[1]); err != nil {
return fmt.Errorf("inline style %d: %w", i+1, err)
}
}
m, ok, err := ExtractManifest(html)
if err != nil {
return err
}
if ok {
if err := compareDeclaredRegionHashes(m.Regions, html); err != nil {
return err
}
}
return nil
}