-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
162 lines (136 loc) · 3.02 KB
/
main.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
package main
import (
"archive/zip"
"bytes"
"encoding/xml"
"flag"
"fmt"
"html"
"io"
"io/ioutil"
"log"
"os"
"regexp"
"strings"
)
type Node struct {
XMLName xml.Name
Attrs []xml.Attr `xml:"-"`
Content []byte `xml:",innerxml"`
Nodes []Node `xml:",any"`
}
const (
FNAME = "pptxgrep"
VERSION = "0.0.2"
SLIDE_PATH_PREFIX = "ppt/slides/slide"
)
func usage() {
fmt.Printf("Usage:\n %s [options] pattern pptx1 [pptx2 ... pptxN]\n\n", FNAME)
fmt.Printf("Version:\n %s\n\n", VERSION)
fmt.Printf("Options:\n")
flag.PrintDefaults()
}
func extractLocation(filePath, prefix string) string {
return strings.TrimRight(strings.TrimLeft(filePath, prefix), ".xml")
}
func colorize(targetStr string, pattern *regexp.Regexp) string {
var buf bytes.Buffer
matchedIdx := pattern.FindAllStringSubmatchIndex(targetStr, -1)
startIdx := 0
for _, startEnd := range matchedIdx {
matchedStart := startEnd[0]
matchedEnd := startEnd[1]
fmt.Fprintf(
&buf,
"%s\x1b[31m%s\x1b[0m",
string([]byte(targetStr)[startIdx:matchedStart]),
string([]byte(targetStr)[matchedStart:matchedEnd]))
startIdx = matchedEnd
}
if len(targetStr) > startIdx {
fmt.Fprint(&buf, string([]byte(targetStr)[startIdx:]))
}
return buf.String()
}
func walk(node *Node, w io.Writer) error {
switch node.XMLName.Local {
case "t":
fmt.Fprintf(w, string(node.Content))
default:
for _, n := range node.Nodes {
if err := walk(&n, w); err != nil {
return err
}
}
}
return nil
}
func pptxgrep(pattern *regexp.Regexp, arg string, color bool) error {
r, err := zip.OpenReader(arg)
if err != nil {
return err
}
defer r.Close()
for _, f := range r.File {
var node Node
var buf bytes.Buffer
if strings.HasPrefix(f.Name, SLIDE_PATH_PREFIX) {
rc, err := f.Open()
if err != nil {
return err
}
defer rc.Close()
b, err := ioutil.ReadAll(rc)
if err != nil {
return err
}
err = xml.Unmarshal(b, &node)
if err != nil {
return err
}
err = walk(&node, &buf)
if err != nil {
return err
}
unescapedString := html.UnescapeString(buf.String())
if pattern.MatchString(unescapedString) {
if color {
unescapedString = colorize(unescapedString, pattern)
}
fmt.Printf("%s:%s:%s\n", arg, extractLocation(f.Name, SLIDE_PATH_PREFIX), unescapedString)
}
}
}
return nil
}
func main() {
var version bool
var color bool
var ignoreCase bool
flag.BoolVar(&version, "version", false, "print version")
flag.BoolVar(&color, "color", false, "colorize matched pattern")
flag.BoolVar(&ignoreCase, "i", false, "ignore case when matching pattern")
flag.Usage = usage
flag.Parse()
if flag.NArg() <= 1 {
if version {
fmt.Printf("Version: %s\n", VERSION)
} else {
flag.Usage()
}
os.Exit(0)
}
var pattern *regexp.Regexp
for i, arg := range flag.Args() {
if i == 0 {
if ignoreCase {
arg = `(?i)` + arg
}
pattern = regexp.MustCompile(arg)
} else {
if err := pptxgrep(pattern, arg, color); err != nil {
log.Fatal(err)
}
}
}
}