This repository has been archived by the owner on Aug 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpipeline.go
184 lines (158 loc) · 3.86 KB
/
pipeline.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
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
package search
import (
"bytes"
"io"
"os"
"path"
"strings"
"time"
"github.com/microcosm-cc/bluemonday"
"github.com/pedronasser/caddy-search/indexer"
"github.com/pedronasser/go-piper"
"golang.org/x/net/html"
)
var bm = bluemonday.UGCPolicy()
// NewPipeline creates a new Pipeline instance
func NewPipeline(config *Config, indxr indexer.Handler) (*Pipeline, error) {
ppl := &Pipeline{
config: config,
indexer: indxr,
}
pipe, err := piper.New(
piper.P(1, ppl.read),
piper.P(1, ppl.validate),
piper.P(1, ppl.parse),
piper.P(1, ppl.index),
)
if err != nil {
return nil, err
}
ppl.pipe = pipe
go func() {
tick := time.NewTicker(100 * time.Millisecond)
out := pipe.Output()
for {
select {
case in := <-out:
if record, ok := in.(indexer.Record); ok {
if record.Ignored() {
ppl.indexer.Kill(record)
}
}
case <-tick.C:
}
}
}()
return ppl, nil
}
// Pipeline is the structure that holds search's pipeline infos and methods
type Pipeline struct {
config *Config
indexer indexer.Handler
pipe piper.Handler
}
// Pipe is the step of the pipeline that pipes valid documents to the indexer.
func (p *Pipeline) Pipe(record indexer.Record) {
p.pipe.Input() <- record
}
// Piper is a func that returns the piper.Handler
func (p *Pipeline) Piper() piper.Handler {
return p.pipe
}
// validate is the step of the pipeline that reads the file content
func (p *Pipeline) read(in interface{}) interface{} {
if record, ok := in.(indexer.Record); ok && !record.Ignored() {
in, err := os.Open(record.FullPath())
defer in.Close()
if err != nil {
record.Ignore()
} else {
io.Copy(record, in)
}
}
return in
}
// validate is the step of the pipeline that checks if documents are valid for
// being indexed
func (p *Pipeline) validate(in interface{}) interface{} {
if record, ok := in.(indexer.Record); ok && !record.Ignored() {
if !p.ValidatePath(record.Path()) {
record.Ignore()
}
}
return in
}
var titleTag = []byte("title")
// parse is the step of the pipeline that tries to parse documents and get
// important information
func (p *Pipeline) parse(in interface{}) interface{} {
if record, ok := in.(indexer.Record); ok && !record.Ignored() {
if strings.HasSuffix(record.Path(), ".txt") || strings.HasSuffix(record.Path(), ".md") {
// TODO: We can improve file type detection; this is a very limited subset of indexable file types
// text or markdown file
record.SetTitle(path.Base(record.Path()))
} else {
body := bytes.NewReader(record.Body())
title, err := getHTMLContent(body, titleTag)
if err == nil {
// html file
record.SetTitle(title)
stripped := bm.SanitizeBytes(record.Body())
record.SetBody(stripped)
} else {
record.Ignore()
}
}
}
return in
}
func getHTMLContent(r io.Reader, tag []byte) (result string, err error) {
z := html.NewTokenizer(r)
result = ""
valid := 0
cacheLen := len(tag)
for {
tt := z.Next()
switch tt {
case html.ErrorToken:
err = z.Err()
return
case html.TextToken:
if valid == 1 {
return string(z.Text()), nil
}
case html.StartTagToken, html.EndTagToken:
tn, _ := z.TagName()
if len(tn) == cacheLen && bytes.Equal(tn[0:cacheLen], tag) {
if tt == html.StartTagToken {
valid = 1
} else {
valid = 0
}
}
}
}
}
// index is the step of the pipeline that pipes valid documents to the indexer.
func (p *Pipeline) index(in interface{}) interface{} {
if record, ok := in.(indexer.Record); ok {
if !record.Ignored() {
p.indexer.Pipe(record)
}
}
return in
}
// ValidatePath is the method that checks if the target page can be indexed
func (p *Pipeline) ValidatePath(path string) bool {
for _, pa := range p.config.ExcludePaths {
if pa.MatchString(path) {
return false
}
}
for _, pa := range p.config.IncludePaths {
if pa.MatchString(path) {
return true
}
}
return false
}