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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
package index
import (
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"slices"
"strconv"
"strings"
)
type DocFilter struct {
Name string
Filter func(InfoPath, io.ReadSeeker) bool
}
const FilterHelp string = `
YAMLHeader - reject files without YAML header
Ext,Extension_<ext> - accept files ending with <ext>
MaxSize,MaxFilesize_<size> - accept files of at most <size> bytes
ExcludeName,ExcludeFilename_<name1>,...,<nameN> - reject files with names in list
IncludeName,IncludeFilename_<name1>,...,<nameN> - accept files with names in list
ExcludeParent_<dir> - reject files if <dir> is a parent directory
IncludeRegex_<pattern> - accept files whose path matches <pattern>
ExcludeRegex_<pattern> - reject files whose path matches <pattern>`
func ParseFilter(s string) (DocFilter, error) {
name, param, found := strings.Cut(s, "_")
// paramless filters
if name == "YAMLHeader" {
return YamlHeaderFilter, nil
}
if !found {
return DocFilter{}, fmt.Errorf("Expected parameter with filter %s", name)
}
switch name {
case "Ext", "Extension":
return NewExtensionFilter(param), nil
case "MaxSize", "MaxFilesize":
size, err := strconv.ParseInt(param, 10, 64)
if err != nil {
return DocFilter{}, err
}
return NewMaxFilesizeFilter(size), nil
case "ExcludeName", "ExcludeFilename":
return NewExcludeFilenameFilter(strings.Split(param, ",")), nil
case "IncludeName", "IncludeFilename":
return NewIncludeFilenameFilter(strings.Split(param, ",")), nil
case "ExcludeParent":
return NewExcludeParentFilter(param), nil
case "IncludeRegex":
filter, err := NewIncludeRegexFilter(param)
if err != nil {
return DocFilter{}, err
}
return filter, nil
case "ExcludeRegex":
filter, err := NewIncludeRegexFilter(param)
if err != nil {
return DocFilter{}, err
}
return filter, nil
default:
return DocFilter{}, fmt.Errorf("Unrecognized filter %s, see FILTERS", s)
}
}
func NewExtensionFilter(ext string) DocFilter {
return DocFilter{
ext + " Filter",
func(ip InfoPath, _ io.ReadSeeker) bool {
return filepath.Ext(ip.Path) == ext
},
}
}
func NewMaxFilesizeFilter(size int64) DocFilter {
return DocFilter{
fmt.Sprintf("Max Size Filter %d", size),
func(ip InfoPath, _ io.ReadSeeker) bool {
return ip.Info.Size() <= size
},
}
}
func NewExcludeFilenameFilter(excluded []string) DocFilter {
return DocFilter{
"Excluded Filename filter",
func(ip InfoPath, _ io.ReadSeeker) bool {
filename := filepath.Base(ip.Path)
return !slices.Contains(excluded, filename)
},
}
}
func NewIncludeFilenameFilter(included []string) DocFilter {
return DocFilter{
"Included Filename filter",
func(ip InfoPath, _ io.ReadSeeker) bool {
filename := filepath.Base(ip.Path)
return slices.Contains(included, filename)
},
}
}
// exclude files if it has a parent directory badParent
func NewExcludeParentFilter(badParent string) DocFilter {
return DocFilter{
"Excluded Parent Directory filter: " + badParent,
func(ip InfoPath, _ io.ReadSeeker) bool {
return !slices.Contains(strings.Split(ip.Path, string(os.PathSeparator)), badParent)
},
}
}
func NewIncludeRegexFilter(pattern string) (DocFilter, error) {
re, err := regexp.Compile(pattern)
if err != nil {
return DocFilter{}, fmt.Errorf("Cannot compile regex: %v", err)
}
return DocFilter{
"Included Regex Filter: " + pattern,
func(ip InfoPath, _ io.ReadSeeker) bool {
return re.MatchString(ip.Path)
},
}, nil
}
func NewExcludeRegexFilter(pattern string) (DocFilter, error) {
re, err := regexp.Compile(pattern)
if err != nil {
return DocFilter{}, fmt.Errorf("Cannot compile regex: %v", err)
}
return DocFilter{
"Excluded Regex Filter: " + pattern,
func(ip InfoPath, _ io.ReadSeeker) bool {
return !re.MatchString(ip.Path)
},
}, nil
}
var YamlHeaderFilter = DocFilter{
"YAML Header Filter",
func(_ InfoPath, rs io.ReadSeeker) bool {
return YamlHeaderPos(rs) > 0
},
}
// Position of the end of a yaml header, negative
func YamlHeaderPos(r io.ReadSeeker) int64 {
const bufSize = 4096
buf := make([]byte, bufSize)
carry := make([]byte, 4)
cmp := make([]byte, 4)
n, err := r.Read(carry)
if err != nil || n < 4 || string(carry) != "---\n" {
return -1
}
pos := int64(3)
headerFound := false
readMore := true
for readMore {
buf = buf[:bufSize]
n, err := r.Read(buf)
if err == io.EOF {
readMore = false
} else if err != nil {
return -1
}
buf = buf[:n]
// PERF: the carry doesn't need to be checked on the first loop iteration
for i := range min(4, n) {
pos++
b := carry[i]
for j := range 4 {
if i+j < 4 {
cmp[j] = carry[i+j]
} else {
cmp[j] = buf[(i+j)%4]
}
}
if b == '\n' && string(cmp) == "\n---\n" {
headerFound = true
readMore = false
break
}
}
for i := range n - 4 {
pos++
b := buf[i]
if b == '\n' && string(buf[i:i+5]) == "\n---\n" {
headerFound = true
readMore = false
break
}
}
if readMore {
for i := range 4 {
carry[i] = buf[n-4+i]
}
}
}
if headerFound {
return pos
} else {
return -1
}
}
func DefaultFilters() []DocFilter {
return []DocFilter{NewExtensionFilter(".md"), NewMaxFilesizeFilter(200 * 1024), NewExcludeParentFilter("templates"), YamlHeaderFilter}
}
|