forked from rainycape/gondola
/
extract.go
371 lines (356 loc) · 9.45 KB
/
extract.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package messages
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template/parse"
"gnd.la/internal/astutil"
"gnd.la/internal/pkgutil"
"gnd.la/internal/templateutil"
"gnd.la/log"
)
func DefaultFunctions() []*Function {
return []*Function{
// Singular functions without context
{Name: "gnd.la/i18n.T", Start: 1},
{Name: "gnd.la/i18n.Errorf"},
{Name: "gnd.la/i18n.Sprintf", Start: 1},
{Name: "gnd.la/i18n.NewError"},
{Name: "gnd.la/app.Context.T"},
{Name: "t", Template: true},
// Singular functions with context
{Name: "gnd.la/i18n.Tc", Context: true, Start: 1},
{Name: "gnd.la/i18n.Sprintfc", Context: true, Start: 1},
{Name: "gnd.la/i18n.Errorfc", Context: true},
{Name: "gnd.la/i18n.NewErrorc", Context: true},
{Name: "gnd.la/app.Context.Tc", Context: true, Start: 1},
{Name: "tc", Template: true, Context: true},
// Plural functions without context
{Name: "gnd.la/i18n.Tn", Plural: true, Start: 1},
{Name: "gnd.la/i18n.Sprintfn", Plural: true, Start: 1},
{Name: "gnd.la/i18n.Errorfn", Plural: true},
{Name: "gnd.la/i18n.NewErrorn", Plural: true},
{Name: "gnd.la/app.Context.Tn", Plural: true},
{Name: "tn", Template: true, Plural: true},
// Plural functions with context
{Name: "gnd.la/i18n.Tnc", Context: true, Plural: true, Start: 1},
{Name: "gnd.la/i18n.Errorfnc", Context: true, Plural: true},
{Name: "gnd.la/i18n.Sprintfnc", Context: true, Plural: true, Start: 1},
{Name: "gnd.la/i18n.NewErrornc", Context: true, Plural: true},
{Name: "gnd.la/app.Context.Tnc", Context: true, Plural: true},
{Name: "tnc", Template: true, Context: true, Plural: true},
}
}
func DefaultTypes() []string {
return []string{
"gnd.la/i18n.String",
}
}
func DefaultTagFields() []string {
return []string{
"help",
"label",
"placeholder",
}
}
type ExtractOptions struct {
Functions []*Function
Types []string
TagFields []string
}
func DefaultExtractOptions() *ExtractOptions {
return &ExtractOptions{
Functions: DefaultFunctions(),
Types: DefaultTypes(),
TagFields: DefaultTagFields(),
}
}
func Extract(dir string, opts *ExtractOptions) ([]*Message, error) {
if opts == nil {
opts = DefaultExtractOptions()
}
messages := make(messageMap)
err := extract(messages, dir, opts)
if err != nil {
return nil, err
}
return messages.Messages(), nil
}
func extract(messages messageMap, dir string, opts *ExtractOptions) error {
f, err := os.Open(dir)
if err != nil {
return err
}
defer f.Close()
infos, err := f.Readdir(-1)
if err != nil {
return err
}
for _, v := range infos {
name := v.Name()
p := filepath.Join(dir, name)
if v.IsDir() {
if !pkgutil.IsPackage(p) {
if err := extract(messages, p, opts); err != nil {
return err
}
}
continue
}
switch strings.ToLower(filepath.Ext(name)) {
// TODO: text and strings files
case ".html", ".txt":
if err := extractTemplateMessages(messages, p, opts); err != nil {
return err
}
case ".go":
if err := extractGoMessages(messages, p, opts); err != nil {
return err
}
case ".po", ".pot":
// Do nothing
}
}
return nil
}
func extractGoMessages(messages messageMap, path string, opts *ExtractOptions) error {
log.Debugf("Extracting messages from Go file %s", path)
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
if err != nil {
return fmt.Errorf("error parsing go file %s: %s", path, err)
}
if opts != nil {
for _, v := range opts.Functions {
if v.Template {
continue
}
if err := extractGoFunc(messages, fset, f, v); err != nil {
return err
}
}
for _, v := range opts.Types {
if err := extractGoType(messages, fset, f, v); err != nil {
return err
}
}
for _, v := range opts.TagFields {
if err := extractGoTagField(messages, fset, f, v); err != nil {
return err
}
}
}
return nil
}
func extractGoFunc(messages messageMap, fset *token.FileSet, f *ast.File, fn *Function) error {
calls, err := astutil.Calls(fset, f, fn.Name)
if err != nil {
return err
}
n := fn.Start
if fn.Context {
n++
}
var message *Message
var position *token.Position
for _, c := range calls {
if fn.Plural {
if len(c.Args) < n+3 {
log.Debugf("Skipping plural function %s (%v) - not enough arguments", astutil.Ident(c.Fun), fset.Position(c.Pos()))
continue
}
slit, spos := astutil.StringLiteral(fset, c.Args[n])
if slit == "" || spos == nil {
log.Debugf("Skipping first argument to plural function %s (%v) - not a literal", astutil.Ident(c.Fun), fset.Position(c.Pos()))
continue
}
plit, ppos := astutil.StringLiteral(fset, c.Args[n+1])
if plit == "" || ppos == nil {
log.Debugf("Skipping second argument to plural function %s (%v) - not a literal", astutil.Ident(c.Fun), fset.Position(c.Pos()))
continue
}
message = &Message{
Singular: slit,
Plural: plit,
}
position = spos
} else {
if len(c.Args) < n+1 {
log.Debugf("Skipping singular function %s (%v) - not enough arguments", astutil.Ident(c.Fun), fset.Position(c.Pos()))
continue
}
lit, pos := astutil.StringLiteral(fset, c.Args[n])
if lit == "" || pos == nil {
log.Debugf("Skipping argument to singular function %s (%v) - not a literal", astutil.Ident(c.Fun), fset.Position(c.Pos()))
continue
}
message = &Message{
Singular: lit,
}
position = pos
}
if message != nil && position != nil {
if fn.Context {
ctx, cpos := astutil.StringLiteral(fset, c.Args[fn.Start])
if ctx == "" || cpos == nil {
log.Debugf("Skipping argument to context function %s (%v) - empty context", astutil.Ident(c.Fun), fset.Position(c.Pos()))
continue
}
message.Context = ctx
}
if err := messages.Add(message, position, comments(fset, f, position)); err != nil {
return err
}
}
}
return nil
}
func extractGoType(messages messageMap, fset *token.FileSet, f *ast.File, typ string) error {
// for castings
calls, err := astutil.Calls(fset, f, typ)
if err != nil {
return err
}
for _, c := range calls {
if len(c.Args) > 0 {
lit, pos := astutil.StringLiteral(fset, c.Args[0])
if pos == nil {
p := fset.Position(c.Pos())
log.Debugf("Skipping cast to %s (%v) - not a literal", typ, p)
continue
}
comment := comments(fset, f, pos)
if err := messages.AddString(&astutil.String{Value: lit, Position: pos}, comment); err != nil {
return err
}
}
}
strings, err := astutil.Strings(fset, f, typ)
if err != nil {
return err
}
for _, s := range strings {
comment := comments(fset, f, s.Position)
if err := messages.AddString(s, comment); err != nil {
return err
}
}
return nil
}
func extractGoTagField(messages messageMap, fset *token.FileSet, f *ast.File, tagField string) error {
strings, err := astutil.TagFields(fset, f, tagField)
if err != nil {
return err
}
for _, s := range strings {
comment := comments(fset, f, s.Position)
if err := messages.AddString(s, comment); err != nil {
return err
}
}
return nil
}
func extractTemplateMessages(messages messageMap, path string, opts *ExtractOptions) error {
log.Debugf("Extracting messages from template file %s", path)
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
funcs := make(map[string]*Function)
if opts != nil {
for _, v := range opts.Functions {
if v.Template {
funcs[v.Name] = v
}
}
}
text := string(b)
treeSet, err := templateutil.Parse(path, text)
if err != nil {
return err
}
for _, v := range treeSet {
if err := templateutil.ReplaceTranslatableBlocks(v, "t"); err != nil {
return err
}
templateutil.WalkTree(v, func(n, p parse.Node) {
var fname string
switch n.Type() {
case parse.NodeIdentifier:
fname = n.(*parse.IdentifierNode).Ident
case parse.NodeField:
ident := n.(*parse.FieldNode).Ident
if len(ident) > 1 {
fname = ident[len(ident)-1]
}
case parse.NodeVariable:
ident := n.(*parse.VariableNode).Ident
if len(ident) > 1 {
fname = ident[len(ident)-1]
}
}
if fname != "" {
f := funcs[fname]
if f != nil {
count := 1
if f.Context {
count++
}
if f.Plural {
count++
}
cmd := p.(*parse.CommandNode)
// First argument is the function name
if c := len(cmd.Args) - 1; c != count {
log.Debugf("Skipping function %s (%v) - want %d arguments, got %d", f.Name, n.Position(), count, c)
return
}
var s []string
for ii := 1; ii < len(cmd.Args); ii++ {
if sn, ok := cmd.Args[ii].(*parse.StringNode); ok {
s = append(s, sn.Text)
} else {
log.Debugf("Skipping function %s (%v) - non-string argument at position %d", f.Name, n.Position(), ii)
return
}
}
message := &Message{}
switch len(s) {
case 1:
message.Singular = s[0]
case 2:
if f.Context {
message.Context = s[0]
message.Singular = s[1]
} else {
message.Singular = s[0]
message.Plural = s[1]
}
case 3:
message.Context = s[0]
message.Singular = s[1]
message.Plural = s[2]
}
// TODO: The line number doesn't match exactly because of the
// prepended variables
pos := templatePosition(path, text, n)
if err = messages.Add(message, pos, ""); err != nil {
return
}
}
}
})
}
return err
}
func templatePosition(name string, text string, n parse.Node) *token.Position {
return &token.Position{
Filename: name,
Line: strings.Count(text[:int(n.Position())], "\n") + 1,
}
}