-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv2lua.editor_script
More file actions
383 lines (345 loc) · 11.2 KB
/
csv2lua.editor_script
File metadata and controls
383 lines (345 loc) · 11.2 KB
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
372
373
374
375
376
377
378
379
380
381
382
383
-- -- ----------------------------------------------
-- -- CSV → Lua table converter
-- -- ----------------------------------------------
local M = {}
-- -- ----------------------------------------------
-- Utilities
-- -- ----------------------------------------------
local function trim(s)
return s:match("^%s*(.-)%s*$")
end
local function ends_with(str, suffix)
return str:sub(- #suffix) == suffix
end
local function remove_utf8bom(text)
local utf8_bom = string.char(0xEF, 0xBB, 0xBF)
if text:sub(1, 3) == utf8_bom then
return text:sub(4)
end
return text
end
local function parse_csv_line(line)
local res = {}
local in_quotes = false
local field = ""
local i = 1
while i <= #line do
local c = line:sub(i, i)
if c == '"' then
if line:sub(i + 1, i + 1) == '"' then
field = field .. '"'
i = i + 1
else
in_quotes = not in_quotes
end
elseif c == ',' and not in_quotes then
table.insert(res, field)
field = ""
else
field = field .. c
end
i = i + 1
end
table.insert(res, field)
return res
end
local function read_csv_lines(path)
local f = io.open("." .. path, "r")
if not f then return {} end
local content = f:read("*a")
f:close()
content = remove_utf8bom(content)
local lines = {}
local buffer = ""
for line in content:gmatch("([^\r\n]*)\r?\n?") do
if line ~= "" or buffer ~= "" then
buffer = buffer ~= "" and (buffer .. "\n" .. line) or line
local quote_count = select(2, buffer:gsub('"', ""))
if quote_count % 2 == 0 then
table.insert(lines, buffer)
buffer = ""
end
end
end
if buffer ~= "" then
table.insert(lines, buffer)
end
return lines
end
local function remove_urls(s)
local original = s
while s:find("%b()") do
s = s:gsub("%b()", function(captured)
if captured:match("^%(%s*https?://") then
return ""
end
return captured
end)
s = trim(s)
end
return s, s ~= original
end
local function parse_value(s)
s = trim(s)
if not s or s == "" then
-- print("parse_value nil")
return "nil"
elseif s:find("[\r\n,]") then
s = remove_urls(s)
local raw_items = {}
local all_numbers = true
local all_booleans = true
for item in s:gmatch("([^\r\n,]+)") do
item = trim(item)
item = remove_urls(item)
if item ~= "" then
table.insert(raw_items, item)
if not tonumber(item) then
all_numbers = false
end
if item ~= "true" and item ~= "false" then
all_booleans = false
end
end
end
if #raw_items == 0 then return "{}" end
local values = {}
for _, item in ipairs(raw_items) do
if all_numbers then
table.insert(values, item)
elseif all_booleans then
table.insert(values, item)
else
table.insert(values, '"' .. item .. '"')
end
end
return "{" .. table.concat(values, ", ") .. "}"
else
s, had_url = remove_urls(s)
local item = s
if tonumber(s) then
elseif s == "true" or s == "false" then
else
item = '"' .. s .. '"'
end
if had_url then
item = '{' .. item .. '}'
end
return item
end
end
local function table_to_string(tbl)
if type(tbl) ~= "table" then
return tbl
end
local is_array = true
local count = 0
for k, _ in pairs(tbl) do
count = count + 1
if type(k) ~= "number" then
is_array = false
break
end
end
if count == 0 then return "{}" end
local parts = {}
for k, v in pairs(tbl) do
local key_str
if is_array then
key_str = "" -- 配列はキーなし
else
if type(k) == "string" and k:match("^%a[%w_]*$") then
key_str = k .. "="
else
key_str = "[" .. table_to_string(k) .. "]="
end
end
local val_str = table_to_string(v)
table.insert(parts, key_str .. val_str)
end
return "{" .. table.concat(parts, ",") .. "}"
end
local function normalize_nested_fields(row)
local grouped = {} -- prefixごとにsuffixをまとめる一時テーブル
-- 1行の各カラムをチェック
for key, value in pairs(row) do
-- "prefix_suffix" にマッチさせる
local prefix, suffix = key:match("^(.-)_(.+)$")
if prefix and suffix then
-- prefixごとのテーブルを初期化
grouped[prefix] = grouped[prefix] or {}
-- suffixごとのテーブルを初期化
grouped[prefix][suffix] = grouped[prefix][suffix] or {}
-- 値を追加(複数要素に対応)
table.insert(grouped[prefix][suffix], value)
end
end
-- grouped = {
-- effects = { name = {...}, value = {...} },
-- buffs = { name = {...}, value = {...}, duration = {...} },
-- }
-- pprint(grouped)
-- prefixごとにネストテーブルを作成
for prefix, suffixes in pairs(grouped) do
local max_len = 0
-- suffixごとの配列の最大長を取得(要素数に合わせてネストを作るため)
for _, list in pairs(suffixes) do
if type(list) == "table" then
max_len = math.max(max_len, #list)
else
max_len = math.max(max_len, 1)
end
end
local nested = {} -- 実際にrowにセットするネストテーブル
for i = 1, max_len do
-- local item = {}
-- suffixごとに値をセット
for sub, list in pairs(suffixes) do
if type(list) == "table" then
-- item[sub] = list[i] -- 複数要素ならインデックスiの値を取得
nested[sub] = list[i]
else
-- item[sub] = list -- 単一要素の場合そのまま
nested[sub] = list
end
end
-- table.insert(nested, item) -- nested配列に追加
end
-- rowにネストテーブルをセット
row[prefix] = table_to_string(nested)
end
-- 元のフラット列(prefix_suffix)は削除
for key, _ in pairs(row) do
if key:match("_.+$") then
row[key] = nil
end
end
return row
end
-- -- ----------------------------------------------
-- Main Functions
-- -- ----------------------------------------------
local function convert_csv_to_lua(path, use_keys)
local lines = read_csv_lines(path)
if #lines < 2 then return false, "CSV must have header and at least one row" end
local headers = parse_csv_line(lines[1])
local skip_indices = {}
for i, h in ipairs(headers) do
local h_trim = trim(h)
if h_trim:sub(1, 1) == "@" then
skip_indices[i] = true
end
end
local has_key = trim(headers[1]) == "key"
local output = {}
for i = 2, #lines do
local row_data = parse_csv_line(lines[i])
local row_table = {}
local start_col = has_key and 2 or 1
for j = start_col, #headers do
if not skip_indices[j] then
local header = trim(headers[j])
row_table[header] = parse_value(row_data[j])
end
end
row_table = normalize_nested_fields(row_table)
if use_keys == true then
local keyed_table = {}
for k, v in pairs(row_table) do
local part = k .. " = " .. v
table.insert(keyed_table, part)
end
row_table = keyed_table
else
local array_table = {}
for _, v in pairs(row_table) do
table.insert(array_table, v)
end
row_table = array_table
end
if has_key then
table.insert(output, " " .. row_data[1] .. " = { " .. table.concat(row_table, ", ") .. " },")
else
table.insert(output, " { " .. table.concat(row_table, ", ") .. " },")
end
end
table.insert(output, 1, "return {")
table.insert(output, "}")
local result = table.concat(output, "\n")
-- local lua_path = "." .. path:gsub("%.csv$", ".lua")
local lua_path = "." .. path:gsub("%.csv$", ".lua")
-- 元のパスからファイル名を取得しスペースより前の部分だけを抽出
local dir_path, file_name = path:match("^(.-)([^/\\]+)$")
local base_name = file_name:match("^(%S+)")
base_name = base_name:gsub("%.csv$", "")
if base_name then
lua_path = "." .. dir_path .. base_name .. ".lua"
end
local lua_file = io.open(lua_path, "w")
if not lua_file then
return false, "Failed to write output file: " .. lua_path
end
lua_file:write(result)
lua_file:close()
return true, lua_path
end
-- -- ----------------------------------------------
-- get_commands UI
-- -- ----------------------------------------------
local function use_header_keys()
local result = editor.ui.show_dialog(editor.ui.dialog({
title = "Use column as header keys?",
buttons = {
editor.ui.dialog_button({
text = "Use",
default = true,
result = true
}),
editor.ui.dialog_button({
text = "No",
cancel = true,
result = false
})
},
}))
return result
end
-- -- ----------------------------------------------
-- get_commands
-- -- ----------------------------------------------
function M.get_commands()
return {
{
label = "Convert CSV to Lua Table",
locations = { "Assets" },
active = function(opts)
for _, id in pairs(opts.selection) do
local path = editor.get(id, "path")
if ends_with(path, ".csv") then
return true
end
end
return false
end,
query = {
selection = { type = "resource", cardinality = "many" }
},
run = function(opts)
local use_result = use_header_keys()
print('Use column as keys:', use_result)
for _, id in pairs(opts.selection) do
local path = editor.get(id, "path")
if ends_with(path, ".csv") then
local ok, target_path = convert_csv_to_lua(path, use_result)
if ok then
print("CSV converted: " .. target_path)
else
print("Error: " .. target_path)
end
end
end
end,
}
}
end
return M