모듈:zh-forms

위키낱말사전, 말과 글의 누리

이 모듈에 대한 설명문서는 모듈:zh-forms/설명문서에서 만들 수 있습니다

local export = {}

local find = mw.ustring.find
local gsub = mw.ustring.gsub
local match = mw.ustring.match
local len = mw.ustring.len

function export.extract_gloss(content, useetc)
	local senses = {}
	local len = mw.ustring.len
	local sense_id = 0
	local etc = false
	local translingual_section, zh_section, j, pos, section
	for sense in content:gmatch("%* '''1%.''' ([^#\n]+)") do
		sense_id = sense_id + 1
		if sense_id > 2 then
			etc = true
			break
		end
		table.insert(senses, sense)
	end
	gloss_text = senses[1] or ""
	local gloss_text_extend = gloss_text .. (senses[2] and "; " .. senses[2] or "")
	gloss_text = (len(gloss_text) < 80 and len(gloss_text_extend) < 160) and gloss_text_extend or gloss_text
	if gloss_text ~= gloss_text_extend then etc = true end
	gloss_text = gsub(gloss_text, "%.$", "")

	return gloss_text
end

function export.make(frame)
	local params = {
		[1] = { list = true, allow_holes = true, allow_empty = true },
		["s"] = { list = true },
		["t"] = { list = true },
		["alt"] = {}, ["type"] = {}, ["delink"] = {}, ["lit"] = {}, ["note"] = {}, ["gloss"] = {}
	}
	local args = require("Module:parameters").process(frame:getParent().args, params)
	local comp_type = args["type"]
	local s, t = {}, {}
	local annotation = {}
	if not frame:getParent().args["t"] then
		table.insert(t, 1, mw.title.getCurrentTitle().subpageText)
	end
	
	local function insert_st(set, text)
		if find(text, "^ ") or find(text, " $") then
			error("Please remove the leading and / or trailing space(s) in the 's' and 't' parameters.")
		end
		table.insert(set, text)
	end
	
	for i = 1, #args.s do insert_st(s, args.s[i]) end
	for i = 1, #args.t do insert_st(t, args.t[i]) end
	
	s.name = "simp"
	t.name = "trad"
	
	if #t ~= 1 and #s == 0 then
		table.insert(s, t[1])
	end

	local function asterisk(term, iscomp)
		if iscomp and len(t[1]) > 1 then return "" end
		if term == mw.title.getCurrentTitle().subpageText or not (mw.title.new(term) or {}).exists then return "" end
		local content = mw.title.new(term):getContent()
		content = gsub(content, "zh%-pron", "Ꙁ")
		content = gsub(content, "zh%-see", "Ꙁ")
		content = gsub(content, "[^Ꙁ]", "")
		return mw.ustring.len(content) > 1 and '<sup><span class="explain" title="This form has one or more other meanings.">*</span></sup>' or ''
	end

	local function var_fmt(length, color)
		return '\n|-\n! style="padding: 0.' .. (length > 8 and '3' or '5') ..
			'em;border: 1px solid #aaa;background: #' .. (color or 'E0FFFF') ..
			';font-weight: normal;font-size: smaller;" colspan="2" |'
	end
	
	local function char_gap(length)
		return '\n| style="padding: 0.' .. (length > 8 and '3' or '5') ..
			'em; background-color:white;border-bottom: 1px solid #aaa; font-size:x-large" lang="zh" class="Hani" | '
	end
	
	local function char_last(length)
		return '\n| style="padding: 0.' .. (length > 8 and '3' or '5') ..
			'em; background-color:white;border-right: 1px solid #aaa;border' ..
			(length ~= 1 and '-bottom' or '') .. ': 1px solid #aaa; font-size:x-large" lang="zh" class="Hani" | '
	end
	
	local function header(length, var_count)
		return ((length > 3 or var_count * length > 5) and ':{|' or '{| align=right') .. 
			' style="clear: right;margin: 1em;border-collapse: collapse;text-align: center"' ..
			(length ~= 1 and '\n|-\n! colspan=2|' or '')
	end
	
	local function gloss_fmt(word, colspan, length)
		return '\n! style="padding: 0.' .. (length > 8 and '3' or '5') ..
			'em;border: 1px solid #aaa;background:#F5F5DC;font-weight: normal;font-size: 80%; width:' ..
			(length <= 8 and (30 * word + 30) or (25 * word + 25)) .. 'px" colspan=' .. (colspan or 1)  ..'|'
	end

	local function form_fmt(text, length)
		for i,value in ipairs(text) do
			text[i] = '[[' .. value .. ']]' .. asterisk(value, false)
		end
		return length ~= 1 and ((length > 8 and '' or '<span style="font-size:140%">') ..
			'(<span lang="zh" class="Hani">' .. table.concat(text, "/") .. '</span>)' ..
			(length > 8 and '' or '</span>')) or ''
	end
	
	local function char_fmt(text, length)
		return (#text ~= 1 and (char_gap(length) .. table.concat(text, char_gap(length), 1, #text-1)) or '') .. char_last(length) .. text[#text]
	end
	
	local test_word = t[1]
	local length = len(test_word)
	local word_division = {}
	local i = 1
	local decomposable
	if comp_type then
		for index in mw.text.gsplit(comp_type, "", true) do
			if find(mw.ustring.sub(test_word, i, i), '[,%-]') then
				table.insert(word_division, { i, i } )
				i = i + 1
			elseif mw.ustring.sub(test_word, i, i) == '…' then
				table.insert(word_division, { i, i + 1 } )
				i = i + 2
			end
			table.insert(word_division, { i, i + index - 1 } )
			i = i + index
		end
		if i - 1 ~= len(gsub(test_word, '…+$', '')) and not find(table.concat(t) .. table.concat(s), "[⿰⿱⿲⿳⿴⿵⿶⿷⿸⿹⿺⿻]") then
			error("'type' parameter does not match word length.")
		end
	else
		for i = 1, length do
			table.insert(word_division, { i, i } )
		end
		decomposable = len(gsub(test_word, '…+$', '')) > 2 and true or false
	end
	
	local delink = {}
	if args["delink"] then
		if args["delink"] == "y" then
			for del_index, _ in ipairs(word_division) do
				delink[del_index] = "yes"
			end
		else
			for position in mw.text.gsplit(args["delink"], ",") do
				delink[tonumber(position)] = "yes"
			end
		end
	end
	
	local char_set = { ['simp'] = {}, ['trad'] = {} }
	local identity = #s == 0 and {t} or {s,t}
	local uncreated = {}
	
	for _, id in ipairs(identity) do
		for i, position in ipairs(word_division) do
			local char_string = ""
			for j = 1, #id do
				local word_form = mw.ustring.sub(id[j], position[1], position[2])
				if not find(char_string, word_form) then
					char_string = (char_string ~= "" and (char_string .. '/') or "") .. word_form
				end
			end
			if not find(char_string, '[,%-]') then
				local hash = {}
				for thing in mw.text.gsplit(char_string, (delink[i] and "" or "/")) do
					table.insert(hash, '[[' .. thing .. ']]' .. asterisk(thing, true))
				end
				char_string = table.concat(hash, (delink[i] and "" or "/"))
			end
			table.insert(char_set[id.name], char_string)
		end
		for _, item in ipairs(id) do
			if not (mw.title.new(item) or {}).exists and item ~= mw.title.getCurrentTitle().subpageText then
				table.insert(uncreated, '"[[' .. item .. ']]"')
			end
		end
	end

	local scripts = { ['一-龯㐀-䶵'] = 'Hani' , ['a-zA-ZāēīōūĀĒĪŌŪa-zA-Z'] = 'Latn', ['0-90-9'] = 'Numb', ['Ͱ-Ͽ'] = 'Grek' }
	local script = {}
	for range, script_name in pairs(scripts) do
		if find(test_word, '[' .. range .. ']') then
			table.insert(script, script_name)
		end
	end

	local altforms = ""
	if args["alt"] then
		local altform_list = {}
		for altform in mw.text.gsplit(args["alt"], ",") do
			local altdecomp = mw.text.split(altform, ":")
			local altdecomp2 = mw.text.split(altdecomp[1], "-")
			table.insert(altform_list, '<span style="white-space:nowrap;">[[' .. altdecomp2[1] .. ']]</span>')
		end
		if #altform_list > 5 then
			altforms = '<div class="vsSwitcher vsToggleCategory-glosses"><span class="vsToggleElement">&nbsp;</span>' ..
				'<div class="vsShow" style="display:none">' .. table.concat(altform_list, "<br>", 1, 5) ..
				'</div><div class="vsHide">' .. table.concat(altform_list, "<br>") .. '</div></div>'
		else
			altforms = table.concat(altform_list, "<br>")
		end
		altforms = var_fmt(length, 'F0FFE0') .. '이체 문자' .. mw.ustring.sub(char_last(length), 1, -45)
			.. 'font-size:90%; padding-top: 10px; padding-bottom: 10px; border-right: 1px solid #aaa; border-bottom: 1px solid #aaa"'
			.. ' colspan="' .. #word_division .. '"|' .. altforms
	end
	
	local anagram = ""
	if len(t[1]) == 2 and not find(t[1], "(.)%1") then
		local anagram_link = mw.ustring.sub(t[1], 2, 2) .. mw.ustring.sub(t[1], 1, 1)
		local anagram_content = mw.title.new(anagram_link):getContent() or false
		if anagram_content and find(anagram_content, "== 중국어 ==") then
			anagram = var_fmt(length, 'F0FFE0') .. 'anagram' .. mw.ustring.sub(char_last(length), 1, -45)
			.. 'font-size:90%; padding-top: 10px; padding-bottom: 10px; border-right: 1px solid #aaa; border-bottom: 1px solid #aaa"'
			.. ' colspan="' .. #word_division .. '"|[[' .. anagram_link .. "]]"
		end
	end
	
	local literal = (args["lit"] or args["note"]) and '\n|-' .. 
		gloss_fmt(length, #word_division + 2, length) .. 
		(args["lit"] and '글자 그대로: “' .. args["lit"] .. 
		(find(args["lit"], "%.$") and "”" or "”.") or args["note"]) or ""
	
	local gloss = {}
	if args['gloss'] == '-' then
		gloss = { gloss_fmt(length * 1.6, #word_division, length) .. '(음역)' }
	elseif length == 1 then
		gloss = {}
	else
		for i, position in ipairs(word_division) do
			local character = mw.ustring.sub(t[1], position[1], position[2])
			
			local gloss_text = args[1][i]
			
			if gloss_text == "-" then gloss_text = "(음역)" end
			if not gloss_text then
				gloss_text = ""
			end
			
			if find(gloss_text, "-", nil, true) then
				local nonbreaking_hyphen = mw.ustring.char(0x2011)
				gloss_text = mw.ustring.gsub(gloss_text, "^%-", nonbreaking_hyphen)
				gloss_text = mw.ustring.gsub(gloss_text, "(%s)%-", "%1" .. nonbreaking_hyphen)
			end
		
			if gloss_text == "" and position[2] > position[1] then
				local senses = {}
				local content = mw.title.new(character):getContent() or false
				if content then
					gloss_text = export.extract_gloss(content, false)
				end
			end
			gloss_text = gsub(gloss_text, ";[^a-zA-Z0-9]+;", ";")
			gloss_text = gsub(gloss_text, ";[^a-zA-Z0-9]*$", "")
			gloss_text = gsub(gloss_text, ";+", ";")
			if len(gsub(gloss_text, '[^;]', '')) > 2 then
				gloss_text = '<div class="vsSwitcher vsToggleCategory-glosses"><span class="vsToggleElement">&nbsp;</span><div class="vsShow" style="display:none">' ..
					match(gloss_text, '^[^;]+;[^;]+;[^;]+') .. '</div><div class="vsHide">' .. gloss_text .. '</div></div>'
			end
			local word_length = match(character, '[,…%-]') and 0 or 
				(delink[i] and len(character) or len(character) * (len(gsub(char_set['trad'][i], '[^/]', '')) + 1))
			table.insert(gloss, gloss_fmt(word_length, 1, length) .. gloss_text)
		end
	end
		
	local PAGENAME = mw.title.getCurrentTitle().text
	local content = mw.title.new(PAGENAME):getContent()
	
	if content then
		local applicable_pos = { ["Noun"] = 1, ["Verb"] = 1, ["Adjective"] = 1, ["Adverb"] = 1, 
			["Definitions"] = 1, ["Pronunciation"] = 1 }
		local previous_level = 2
		local subheading_wanted
		for equals, heading_text in string.gmatch(content, "\n(%=%=+)([^%=]+)%=%=+") do
			local current_level = #equals
			if subheading_wanted and current_level ~= previous_level + 1 then
				if applicable_pos[heading_text] then
					error("The heading \"===Etymology ''n''===\" or \"===Pronunciation ''n''===\" must be followed by a subheading one level lower.")
				end
			end
			previous_level = current_level
			subheading_wanted = string.find(heading_text, " [0-9]+") and true or false
		end
	end
	
	return table.concat{
		header(length, math.max(#t, #s)), table.concat(gloss, ""),
		(#identity == 1 and
			(var_fmt(length) .. '[[정체]]/[[간체]]<br>' .. 
				form_fmt(t, length) .. char_fmt(char_set['trad'], length))
		or
			 var_fmt(length) .. '[[정체]] ' .. 
			 	form_fmt(t, length) .. char_fmt(char_set['trad'], length) .. 

			(var_fmt(length) .. '[[간체]] ' ..
				form_fmt(s, length) .. char_fmt(char_set['simp'], length))

		), altforms, anagram, literal, '\n|}', table.concat(annotation)
	}
end

return export