Перейти к содержанию

Модуль:zh-forms

Материал из Викисловаря
Документация

Модуль, используемый в {{zh-forms}}.

local export = {}
local m_zh = require("Module:zh")

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
	
	for i = 1, #args.s do table.insert(s, args.s[i]) end
	for i = 1, #args.t do table.insert(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 delink = {}
	if args["delink"] and args["delink"] ~= "" then
		for position in mw.text.gsplit(args["delink"], ",") do
			delink[tonumber(position)] = "yes"
		end
	end
	
	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
	
	function char_gap(length)
		return '\n| style="padding: 0.' .. (length > 8 and '3' or '5') ..
			'em;border-bottom: 1px solid #aaa; font-size:x-large" lang="zh" class="Hani" | '
	end
	
	function char_last(length)
		return '\n| style="padding: 0.' .. (length > 8 and '3' or '5') ..
			'em;border-right: 1px solid #aaa;border' ..
			(length ~= 1 and '-bottom' or '') .. ': 1px solid #aaa; font-size:x-large" lang="zh" class="Hani" | '
	end
	
	function header(length)
		return (length > 3 and ':{|' or '{| align=right') .. 
			' style="clear: right;margin: 1em;border-collapse: collapse;text-align: center"' ..
			(length ~= 1 and '\n|-\n! colspan=2|' or '')
	end
	
	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

	function form_fmt(text, length)
		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
	
	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 = mw.ustring.len(test_word)
	local word_division = {}
	local i = 1
	if comp_type then
		for index in mw.text.gsplit(comp_type, "", true) do
			if mw.ustring.match(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 ~= mw.ustring.len(mw.ustring.gsub(test_word, '…+$', '')) then
			error("'type' parameter does not match word length.")
		end
	else
		for i = 1, length do
			table.insert(word_division, { i, i } )
		end
		decomposable = mw.ustring.len(mw.ustring.gsub(test_word, '…+$', '')) > 2 and true or false
	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 mw.ustring.find(char_string, word_form) then
					char_string = (char_string ~= "" and (char_string .. '/') or "") .. word_form
				end
			end
			if not mw.ustring.match(char_string, '[,%-]') and not delink[i] then
				char_string = "[[" .. mw.ustring.gsub(char_string, '/', ']]/[[') .. "]]"
			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' , ['а-яА-Я'] = 'Rus' }
	local script = {}
	for range, script_name in pairs(scripts) do
		if mw.ustring.match(test_word, '[' .. range .. ']') then
			table.insert(script, script_name)
		end
	end

	table.insert(annotation, #script > 1 and '' or nil)
	table.insert(annotation, (decomposable and args['gloss'] ~= '-') and '' or nil)
	table.insert(annotation, #uncreated > 0 and '' .. 
		'<small class="attentionseeking">(' .. (#uncreated == 1 and 'Эта форма' or 'Эти формы') ..
		' иероглиф' .. (#uncreated == 1 and 'а не создана:' or 'ов не созданы:') .. 
		table.concat(uncreated, ", ") .. '.)</small>' or nil)
	
	altforms = ""
	if args["alt"] or "" ~= "" then
		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;">' ..
				m_zh.link(nil, nil, { altdecomp2[1], tr = (altdecomp[2] or "-") }, mw.title.getCurrentTitle().subpageText) .. 
				(altdecomp2[2] and ' <span style="font-size:70%"><i>' .. altdecomp2[2] .. '</i></span>' or '') .. '</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') .. 'alt. forms' .. 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

	literal = (args["lit"] or args["note"] or "" ~= "") and '\n|-' .. 
		gloss_fmt(length, #word_division + 2, length) .. 
		((args["lit"] or "" ~= "") and '<i>Буквально:</i> “' .. args["lit"] .. "”." or args["note"]) or ""
	
	local gloss, m_gloss_data = {}, require("Module:zh/data/glosses")
	if args['gloss'] == '-' then
		gloss = { gloss_fmt(length * 1.6, #word_division, length) .. '<i>phonetic</i>' }
	elseif length == 1 then
		gloss = {}--{ gloss_fmt(1.6, #word_division) .. '-' }
	else
		for i, position in ipairs(word_division) do
			character = mw.ustring.sub(t[1], position[1], position[2])
			local gloss_text = args[1][i] or m_gloss_data.glosses[character] or ""
		
			if gloss_text == "" and position[2] > position[1] then
				local content = mw.title.new(character):getContent() or false
				if content then
					gloss_text = mw.ustring.match(mw.text.split(content, '%-%-%-%-')[1] .. '\n', '\n#([^#\n]+)\n') or ""
					local function replace_gloss(text)
						text = mw.ustring.gsub(text, ' ?([%{%(]+[^%}%)%{%(]+[%}%)]+)', function(captured)
							wiki_link = mw.ustring.match(captured, "%{%{w%|(.+)%}%}") or mw.ustring.match(captured, "%{%{taxlink%|([^%|]+)[^%}]+%}%}") or false
							return wiki_link or "" end)
						return text
					end
					gloss_text = replace_gloss(gloss_text)
					gloss_text = replace_gloss(gloss_text)
				end
			end
			if mw.ustring.len(mw.ustring.gsub(gloss_text, '[^;]', '')) > 2 then
				gloss_text = '<div class="vsSwitcher vsToggleCategory-glosses"><span class="vsToggleElement">&nbsp;</span><div class="vsShow" style="display:none">' ..
					mw.ustring.match(gloss_text, '^[^;]+;[^;]+;[^;]+') .. '</div><div class="vsHide">' .. gloss_text .. '</div></div>'
			end
			local word_length = mw.ustring.match(character, '[,…%-]') and 0 or mw.ustring.len(mw.ustring.gsub(char_set['trad'][i], '[%[%]/]', ''))
			table.insert(gloss, gloss_fmt(word_length, 1, length) .. gloss_text)
		end
	end
		
	output_text = header(length) .. 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 .. literal .. '\n|}' .. table.concat(annotation)
	
	return output_text
end

return export