-
Notifications
You must be signed in to change notification settings - Fork 2
/
detokenizer.go
75 lines (60 loc) · 1.54 KB
/
detokenizer.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
package main
import (
"encoding/json"
"strconv"
"io/ioutil"
"fmt"
"bytes"
)
func mapKeyPairs(input [][]byte, buffer []string) []map[string][]byte {
tokenMapS := []map[string][]byte{}
for _, pv := range input {
tokenMap := map[string]*json.RawMessage{}
err := json.Unmarshal(pv, &tokenMap)
if err != nil {
fmt.Println("Failed to parse one of the JSON token files.")
}
tempMap := map[string][]byte{}
for k, v := range tokenMap {
j, err := json.Marshal(&v)
checkError(err)
tempMap[buffer[0]+k+buffer[1]] = j
}
tokenMapS = append(tokenMapS, tempMap)
}
return tokenMapS
}
func detokenize(input []byte, tokenMap []map[string][]byte, unquoted bool) []byte {
overrideCompiled := map[string][]byte{}
for _, v := range tokenMap {
for mk, mv := range v {
if unquoted {
v, _ := strconv.Unquote(string(mv))
mv = []byte(v)
}
overrideCompiled[mk] = mv
}
}
for k, v := range overrideCompiled {
input = bytes.Replace(input, []byte(k), v, -1)
}
input = bytes.Replace(input, []byte("\\u003c"), []byte("<"), -1)
input = bytes.Replace(input, []byte("\\u003e"), []byte(">"), -1)
input = bytes.Replace(input, []byte("\\u0026"), []byte("&"), -1)
if !unquoted {
input = bytes.Replace(input, []byte(`\\`), []byte(`\`), -1)
}
return input
}
func loadFile(path string) []byte {
file, err := ioutil.ReadFile(path)
checkError(err)
return file
}
func outputToFile(path string, data []byte) {
err := ioutil.WriteFile(path, data, 0644)
checkError(err)
}
func outputToStdout(data []byte) {
fmt.Print(string(data))
}