Module:nyms

From Linguifex
Jump to navigation Jump to search


local export = {}

local m_languages = require("Module:languages")
local m_links = require("Module:links")
local m_qual = require("Module:qualifier")
local rsplit = mw.text.split

local function wrap_span(text, lang, sc)
	return '<span class="' .. sc .. '" lang="' .. lang .. '">' .. text .. '</span>'
end

local function get_thesaurus_text(lang, args, maxindex)
	local thesaurus
	local thesaurus_links = {}
	
	while args[2][maxindex] and args[2][maxindex]:find("^Thesaurus:") do
		if args["alt"][maxindex] or args["g"][maxindex]	or args["q"][maxindex]
			or args["tr"][maxindex]	or args["ts"][maxindex]	or args["id"][maxindex]
			or args["lit"][maxindex] or args["pos"][maxindex] then
			error("You cannot use named parameters with Thesaurus links.")
		end
		local link
		local term = args[2][maxindex]:sub(11) -- remove Thesaurus: from beginning
		local sc = require("Module:scripts").findBestScript(term, lang):getCode()
		local fragment = term:find("#")
		if fragment then
			link = "[[" .. args[2][maxindex] .. "|Thesaurus:" .. wrap_span(term:sub(1, fragment-1), lang:getCode(), sc) .. "]]"
		else
			link = "[[" .. args[2][maxindex] .. "|Thesaurus:" .. wrap_span(term, lang:getCode(), sc) .. "]]"
		end
		
		table.insert(thesaurus_links, 1, link)
		
		maxindex = maxindex - 1
	end
	
	if #thesaurus_links > 0 then
		thesaurus = (maxindex == 0 and "''see'' " or "; ''see also'' ")
			.. table.concat(thesaurus_links, ", ")
	end
	
	return thesaurus or "", maxindex
end

function export.nyms(frame)
	local list_with_holes = {list = true, allow_holes = true}
	local params = {
		[1] = {required = true, default = "und"},
		[2] = {list = true, allow_holes = true, required = true},
		
		["alt"] = list_with_holes,
		["tr"] = list_with_holes,
		["ts"] = list_with_holes,
		["t"] = list_with_holes,
		["id"] = list_with_holes,
		["q"] = list_with_holes,
		["lit"] = list_with_holes,
		["pos"] = list_with_holes,
		["g"] = list_with_holes,
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local nym_type = frame.args[1]
	local nym_type_class = string.gsub(nym_type, "%s", "-")
	local lang = m_languages.getByCode(args[1]) or m_languages.err(args[1], 1)
	
	local maxindex = math.max(args[2].maxindex, args["alt"].maxindex, args["tr"].maxindex)
	local thesaurus, link_maxindex = get_thesaurus_text(lang, args, maxindex)
	
	local items = {}
	local use_semicolon = false
	
	for i = 1, link_maxindex do
		if args[2][i] then
			if args[2][i]:find("^Thesaurus:") then
				error("A link to Thesaurus must be the last in the list")
			end
			if args[2][i]:find(",", 1, true) then
				use_semicolon = true
			end
		end
		
		local item = m_links.full_link{
			lang = lang, term = args[2][i], id = args["id"][i],
			alt = args["alt"][i], tr = args["tr"][i], ts = args["ts"][i],
			gloss = args["t"][i], lit = args["lit"][i], pos = args["pos"][i],
			genders = args["g"][i] and rsplit(args["g"][i], ",") or {},
		}
		
		if args["q"][i] then
			item = m_qual.format_qualifier({args["q"][i]}) .. " " .. item
		end
		
		table.insert(items, item)
	end
	
	return "<span class=\"nyms " .. nym_type_class .. "\"><span class=\"defdate\" style=\"font-size: smaller;\">" .. 
		mw.getContentLanguage():ucfirst(nym_type) .. ((#items > 1 or thesaurus ~= "") and "s" or "") ..
		":</span> " .. table.concat(items, use_semicolon and "; " or ", ") .. thesaurus .. "</span>"
end


return export