-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
main.go
79 lines (71 loc) · 1.74 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
// Copyright 2020 - Offen Authors <hioffen@posteo.de>
// SPDX-License-Identifier: Apache-2.0
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"text/scanner"
)
const (
translationFunc = "__"
opening = '{'
closing = '}'
)
type token struct {
token string
line int
filename string
}
func main() {
for _, file := range os.Args[1:] {
tokens, err := extractFromFile(file)
if err != nil {
panic(err)
}
fmt.Printf("# -*- coding: utf-8 -*-\n")
fmt.Printf("\n")
for _, token := range tokens {
fmt.Printf("#~ line %d \"%s\"\n", token.line, token.filename)
fmt.Printf("gettext(%s)\n", token.token)
fmt.Printf("\n")
}
}
}
func extractFromFile(f string) ([]*token, error) {
var s scanner.Scanner
tpl, err := ioutil.ReadFile(f)
if err != nil {
return nil, fmt.Errorf("extract: error opening file %s: %w", f, err)
}
s.Init(bytes.NewBuffer(tpl))
s.Filename = f
var braces [][]token
for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
if tok == opening && s.Peek() == opening {
s.Scan()
// gobble up every token until the matching closing braces
braces = append(braces, []token{})
for enclosedToken := s.Scan(); enclosedToken != scanner.EOF; enclosedToken = s.Scan() {
if enclosedToken == closing && s.Peek() == closing {
break
}
braces[len(braces)-1] = append(braces[len(braces)-1], token{
token: s.TokenText(),
filename: s.Position.Filename,
line: s.Position.Line,
})
}
}
}
var translationStrings []*token
for _, brace := range braces {
for idx, token := range brace {
if token.token == translationFunc && len(brace) > idx+1 {
translationStrings = append(translationStrings, &brace[idx+1])
}
}
}
return translationStrings, nil
}