模組:Homophones

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


local export = {}

local m_languages = require("Module:languages")
local m_links = require("Module:links")
local m_qual = require("Module:qualifier")

--[=[
Meant to be called from a module. `data` is a table containing the following fields:

{
  lang = LANGUAGE_OBJECT,
  homophones = {{term = "HOMOPHONE", alt = nil or "DISPLAY_TEXT", gloss = nil or "GLOSS", tr = nil or "TRANSLITERATION",
				 pos = nil or "PART_OF_SPEECH", qualifiers = nil or {"QUALIFIER", "QUALIFIER", ...}}, ...},
  sc = nil or SCRIPT_OBJECT,
  sort = nil or "SORTKEY",
  caption = nil or "CAPTION",
  nocaption = BOOLEAN,
}

Here:

* `lang` is a language object.
* `homophones` is the list of homophones to display. HOMOPHONE is a homophone. QUALIFIER is a qualifier string to
  display after the specific homophone in question, formatted using format_qualifier() in [[Module:qualifier]].
  (FIXME: This should be changed to display the qualifier before the homophone.)
* `sc`, if specified, is a script object.
* `sort`, if specified, is a sort key.
* `caption`, if specified, overrides the default caption "Homophone"/"Homophones". A colon and space is automatically
  added after the caption.
* `nocaption`, if specified, suppresses the caption entirely.
]=]
function export.format_homophones(data)
	local hmptexts = {}
	local hmpcats = {}

	for _, hmp in ipairs(data.homophones) do
		hmp.lang = data.lang
		hmp.sc = data.sc
		local text = m_links.full_link(hmp)
		if hmp.qualifiers and hmp.qualifiers[1] then
			text = text .. " " .. require("Module:qualifier").format_qualifier(hmp.qualifiers)
		end
		table.insert(hmptexts, text)
	end

	table.insert(hmpcats, "有同音詞的" .. data.lang:getCanonicalName() .. "詞")
	local text = table.concat(hmptexts, ",")
	local caption = data.nocaption and "" or (
		data.caption or "[[Appendix:術語表#同音詞|同音詞]]"
	) .. ":"
	text = "<span class=\"homophones\">" .. caption .. text .. "</span>"
	local categories = require("Module:utilities").format_categories(hmpcats, data.lang, data.sort)
	return text .. categories
end


-- Entry point for {{homophones}} template (also written {{homophone}} and {{hmp}}).
function export.show(frame)
	local parent_args = frame:getParent().args
	local compat = parent_args["lang"]
	local offset = compat and 0 or 1

	local params = {
		[1 + offset] = {list = true, allow_holes = true, required = false},
		
		[compat and "lang" or 1] = {required = true, default = "en"},
		["alt"] = {list = true, allow_holes = true},
		["tr"] = {list = true, allow_holes = true},
		["q"] = {list = true, allow_holes = true},
		["sort"] = {},
	}
	
	local args, unrecognized_args =
		require("Module:parameters").process(parent_args, params, true)
	
	if next(unrecognized_args) then
		local list = {}
		local tracking = { "homophones/unrecognized param" }
		for k, v in pairs(unrecognized_args) do
			table.insert(tracking, "homophones/unrecognized param/" .. tostring(k))
			table.insert(list, "|" .. tostring(k) .. "=" .. tostring(v))
		end
		require("Module:debug").track(tracking)
		mw.log("Unrecognized parameter" .. (list[2] and "s" or "")
			.. " in {{homophones}}: " .. table.concat(list, ", "))
	end
	
	local lang = args[compat and "lang" or 1]
	lang = m_languages.getByCode(lang) or m_languages.err(lang, 1)
	
	local maxindex = math.max(args[1 + offset].maxindex, args["alt"].maxindex, args["tr"].maxindex)
	
	-- done this way to maintain past behaivour
	if (args[1 + offset][1] == nil and args["alt"][1] == nil and args["tr"][1] == nil) then
		if mw.title.getCurrentTitle().nsText == "Template" then
			-- so as not to cause an error on the template's page
			args[1 + offset][1] = "term"
		else
			error("Please provide at least one homophone.") -- 请提供至少一个同音词
		end
	end
	
	for i = 1, maxindex do
		args[1 + offset][i] = m_links.full_link{ lang = lang, term = args[1 + offset][i], alt = args["alt"][i], tr = args["tr"][i] }
		if args["q"][i] then
			args[1 + offset][i] = args[1 + offset][i] .. " " .. m_qual.format_qualifier({args["q"][i]})
		end
	end
	
	return "<span class=\"homophones\">[[Appendix:Glossary#同音词|同音词]]:" .. table.concat(args[1 + offset], ", ")
		.. "</span>[[Category:有同音詞的" ..
		 lang:getCanonicalName() .. "詞" .. (args["sort"] and "|" .. args["sort"] or "") .. "]]"
end

return export