-
Notifications
You must be signed in to change notification settings - Fork 7
/
processor.go
81 lines (66 loc) · 1.68 KB
/
processor.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
package content
import (
"time"
"github.com/op/go-logging"
)
var log = logging.MustGetLogger("content")
// Content processor definition.
type Processor func(deps, Parseable, tags) (Parseable, error)
type Preprocessor func(deps, Parseable) (Parseable, error)
// Postprocess a parseable type.
func Postprocess(d deps, c Parseable) (processed Parseable, err error) {
starts := time.Now()
list := parseTags(c)
pipeline := []Processor{
postReplaceMentionTags,
postReplaceAssetTags,
}
// Run pipeline over parseable.
processed = c
for _, fn := range pipeline {
processed, err = fn(d, processed, list)
if err != nil {
return
}
}
elapsed := time.Since(starts)
log.Debugf("postprocess content took=%v", elapsed)
return
}
// Preprocess a parseable type.
func Preprocess(d deps, c Parseable) (processed Parseable, err error) {
starts := time.Now()
pipeline := []Preprocessor{
preReplaceMentionTags,
preReplaceAssetTags,
}
// Run pipeline over parseable.
processed = c
for _, fn := range pipeline {
processed, err = fn(d, processed)
if err != nil {
return
}
}
elapsed := time.Since(starts)
log.Debugf("preprocess took = %v", elapsed)
return
}
func parseTags(c Parseable) (list []tag) {
// Use regex to find all tags inside the parseable content.
found := tagRegex.FindAllString(c.GetContent(), -1)
for _, match := range found {
// Having parsed all tags now destructure the tag params.
params := tagParamsRegex.FindAllString(match, -1)
count := len(params) - 1
for n, param := range params {
if n != count {
params[n] = param[:len(param)-1]
}
}
if len(params) > 0 {
list = append(list, tag{match, params[0], params[1:]})
}
}
return
}