/
processor.go
64 lines (48 loc) · 1.19 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
package xml
import (
"github.com/mateussouzaweb/compactor/compactor"
"github.com/mateussouzaweb/compactor/os"
"github.com/mateussouzaweb/compactor/pkg/generic"
"github.com/tdewolff/minify"
"github.com/tdewolff/minify/xml"
)
// XML minify
func Minify(content string) (string, error) {
m := minify.New()
m.AddFunc("generic", xml.Minify)
content, err := m.String("generic", content)
return content, err
}
// XML processor
func RunProcessor(bundle *compactor.Bundle) error {
// TODO: to multiple, merge xmls as array and join data
for _, item := range bundle.Items {
if !item.Exists {
continue
}
content := item.Content
var err error
if bundle.ShouldCompress(item.Path) {
content, err = Minify(content)
if err != nil {
return err
}
}
destination := bundle.ToDestination(item.Path)
err = os.Write(destination, content, item.Permission)
if err != nil {
return err
}
bundle.Processed(item.Path)
}
return nil
}
func Plugin() *compactor.Plugin {
return &compactor.Plugin{
Extensions: []string{".xml"},
Init: generic.InitProcessor,
Run: RunProcessor,
Delete: generic.DeleteProcessor,
Resolve: generic.ResolveProcessor,
}
}