Модуль:zh-forms

Wiktionary дан

Documentation for this module may be created at Модуль:zh-forms/doc

local export = {}

function export.make(frame)
	local params = {
		[1] = { list = true, allow_holes = true, allow_empty = true },
		["s"] = { list = true },
		["t"] = { list = true },
		["type"] = {}, ["delink"] = {}, ["lit"] = {}, ["note"] = {}, ["gloss"] = {}
	}
	local args = require("Module:parameters").process(frame:getParent().args, params)
	local comp_type = args["type"]
	local s, t = {}, {}
	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)
		return '\n|-\n! style="padding: 0.' .. (length > 8 and '3' or '5') ..
			'em;border: 1px solid #aaa;background: #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 > 4 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("'түрү' параметр сөз узундугун дал келбейт.")
		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 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 mw.ustring.match(test_word, '[' .. range .. ']') then
			table.insert(script, script_name)
		end
	end

	note = #script > 1 and '[[Category:Бир нече жазууларында жазылган кытай шарттары]]' or ""
	note = (decomposable and args['gloss'] ~= '-') and note .. '[[Category:Мүмкүн наамдар менен кытай жазуулар]]' or note
	note = #uncreated > 0 and note .. '[[Category:Ыйык түрлөрү менен кытай шарттары]]' .. 
		'<small class="көңүл издеп">(' .. (#uncreated == 1 and 'Бул түрү' or 'Бул түрлөрү') .. ' ички жана адамдын пайдалануу ' .. (#uncreated == 1 and 'is' or 'are') .. ' uncreated: ' .. 
			table.concat(uncreated, ", ") .. '.)</small>' or note
	literal = (args["lit"] or args["note"] or "" ~= "") and '\n|-' .. 
		(length > 3 and '\n|\n|' .. gloss_fmt(length, #word_division, length) or gloss_fmt(length, #word_division + 2, length)) .. '<i>' .. 
		((args["lit"] or "" ~= "") and 'Literally: ' or '') .. (args["lit"] or args["note"]) .. '</i>' 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>арап</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
					content = mw.ustring.match(mw.text.split(content, '%-%-%-%-')[1] .. '\n', '\n#([^#\n]+)\n') or ""
					gloss_text = mw.ustring.gsub(content, '([%{%(]+[^%}%)]+[%}%)]+)', function(captured)
						wiki_link = mw.ustring.match(captured, "%{%{w|(.+)%}%}") or false
						return wiki_link or "" end)
				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) .. '[[жөнөкөйлөтүлгөн кытай|simp.]] жана [[салттуу кытай|trad.]]<br>' .. 
				form_fmt(t, length) .. char_fmt(char_set['trad'], length))
		or
			 var_fmt(length) .. '[[салттуу кытай|trad.]] ' .. 
			 	form_fmt(t, length) .. char_fmt(char_set['trad'], length) .. 

			(var_fmt(length) .. '[[жөнөкөйлөтүлгөн кытай|simp.]] ' ..
				form_fmt(s, length) .. char_fmt(char_set['simp'], length))

		) .. literal .. '\n|}' .. note
	
	return output_text
end

return export