Module:User lang subcat
Jump to navigation
Jump to search
For {{user lang subcat}}.
--[=[
Implementation logic for [[Template:User lang subcat]]
]=]
require('strict')
local p = {} --p stands for package
local getArgs = require('Module:Arguments').getArgs
local ISO_639_language_name = require('Module:ISO_639').language_name
local error_function = require('Module:Error').error
local userbox = require('Module:Userbox').userbox
local function id()
local cat = mw.title.getCurrentTitle().baseText
if mw.ustring.sub(cat, 1, 5) == "User " then
return mw.ustring.sub(cat, 6, -1)
else
return nil
end
end
local valid_levels = {"0", "1", "2", "3", "4", "5", "N"}
local function id2level(id)
if id == nil then
return nil
end
local level = mw.ustring.sub(id, -1)
if mw.ustring.sub(id, -2, -2) == "-" then
for k, valid_level in pairs(valid_levels) do -- function to check if value is in table?
if level == valid_level then
return level
end
end
else
return nil
end
end
local function id2code(id)
if id2level(id) then
return mw.ustring.sub(id, 1, -3)
else
return id
end
end
local function id2category(id)
if id then
return "Category:User " .. id
else
return nil
end
end
local function switchByLevel(list, level)
if list[level] then
return list[level]
else
return list["default"]
end
end
local function knowledgeText(level, language_name, plural)
local list = {
["0"] = "have '''no''' knowledge of",
["1"] = "have '''basic''' knowledge of",
["2"] = "have '''intermediate''' knowledge of",
["3"] = "have '''advanced''' knowledge of",
["4"] = "have '''near-native''' knowledge of",
["5"] = "have '''professional''' knowledge of",
["N"] = "are '''native''' speakers of",
["default"] = "indicate their knowledge of"
}
local text = switchByLevel(list, level) .. " " .. language_name
if level == "0" and plural then
text = text .. " (or understand them with great difficulty)"
elseif level == "0" then
text = text .. " (or understand it with great difficulty)"
end
text = text .. "."
return text
end
local function idBackgroundColor(level)
local list = {
["0"] = "#ffbbbb",
["1"] = "#e0c0e0",
["2"] = "#bcb9ef",
["3"] = "#99b3ff",
["4"] = "#77e0e8",
["5"] = "#ffcf4d",
["N"] = "#6ef7a7",
["default"] = "#dddddd"
}
return switchByLevel(list, level)
end
local function infoBackgroundColor(level)
local list = {
["0"] = "#ffeeee",
["1"] = "#f3e0f3",
["2"] = "#e9e5f9",
["3"] = "#e0e8ff",
["4"] = "#d0f8ff",
["5"] = "#ffefa6",
["N"] = "#c5fcdc",
["default"] = "#eeeeee"
}
return switchByLevel(list, level)
end
local function rule(color)
local style = ""
if color then
style = " style='color:" .. color .. "; background-color:" .. color .. ";'"
end
return "\n<hr class='wst-rule'" .. style .. ">\n"
end
local function info_text(level, language_name, userbox_text, plural)
local text = "These users " .. knowledgeText(level, language_name, plural)
if userbox_text then
text = text .. rule(idBackgroundColor(level)) .. userbox_text
end
return text
end
local function catLinkText(id, level, language_name, plural)
local category = id2category(id)
if category then
if level then
category = category .. "-" .. level
end
local text = "* [[:" .. category .. "|" .. category .. ":]] "
if level == nil then
text = text .. "all "
end
text = text .. "users who " .. knowledgeText(level, language_name, plural)
return text
else
return nil
end
end
local function description(id, language_name, babel_code, description_text, plural)
local list = { catLinkText(babel_code, nil, language_name, plural) }
for k, valid_level in pairs(valid_levels) do
list[k+1] = catLinkText(babel_code, valid_level, language_name, plural)
end
local text = table.concat(list, "\n") .. "\n\nFor a list of language-specific tags, see [[Wikisource:Babel]]."
if description_text then
text = text .. rule() .. description_text
end
return text
end
local function supercategory(babel_code, level)
if level then
return "[[" .. id2category(babel_code) .. "| " .. level .. "]]"
else
return "[[Category:User languages|" .. babel_code .. "]]"
end
end
--[=[
Make userbox and description
]=]
function p.user_lang_subcat(frame)
local args = getArgs(frame)
local id = args.id or id()
local babel_code = id2code(id)
local iso_code = args.iso_code or babel_code
local level = id2level(id)
local idBackgroundColor = idBackgroundColor(level)
local infoBackgroundColor = infoBackgroundColor(level)
local language_name = args.language_name or ISO_639_language_name(iso_code, 'Unrecognized language')
local info_text = info_text(level, language_name, args.userbox_text, args.plural)
local assignments = {
['id'] = id,
['id-op'] = "white-space:nowrap;",
['id-c'] = idBackgroundColor,
['info-c'] = infoBackgroundColor,
['info'] = info_text
}
local description = description(id, language_name, babel_code, args.description_text, args.plural)
local user_lang_subcat = userbox(assignments) .. "\n<div style='clear:both;'></div>\n" .. description
if mw.title.getCurrentTitle().nsText == 'Category' then
user_lang_subcat = user_lang_subcat .. "\n" .. supercategory(babel_code, level)
end
return user_lang_subcat
end
return p