Difference between pages "Module:TNT" and "Module:TNT/sandbox"

(Difference between pages)
Jump to navigation Jump to search
Page 1
Page 2
>Yurik
(Apply fix from user:ExE Boss - copying https://www.mediawiki.org/w/index.php?title=Module:TNT/sandbox&oldid=4692817)
 
>ExE Boss
(Only use the `zzz123` workaround for integer‑only parameter names)
 
Line 42: Line 42:
 
function p.msg(frame)
 
function p.msg(frame)
 
local dataset, id
 
local dataset, id
local params = {}
+
local params = { n = 0 }
 
local lang = nil
 
local lang = nil
 
for k, v in pairs(frame.args) do
 
for k, v in pairs(frame.args) do
Line 50: Line 50:
 
id = mw.text.trim(v)
 
id = mw.text.trim(v)
 
elseif type(k) == 'number' then
 
elseif type(k) == 'number' then
params[k - 2] = mw.text.trim(v)
+
local i = k - 2
 +
params[i] = mw.text.trim(v)
 +
params.n = math.max(params.n, i)
 
elseif k == 'lang' and v ~= '_' then
 
elseif k == 'lang' and v ~= '_' then
 
lang = mw.text.trim(v)
 
lang = mw.text.trim(v)
Line 65: Line 67:
 
checkType('format', 1, dataset, 'string')
 
checkType('format', 1, dataset, 'string')
 
checkType('format', 2, key, 'string')
 
checkType('format', 2, key, 'string')
return formatMessage(dataset, key, {...})
+
return formatMessage(dataset, key, {n = select('#', ...), ...})
 
end
 
end
  
Line 77: Line 79:
 
checkType('formatInLanguage', 2, dataset, 'string')
 
checkType('formatInLanguage', 2, dataset, 'string')
 
checkType('formatInLanguage', 3, key, 'string')
 
checkType('formatInLanguage', 3, key, 'string')
return formatMessage(dataset, key, {...}, lang)
+
return formatMessage(dataset, key, {n = select('#', ...), ...}, lang)
 
end
 
end
  
Line 88: Line 90:
 
function p.doc(frame)
 
function p.doc(frame)
 
local dataset = 'Templatedata/' .. sanitizeDataset(frame.args[1])
 
local dataset = 'Templatedata/' .. sanitizeDataset(frame.args[1])
return frame:extensionTag('templatedata', p.getTemplateData(dataset)) ..
+
return frame:extensionTag('templatedata', p.getTemplateData(dataset, frame.args)) ..
 
  formatMessage(i18nDataset, 'edit_doc', {link(dataset)})
 
  formatMessage(i18nDataset, 'edit_doc', {link(dataset)})
 
end
 
end
  
function p.getTemplateData(dataset)
+
function p.getTemplateData(dataset, args)
 
-- TODO: add '_' parameter once lua starts reindexing properly for "all" languages
 
-- TODO: add '_' parameter once lua starts reindexing properly for "all" languages
 
local data = loadData(dataset)
 
local data = loadData(dataset)
Line 100: Line 102:
 
end
 
end
  
 +
local numOnly = true
 
local params = {}
 
local params = {}
 
local paramOrder = {}
 
local paramOrder = {}
Line 113: Line 116:
 
end
 
end
 
if name then
 
if name then
 +
if (
 +
(type(name) ~= "number")
 +
and (
 +
(type(name) ~= "string")
 +
or not string.match(name, "^%d+$")
 +
)
 +
) then
 +
numOnly = false
 +
end
 
params[name] = newVal
 
params[name] = newVal
 
table.insert(paramOrder, name)
 
table.insert(paramOrder, name)
Line 119: Line 131:
  
 
-- Work around json encoding treating {"1":{...}} as an [{...}]
 
-- Work around json encoding treating {"1":{...}} as an [{...}]
params['zzz123']=''
+
if numOnly then
 +
params['zzz123']=''
 +
end
  
 
local json = mw.text.jsonEncode({
 
local json = mw.text.jsonEncode({
 
params=params,
 
params=params,
 
paramOrder=paramOrder,
 
paramOrder=paramOrder,
description=data.description
+
description=data.description,
 +
-- TODO: Store this in a dataset:
 +
format=(args and args.format or nil),
 
})
 
})
  
json = string.gsub(json,'"zzz123":"",?', "")
+
if numOnly then
 +
json = string.gsub(json,'"zzz123":"",?', "")
 +
end
  
 
return json
 
return json
Line 181: Line 199:
 
local id, msg = unpack(row)
 
local id, msg = unpack(row)
 
if id == key then
 
if id == key then
local result = mw.message.newRawMessage(msg, unpack(params or {}))
+
local result = mw.message.newRawMessage(msg, unpack(params or {}, 1, params and params.n or nil))
 
return result:plain()
 
return result:plain()
 
end
 
end