Jump to content

Module:User:Lunabunn/ko-attest

From Wiktionary, the free dictionary


local export = {}
local us = mw.ustring

function export.ko_attest(frame)
	local parent_args = frame:getParent().args
	local args = require("Module:parameters").process(parent_args, {
		[1] = {type = "language", required = true, etym_lang = true},
		[2] = {},
		["t"] = {},
		["tr"] = {},
		["alt"] = {},
		["hapax"] = {type = "number"},
		["h"] = {alias_of = "header"},
		["header"] = {default = "first"},
		[3] = {alias_of = "work"},
		["work"] = {},
		["work_tr"] = {},
		[4] = {alias_of = "work_hanja"},
		["work_hanja"] = {},
		[5] = {alias_of = "year"},
		["year"] = {type = "number"},
		["volume"] = {},
		["page"] = {}, -- TODO: support multiple pages
		["url"] = {},
	})

	local headers = {
		["first"] = "First [[attested]] as ",
		["also"] = "Also [[attested]] as ",
		["hangeul"] = "In the [[Hangeul]] script, first [[attested]] as ",
		["hangul"] = "In the [[Hangeul]] script, first [[attested]] as ",
		["none"] = "",
	}
	local header = headers[args["header"]]
	
	local hapaxes = {
		[1] = "[[hapax legomenon]] ",
		[2] = "[[dis legomenon]] ",
		[3] = "[[tris legomenon]] ",
		[4] = "[[tetrakis legomenon]] "
	}
	local hapax = (args["hapax"] and {hapaxes[args["hapax"]]} or {""})[1]
	if hapax == nil then
		error("hapax= must be unspecified or between 1 and 4, inclusive")
	end

	local attest = args[1]:makeWikipediaLink() .. " " .. hapax .. require("Module:links").full_link({
		term = args[2],
		lang = args[1],
		gloss = args["t"],
		tr = args["tr"],
		alt = args["alt"],
	})
	
	local attest_work = ""
	if args["work"] then
		if not args["year"] then
			error("work= was supplied, so year= must be supplied")
		end
		local ko = require("Module:languages").getByCode("ko")
		
		local work_tr
		if args["work_tr"] then
			work_tr = args["work_tr"]
		else
			work_tr = ko:transliterate(us.gsub(args["work"], "_", " "))
			work_tr = us.upper(us.sub(work_tr, 1, 1)) .. us.sub(work_tr, 2, -1)
		end
		
		local work = us.gsub(args["work"], "_", "")
		if args["work_hanja"] then
			work = args["work_hanja"] .. " / " .. work 
		end
		local work_tagged = require("Module:script utilities").tag_text(work, ko)
		
		local position = ""
		if args["volume"] and args["page"] then
			position = " " .. args["volume"] .. ":" .. args["page"]
		elseif args["volume"] then
			position = " " .. args["volume"]
		elseif args["page"] then
			position = " " .. args["page"]
		end
		
		local link = ""
		if args["url"] then
			link = "<sup>[" .. args["url"] .. "]</sup>"
		end
		
		attest_work = " in the ''" .. work_tr .. "'' (" .. work_tagged .. ")" .. position .. link .. ", " .. args["year"]
	end
	
	return header .. attest .. attest_work
end

return export