模組:Labels/templates

維基詞典,自由的多語言詞典


local m_labels = require("Module:labels")
local m_params = require("Module:parameters")
local m_utilities = require("Module:utilities")
local m_languages = require("Module:languages")
local print_template = require("Module:template link").format_link

local export = {}

function export.show(frame)
	local compat = (frame.args["compat"] or "") ~= ""
	local term_mode = (frame.args["term"] or "") ~= ""
	
	local params = {
		[1] = {required = true},
		[2] = {list = true},
		["nocat"] = {type = "boolean"},
		["script"] = {},
		["script2"] = {},
		["sort"] = {},
		["sort2"] = {},
	}
	
	if compat then
		params[1] = params[2]
		params[2] = nil
		params["lang"] = {required = true}
	end
	
	local args = m_params.process(frame:getParent().args, params)
	
	-- Gather parameters
	local lang = args[compat and "lang" or 1]
	local labels = args[compat and 1 or 2]
	local nocat = args["nocat"]
	local script = args["script"]
	local script2 = args["script2"]
	local sort_key = args["sort"]
	local sort_key2 = args["sort2"]

	if not lang then
		if mw.title.getCurrentTitle().nsText == "Template" then
			lang = "und"
		else
			error("Language code has not been specified. Please provide it to the template using the first parameter.")
		end
	end
	
	lang = m_languages.getByCode(lang) or m_languages.err(lang, compat and "lang" or 1)
	
	return m_labels.show_labels(labels, lang, script, script2, sort_key, sort_key2, nocat, term_mode)
end

--[[	temporary. intentionally undocumented.
		this function is only to be used in
		{{alternative spelling of}},
		{{eye dialect of}}
		and similar templates					]]
function export.show_from(frame)
	local m_labeldata = require("Module:labels/data")
	
	local froms = {}
	local categories = {}

	local iparams = {
		["lang"] = {},
		["limit"] = {type = "number"},
		["default"] = {},
	}
	
	local iargs = require("Module:parameters").process(frame.args, iparams)
	local parent_args = frame:getParent().args
	local compat = iargs["lang"] or parent_args["lang"]

	local params = {
		[compat and "lang" or 1] = {required = not iargs["lang"]},
		["from"] = {list = true},
		["nocat"] = {type = "boolean"},
	}

	-- This is called by various form-of templates. They accept several params,
	-- and some templates accept additional params. To avoid having to list all
	-- of them, we just ignore unrecognized params. The main processing for the
	-- form-of template will catch true unrecognized params.
	local args = require("Module:parameters").process(parent_args, params, "allow unrecognized params")
	local lang = args[compat and "lang" or 1] or iargs["lang"] or "und"
	local nocat = args["nocat"]
	local limit = iargs.limit or 99999
	
	local m_languages = require("Module:languages")
	lang = m_languages.getByCode(lang) or m_languages.err(lang, "lang")

	local already_seen = {}

	for i, k in ipairs(args["from"]) do
		if i > limit then
			break	
		end
		local ret = require("Module:labels").get_label_info(k, lang, already_seen)
		if ret.label ~= "" then
			table.insert(froms, ret.label)
		end
		if ret.categories ~= "" then
			table.insert(categories, ret.categories)
		end
	end
	
	if #froms == 0 then
		return iargs.default
	end

	return require("Module:table").serialCommaJoin(froms) .. table.concat(categories)
end

function export.examples(frame)
	local beginning =
[[
{| class="wikitable"
]]
	local caption = "|+ "

	local headers = 
[[
! code !! result
]]
	
	local row_template =
[[
|-
| {{{code}}} ||{{{middle}}} {{{result}}}
]]

	local middle
	
	local close = "|}"

	local rows = {}
	
	local params = {
		[1] = { required = true },
		["caption"] = {},
		["header"] = { boolean = true },
	}
	
	local args = m_params.process(frame:getParent().args, params)
	
	if args.caption then
		beginning = beginning .. caption .. args.caption .. "\n"
	end
	
	if args.header then
		beginning = beginning .. headers
		middle = ""
	else
		middle = " → || "
	end
	
	local examples = mw.text.split(args[1], ";%s+")
	
	if not examples then
		error("No examples were found in the first parameter")
	end
	
	for i, example in ipairs(examples) do
		local lang = mw.ustring.match(example, "^([^:]+)%:")
		if lang then
			example = mw.ustring.gsub(example, "^([^:]+):", "")
		end
		
		local parameters = mw.text.split(example, ",%s+") or example
		if type(parameters) ~= "table" then
			error('No parameters were found in "' .. example .. '".')
		end
		local template_params = {
			[1] = "label",
			[2] = lang,
		}
		
		for i, parameter in ipairs(parameters) do
			table.insert(template_params, parameter)
		end
		
		local code = print_template(template_params)
		local result = m_labels.show_labels(parameters, m_languages.getByCode(lang), nil, nil, nil, nil, true)
		
		local content = {
			code = code,
			middle = middle,
			result = result,
		}
		
		local function add_content(item)
			if content[item] then
				return content[item]
			end
		end
		
		local row = mw.ustring.gsub(row_template, "{{{(%w+)}}}", add_content)
		table.insert(rows, row)
	end
	
	return beginning .. table.concat(rows) .. close
end

return export