-
Notifications
You must be signed in to change notification settings - Fork 4
/
insert_string.moon
175 lines (133 loc) Β· 4.96 KB
/
insert_string.moon
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
import shell_escape from require "lapis.cmd.path"
import from_json, to_json, trim from require "lapis.util"
import columnize from require "lapis.cmd.util"
import types from require("tableshape")
import dmenu from require "helpers.menu"
-- TODO:
-- handle when key already exists
argparse = require "argparse"
parser = argparse "insert_string.moon", "Add a translation to a locale file"
parser\argument "text", "Text to be inserted into translation file"
parser\option "--translations_file", "Where translation will be inserted", "locales/en.json"
parser\option "--from", "Filename where text was pulled from, to help infer key prefix"
parser\option "--prefix", "Set key prefix, don't show picker"
parser\option "--variable_template", "Template for code output", '{{%s}}'
parser\flag "--dryrun", "Don't write to translations file"
args = parser\parse [v for _, v in ipairs arg]
jq = (command) ->
handle = assert io.popen "jq --indent 4 '#{shell_escape command}' '#{shell_escape args.translations_file}'"
res = assert handle\read "*a"
from_json(res), res
find_prefixes = ->
current_strings = jq "."
-- find prefixes
recurse = (t, path="", prefixes={}) ->
for k,v in pairs t
if type(v) == "string"
continue if path == ""
prefixes[path\gsub "%.$", ""] = true
else
recurse v, "#{path}#{k}.", prefixes
prefixes
prefixes = recurse current_strings
prefixes = [k for k in pairs prefixes]
table.sort prefixes
-- if we have a file, infert a prefix
if args.from
candidate = args.from\gsub("^views/", "")\gsub("^widgets/", "")\gsub("%.moon$", "")\gsub("/", ".")
prefixes = [p for p in *prefixes when p != candidate]
table.insert prefixes, 1, candidate
prefixes
convert_to_key = (str) ->
out = str\lower!\gsub("[%s@\\:()!]+", "_")\gsub("[^%d%w_]", "")\gsub("__+", "_")
out = out\gsub("^_+", "")\reverse!\gsub("^_+", "")\reverse!
out
format_text = (str) ->
unless str\match [=[^['"]]=]
return str, convert_to_key str
node = assert require("moonscript.parse").string str
node = unpack node
-- strip escape sequences
import P, C, S, Cmt, Cs, Cp, Ct from require "lpeg"
strip_escapes = Cs (P"\\#{node[2]}" / node[2] + P"\\\\" / "\\" + 1)^0
extract_interpolations = do
import build_grammar from require "moonscript.parse"
import trim from require "lapis.util"
White = S" \t\r\n"^0
-- read as much valid moonscript as possible from a #{
grammar = build_grammar!
read_interpolation = P[[#{]] * White * Cmt(Cp! * grammar, (s, stop, start) ->
true, trim s\sub start, stop - 1
) * P"}"
Ct (read_interpolation + P(1))^0
interpolations = assert extract_interpolations\match str
out_chunks = {}
key_parts = {}
variables = {}
k = 0
for part in *node[3,]
switch type part
when "string"
part = assert strip_escapes\match part
table.insert out_chunks, part
table.insert key_parts, convert_to_key part
when "table"
interpolate = types.shape { "interpolate", types.any\tag "exp" }, open: true
if res = interpolate part
k += 1
code = assert interpolations[k], "missing interpoaltion at #{k}"
variable = convert_to_key code
table.insert out_chunks, args.variable_template\format variable
table.insert key_parts, variable
table.insert variables, { variable, code }
else
error "unknown string part type"
table.concat(out_chunks), convert_to_key(table.concat key_parts, "_"), variables
prefix = args.prefix or dmenu "Prefix:", find_prefixes!
if prefix == ""
os.exit 1
return
text, key, variables = format_text args.text
suffix = dmenu "#{prefix}.", { key }
if suffix == ""
os.exit 1
return
merge = {}
key = "#{prefix}.#{suffix}"\gsub("%.%.+", ".")
parts = [part for part in key\gmatch "[^%.]+"]
existing = if file = io.open(args.translations_file, "r")
from_json assert file\read "*a"
local overwriting
current = merge
path = {}
for i, p in ipairs parts
if i == #parts
if existing and existing[p] and type(existing[p]) != "string"
error "type mismatch: #{table.concat path, "."}.#{p}, expected string, have #{type existing[p]}"
current[p] = text
overwriting = existing and existing[p]
else
if existing and existing[p] and type(existing[p]) != "table"
error "type mismatch: #{table.concat path, "."}.#{p}, expected table, have #{type existing[p]}"
current[p] or= {}
current = current[p]
table.insert path, p
if existing
existing = existing[p]
to_append = to_json merge
out, raw_out = jq ". * #{to_append}"
can_write = if overwriting
if overwriting == text
false -- no need to write
else
"yes" == dmenu "overwrite: #{overwriting}", { "yes", "no" }
else
true
if args.dryrun
print raw_out
else
if can_write
assert(io.open(args.translations_file, "w"))\write raw_out
-- io.stderr\write "#{prefix}\t#{suffix}\t#{args.text}\n"
import string_to_code from require "helpers.code_gen"
string_to_code key, text