Module:Convert: Difference between revisions

Content deleted Content added
shouldbe is a message, not a unit code; try removing "~= nil" for cleaner style
allow substitutes in error messages so it is easier to make long styles consistent
Line 58:
},
}
 
-- Following puts wanted style around each unit code marked like '...%{ft%}...'.
local unitcode_regex = '%%([{}])'
local unitcode_replace = { ['{'] = '<code style="background:transparent;">', ['}'] = '</code>' }
 
local all_messages = {
Line 66 ⟶ 70:
-- Put the correct name when have a template established.
cvt_prefix = '[[Module talk:Convert|Conversion error]]:',
-- Messages; each is a numbered table: { 'error text', 'category key', gsub_regex, gsub_table }.
cvt_bad_default = { 'Unit "%s" has an invalid default', 'unknown' },
cvt_bad_num = { 'Value "%s" must be a number', 'general' },
Line 78 ⟶ 82:
cvt_no_num2 = { 'Need second value', 'general' },
cvt_no_unit = { 'Need name of unit', 'unknown' },
cvt_should_be = { '%s', 'general', unitcode_regex, unitcode_replace },
cvt_sigfig_pos = { 'sigfig "%s" must be positive', 'general' },
cvt_unknown = { 'Unit "%s" is not known', 'unknown' },
Line 100 ⟶ 104:
local t = mlang[msg[1]]
if t then
-- t[1] = message text, t[2] = category, t[3] = gsub_regex, t[4] = gsub_replace
local text = string.format(t[1] or 'Missing message',
msg[2] or '?',
Line 107 ⟶ 111:
local cat = all_categories[lang][t[2]] or ''
local prefix = mlang['cvt_prefix'] or ''
local regex, replace = t[3], t[4]
if regex and replace then
text = text:gsub(regex, replace)
end
return '<span style="color:black; background-color:orange;">' ..
prefix .. ' ' .. text .. cat .. '</span>'