Tidy up generate_from_settingtypes.lua a bit.

* Multiline strings
* Table-concat instead of String-concats
* string.rep instead of loop-concat
* string.format %q instead of manual quotation by gsub
* Assert writeable files
* Generate new settings_translation_file
This commit is contained in:
Tim 2016-07-27 18:46:02 +02:00 committed by est31
parent ba50127309
commit 6590140260
2 changed files with 57 additions and 57 deletions

View File

@ -1,99 +1,99 @@
local settings = ... local settings = ...
local function create_minetest_conf_example() local concat = table.concat
local result = "# This file contains a list of all available settings and their default value for minetest.conf\n" .. local insert = table.insert
"\n" .. local sprintf = string.format
"# By default, all the settings are commented and not functional.\n" .. local rep = string.rep
"# Uncomment settings by removing the preceding #.\n" ..
"\n" ..
"# minetest.conf is read by default from:\n" ..
"# ../minetest.conf\n" ..
"# ../../minetest.conf\n" ..
"# Any other path can be chosen by passing the path as a parameter\n" ..
"# to the program, eg. \"minetest.exe --config ../minetest.conf.example\".\n" ..
"\n" ..
"# Further documentation:\n" ..
"# http://wiki.minetest.net/\n" ..
"\n"
local minetest_example_header = [[
# This file contains a list of all available settings and their default value for minetest.conf
# By default, all the settings are commented and not functional.
# Uncomment settings by removing the preceding #.
# minetest.conf is read by default from:
# ../minetest.conf
# ../../minetest.conf
# Any other path can be chosen by passing the path as a parameter
# to the program, eg. "minetest.exe --config ../minetest.conf.example".
# Further documentation:
# http://wiki.minetest.net/
]]
local function create_minetest_conf_example()
local result = { minetest_example_header }
for _, entry in ipairs(settings) do for _, entry in ipairs(settings) do
if entry.type == "category" then if entry.type == "category" then
if entry.level == 0 then if entry.level == 0 then
result = result .. "#\n# " .. entry.name .. "\n#\n\n" insert(result, "#\n# " .. entry.name .. "\n#\n\n")
else else
for i = 1, entry.level do insert(result, rep("#", entry.level))
result = result .. "#" insert(result, "# " .. entry.name .. "\n\n")
end
result = result .. "# " .. entry.name .. "\n\n"
end end
else else
if entry.comment ~= "" then if entry.comment ~= "" then
for _, comment_line in ipairs(entry.comment:split("\n", true)) do for _, comment_line in ipairs(entry.comment:split("\n", true)) do
result = result .."# " .. comment_line .. "\n" insert(result, "# " .. comment_line .. "\n")
end end
end end
result = result .. "# type: " .. entry.type insert(result, "# type: " .. entry.type)
if entry.min then if entry.min then
result = result .. " min: " .. entry.min insert(result, " min: " .. entry.min)
end end
if entry.max then if entry.max then
result = result .. " max: " .. entry.max insert(result, " max: " .. entry.max)
end end
if entry.values then if entry.values then
result = result .. " values: " .. table.concat(entry.values, ", ") insert(result, " values: " .. concat(entry.values, ", "))
end end
if entry.possible then if entry.possible then
result = result .. " possible values: " .. entry.possible:gsub(",", ", ") insert(result, " possible values: " .. entry.possible:gsub(",", ", "))
end end
result = result .. "\n" insert(result, "\n")
local append = "" local append
if entry.default ~= "" then if entry.default ~= "" then
append = " " .. entry.default append = " " .. entry.default
end end
result = result .. "# " .. entry.name .. " =" .. append .. "\n\n" insert(result, sprintf("# %s =%s\n\n", entry.name, append or ""))
end end
end end
return result return concat(result)
end end
local function create_translation_file() local translation_file_header = [[
local result = "// This file is automatically generated\n" .. // This file is automatically generated
"// It conatins a bunch of fake gettext calls, to tell xgettext about the strings in config files\n" .. // It conatins a bunch of fake gettext calls, to tell xgettext about the strings in config files
"// To update it, refer to the bottom of builtin/mainmenu/tab_settings.lua\n\n" .. // To update it, refer to the bottom of builtin/mainmenu/dlg_settings_advanced.lua
"fake_function() {\n"
fake_function() {]]
local function create_translation_file()
local result = { translation_file_header }
for _, entry in ipairs(settings) do for _, entry in ipairs(settings) do
if entry.type == "category" then if entry.type == "category" then
local name_escaped = entry.name:gsub("\"", "\\\"") insert(result, sprintf("\tgettext(%q);", entry.name))
result = result .. "\tgettext(\"" .. name_escaped .. "\");\n"
else else
if entry.readable_name then if entry.readable_name then
local readable_name_escaped = entry.readable_name:gsub("\"", "\\\"") insert(result, sprintf("\tgettext(%q);", entry.readable_name))
result = result .. "\tgettext(\"" .. readable_name_escaped .. "\");\n"
end end
if entry.comment ~= "" then if entry.comment ~= "" then
local comment_escaped = entry.comment:gsub("\n", "\\n") local comment_escaped = entry.comment:gsub("\n", "\\n")
comment_escaped = comment_escaped:gsub("\"", "\\\"") comment_escaped = comment_escaped:gsub("\"", "\\\"")
result = result .. "\tgettext(\"" .. comment_escaped .. "\");\n" insert(result, "\tgettext(\"" .. comment_escaped .. "\");")
end end
end end
end end
result = result .. "}\n" insert(result, "}\n")
return result return concat(result, "\n")
end end
if false then local file = assert(io.open("minetest.conf.example", "w"))
local file = io.open("minetest.conf.example", "w") file:write(create_minetest_conf_example())
if file then file:close()
file:write(create_minetest_conf_example())
file:close() file = assert(io.open("src/settings_translation_file.cpp", "w"))
end file:write(create_translation_file())
end file:close()
if false then
local file = io.open("src/settings_translation_file.cpp", "w")
if file then
file:write(create_translation_file())
file:close()
end
end

View File

@ -1,6 +1,6 @@
// This file is automatically generated // This file is automatically generated
// It conatins a bunch of fake gettext calls, to tell xgettext about the strings in config files // It conatins a bunch of fake gettext calls, to tell xgettext about the strings in config files
// To update it, refer to the bottom of builtin/mainmenu/tab_settings.lua // To update it, refer to the bottom of builtin/mainmenu/dlg_settings_advanced.lua
fake_function() { fake_function() {
gettext("Client"); gettext("Client");