From f944dfb51d276f7e2662d7504a13c994906f947d Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sat, 26 Apr 2025 20:17:37 +0300 Subject: [PATCH] optimize gui.template --- res/scripts/stdlib.lua | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index a0f18e6d..76872cd6 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -226,15 +226,17 @@ end function gui.template(name, params) local text = file.read(file.find("layouts/templates/"..name..".xml")) - for k,v in pairs(params) do - local arg = tostring(v):gsub("'", "\\'"):gsub('"', '\\"') - text = text:gsub("(%%{"..k.."})", arg) - end - text = text:gsub("if%s*=%s*'%%{%w+}'", "if=''") - text = text:gsub("if%s*=%s*\"%%{%w+}\"", "if=\"\"") + text = text:gsub("%%{([^}]+)}", function(n) + local s = params[n] + if not s or #s == 0 then + return + end + local e = string.escape(s) + return e:sub(2, #e-1) + end) + text = text:gsub('if%s*=%s*[\'"]%%{%w+}[\'"]', "if=\"\"") -- remove unsolved properties: attr='%{var}' - text = text:gsub("%s*%S+='%%{[^}]+}'%s*", " ") - text = text:gsub('%s*%S+="%%{[^}]+}"%s*', " ") + text = text:gsub('%s*%S+=[\'"]%%{[^}]+}[\'"]%s*', " ") return text end