Module:lite-new

From Wiktionary, the free dictionary
Jump to navigation Jump to search

export = {}

local decode_entities = require("Module:string utilities").decode_entities

function export.new(frame)
	frameArgs = frame:getParent().args
	for arg, val in pairs(frameArgs) do
		frameArgs[arg] = mw.text.trim(val)
	end
	local template = frame.args[1]
	local twoLangs = (template == "der-lite" or template == "inh-lite" or template == "bor-lite") and true
	if twoLangs then
		local firstLang = require("Module:languages").getByCode(frameArgs[1], nil, true, true)
		if not firstLang then
			require("Module:languages/error")(frameArgs[1], 1, "code")
		end
	end
	
	local langParam = (twoLangs and frameArgs[2]) or (not twoLangs and frameArgs[1])
	local lang = require("Module:languages").getByCode(langParam, nil, true, true)
	
	if not lang then
		if twoLangs then
			require("Module:languages/error")(langParam, 2, "code")
		else
			require("Module:languages/error")(langParam, 1, "code")
		end
	elseif lang:hasType("family") then
		if twoLangs then
			return "{{" .. template .. "|" .. frameArgs[1] .. "|" .. frameArgs[2] .. "}}"
		else
			return "{{" .. template .. "|" .. frameArgs[1] .. "}}"
		end
	elseif lang:hasType("etymology-only") then
		lang = lang:getFull()
	end
	
	local entryName = (twoLangs and frameArgs[3] and (lang:makeEntryName(frameArgs[3]))) or (not twoLangs and frameArgs[2] and (lang:makeEntryName(frameArgs[2]))) or ""
	local displayText
	if template ~= "t-simple" then
		displayText = (twoLangs and frameArgs[4] and frameArgs[4] ~= "" and not frameArgs["override"] and frameArgs[4]) or (twoLangs and frameArgs["alt"] and frameArgs["alt"] ~= "" and not frameArgs["override"] and frameArgs["alt"]) or (twoLangs and frameArgs[3] and frameArgs[3] ~= "" and (lang:makeDisplayText(frameArgs[3]))) or (not twoLangs and frameArgs[3] and frameArgs[3] ~= "" and not frameArgs["override"] and frameArgs[3]) or (not twoLangs and frameArgs["alt"] and frameArgs["alt"] ~= "" and not frameArgs["override"] and frameArgs["alt"]) or (not twoLangs and frameArgs[2] and frameArgs[2] ~= "" and (lang:makeDisplayText(frameArgs[2]))) or ""
	else
		displayText = frameArgs["alt"] or (lang:makeDisplayText(frameArgs[2]))
	end
	local gloss = (twoLangs and frameArgs[5]) or (not twoLangs and frameArgs[4]) or frameArgs["t"] or frameArgs["gloss"] or ""
	local sc
	if displayText and displayText ~= "" and displayText ~= "-" then
		scObj = lang:findBestScript(displayText)
		sc = scObj:getCode()
		if sc == "Latn" or sc == "None" then sc = "" end
	end
	local tr = displayText and not frameArgs["override"] and frameArgs["tr"] or (lang:transliterate(displayText, scObj))
	if tr == displayText then tr = "" end
	if entryName and displayText and decode_entities(displayText) == entryName then displayText = "" end
	
	local args = {}
	args[1] = twoLangs and frameArgs[1] or ""
	args[2] = (twoLangs and frameArgs[2]) or (not twoLangs and frameArgs[1])
	args[3] = entryName or ""
	args[4] = template ~= "t-simple" and displayText or ""
	args[5] = gloss or ""
	args["sc"] = sc or ""
	args["alt"] = template == "t-simple" and displayText or ""
	args["tr"] = tr or ""
	args["ts"] = frameArgs["ts"] or ""
	args["g"] = frameArgs["g"] or ""
	args["g2"] = frameArgs["g2"] or ""
	args["g3"] = frameArgs["g3"] or ""
	args["pos"] = frameArgs["pos"] or ""
	args["lit"] = frameArgs["lit"] or ""
	args["id"] = frameArgs["id"] or ""
	args["sort"] = frameArgs["sort"] or ""
	args["interwiki"] = template == "t-simple" and frameArgs["interwiki"] or ""
	
	for arg, val in pairs(args) do
		if type(arg) ~= "number" and val ~= "" then
			val = arg .. "=" .. val
		end
		if val ~= "" or (arg == 3 and (args[4] ~= "" or args[5] ~= "")) or (arg == 4 and args[5] ~= "") then
			val = "|" .. val
		end
		args[arg] = val
	end
	
	return decode_entities("{{" .. template .. args[1] .. args[2] .. args["sc"] .. args[3] .. args["alt"] .. args[4] .. args["tr"] .. args["ts"] .. args["g"] .. args["g2"] .. args["g3"] .. args["pos"] .. args[5] .. args["lit"] .. args["id"] .. args["sort"] .. args["interwiki"] .. "}}")
end

return export