Jump to content

Module:User lang subcat

From Wikisource

--[=[
Implementation logic for [[Template:User lang subcat]]
]=]

require('strict')

local p = {} --p stands for package

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local ISO_639_language_name = require('Module:ISO_639').language_name
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"] = 0,
	["1"] = 1,
	["2"] = 2,
	["3"] = 3,
	["4"] = 4,
	["5"] = 5,
	["N"] = 6
}

local function id2level(id)
	if not id then
		return nil
	end
	
	local level = tostring(mw.ustring.sub(id, -1))
	if mw.ustring.sub(id, -2, -2) == "-" and valid_levels[level] then
		return level
	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 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 = list[level or "default"] .. " " .. 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 list[level or "default"]
end

local function infoBackgroundColor(level)
	local list = {
		["0"] = "#ffeeee",
		["1"] = "#f3e0f3",
		["2"] = "#e9e5f9",
		["3"] = "#e0e8ff",
		["4"] = "#d0f8ff",
		["5"] = "#ffefa6",
		["N"] = "#c5fcdc",
		["default"] = "#eeeeee"
	}
	return list[level or "default"]
end

local function rule(color)
	return mw.getCurrentFrame():expandTemplate {
		title = 'Rule',
		args = {color = color}
	}
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 valid_level, i in pairs(valid_levels) do
		list[i + 2] = catLinkText(babel_code, valid_level, language_name, plural)
	end
	local text = "\n" .. 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 args[1] or id()
	local babel_code = id2code(id)
	local iso_code = args.iso_code or args['iso-code'] or args['iso code'] or args['ISO_code'] or args['ISO-code'] or args['ISO code'] or babel_code
	local level = id2level(id)
	local language_name = args.language_name or args['language-name'] or args['language name'] or ISO_639_language_name(iso_code, 'Unrecognized language')
	local plural = yesno(args.plural) or false
	local userbox_text = args.userbox_text or args['userbox-text'] or args['userbox text']
	local description_text = args.description_text or args['description-text'] or args['description text']
	
	local assignments = {
		['id'] = id,
		['id-op'] = "white-space:nowrap;",
		['id-c'] = idBackgroundColor(level),
		['info-c'] = infoBackgroundColor(level),
		['info'] = info_text(level, language_name, userbox_text, plural)
	}
	
	local description = description(id, language_name, babel_code, description_text, plural)
	
	local user_lang_subcat = userbox(assignments) .. "\n<div style='clear:both;'></div>" .. description
	
	if mw.title.getCurrentTitle().nsText == 'Category' then
	    user_lang_subcat = user_lang_subcat .. supercategory(babel_code, level)
	end
	
	return user_lang_subcat
end

return p