Module:ko-attest

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

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, default = "und", etym_lang = true},
		[2] = {},
		["t"] = {},
		["tr"] = {},
		["alt"] = {},
		["hapax"] = {type = "number"},
		["h"] = {alias_of = "header"},
		["header"] = {default = "first"},
		["nocap"] = {type = "boolean"},
		[3] = {alias_of = "work"},
		["work"] = {},
		["work_tr"] = {},
		["quotes"] = {type = "boolean"},
		[4] = {alias_of = "work_hanja"},
		["work_hanja"] = {},
		[5] = {alias_of = "year"},
		["year"] = {},
		["volume"] = {},
		["page"] = {},
		["url"] = {},
	})

	local headers = {
		["first"] = "First [[attested]]",
		["also"] = "Also [[attested]]",
		["hangeul"] = "In the [[Hangeul]] script, first [[attested]]",
		["hangul"] = "In the [[Hangeul]] script, first [[attested]]",
		["none"] = "",
	}
	local header = headers[args["header"]]
	if args["nocap"] then
		header = us.lower(us.sub(header, 1, 1)) .. us.sub(header, 2, -1)
	end
	
	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
    if args[2] == "-" then
    	attest = ""
    else
    	if args[2] == "" then
    	    require('Module:debug/track')("ko-attest/missing attested form")
    	end
		attest = (header == "" and " " or " as ") .. args[1]:makeWikipediaLink() .. " " .. hapax .. require("Module:links").full_link({
			term = args[2],
			lang = args[1],
			gloss = args["t"],
			tr = args["tr"],
			alt = args["alt"],
		})
    end
    
	
	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
		
		local delim_left = args["quotes"] and "“" or "''"
		local delim_right = args["quotes"] and "”" or "''"
		
		attest_work = " in the " .. delim_left .. work_tr .. delim_right .. " (" .. work_tagged .. ")" .. position .. link .. ", " .. args["year"]
	else
		require('Module:debug/track')("ko-attest/missing work")
	end
	
	return header .. attest .. attest_work
end

return export