Jump to content

Module:ko-attest

From Wiktionary, the free dictionary


local export = {}
local us = mw.ustring
local ko = require("Module:languages").getByCode("ko")
local tag_text = require("Module:script utilities").tag_text

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"},
		[2] = {},
		["t"] = {},
		["tr"] = {},
		["alt"] = {},
		["hapax"] = {type = "number"},
		["h"] = {alias_of = "header"},
		["header"] = {default = "first"},
		["nocap"] = {type = "boolean"},
		["same"] = {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"],
		}, "term")
    end
    
	
	local attest_work = ""
	if args["same"] or args["work"] then
		local front_fragment = ""
		local back_fragment = ""
		
		export.fetch_data(frame, args)
		
		if args["same"] then
			front_fragment = "same source"
		else
			if args["year"] then
				back_fragment = ", " .. args["year"]
			end
			
			front_fragment = export.format_front_fragment(args)
		end
		
		local position = ""
		local volume = args["volume"]
		local page = args["page"]
		if volume then
			-- prettify
			if volume == "상" then volume = "上"
			elseif volume == "중" then volume = "中"
			elseif volume == "하" then volume = "下"
			end
			volume = tag_text(volume, ko)
			if page then
				position = ", " .. volume .. ":" .. page
			else
				position = ", " .. volume
			end
		elseif page then
			position = ", " .. page
		end
		
		local link = ""
		if args["url"] then
			link = "<sup>[" .. args["url"] .. "]</sup>"
		end
		
		attest_work = " in the " .. front_fragment .. position .. link .. back_fragment
	else
		require('Module:debug/track')("ko-attest/missing work")
	end
	
	return header .. attest .. attest_work
end

function export.format_front_fragment(args)
	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 = tag_text(work, ko)
	
	local delim_left = args["quotes"] and "“" or "''"
	local delim_right = args["quotes"] and "”" or "''"
	
	return delim_left .. work_tr .. delim_right .. " (" .. work_tagged .. ")"
end

function export.fetch_data(frame, args)
	if not args["work"] then return end
	local dt = mw.loadData("Module:ko-attest/data")["works"]
	local data = dt[args["work"]]
	if type(data) == "string" then
		args["work"] = data
		data = dt[data]
	end
	if type(data) == "table" then
		args["work"] = data["work"] or args["work"]
		args["work_hanja"] = data["work_hanja"] or args["work_hanja"]
		args["work_tr"] = data["work_tr"] or args["work_tr"]
		args["year"] = data["year"] or args["year"]
		
		if frame and not args["url"] then
			local volume = args["volume"]
			local page = args["page"]
			
			local d_v = data["volumes"]
			if volume then
				volume = volume:gsub("^[0%s]+", "")
				volume = volume:gsub("%s+$", "")
				if not d_v or d_v.map[volume] == nil then
					error("Invalid volume")
				else
					volume = d_v.map[volume]
					args["volume"] = (d_v.display and d_v.display[volume]) or volume
				end
			elseif d_v == nil then
				volume = "1"
			end
			
			local repl = nil
			if page then
				if not volume then
					error("Cannot specify page in multi-volume work without specifying volume")
				end
				repl = data["url"]["page"] or data["url"]["volume"] or data["url"]["work"]
			elseif volume then
				page = "1"
				repl = data["url"]["volume"] or data["url"]["work"] or data["url"]["page"]
			else
				for _, v in pairs(d_v.map) do
					volume = v
					break
				end
				page = "1"
				repl = data["url"]["work"] or data["url"]["volume"] or data["url"]["page"]
			end
			if repl then
				args["url"] = frame:preprocess(repl:gsub("$1", volume):gsub("$2", page))
			end
		end
	end
end

return export