Module:User:Suzukaze-c/zh-forms

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

This is a private module sandbox of Suzukaze-c, for their own experimentation. Items in this module may be added and removed at Suzukaze-c's discretion; do not rely on this module's stability.


local export = {}
local m_zh = require("Module:zh")
local links = require("Module:links")
local lang = require("Module:languages")
local find = mw.ustring.find
local gsub = mw.ustring.gsub
local match = mw.ustring.match
local len = mw.ustring.len

function is_left(length, var_count)
	return (length > 3 or var_count * length > 5)
end
	
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

function add_template_styles_tag(frame)
	return frame:extensionTag("templatestyles", "", { src = "Template:sandbox/styles.css" } )
end

function calculate_word_division(comp_type, t, length)
	local test_word = t[1]
	local word_division = {}
	local decomposable = false
	local i = 1

	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

	return decomposable, word_division
end

function calculate_char_set(identity, word_division, char_set)
	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, links.language_link({ lang = lang.getByCode("zh"), term = 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

	return char_set
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 identity = {}
	identity.simp = {}
	identity.trad = {}

	if not args["t"] then
		table.insert(identity.trad, 1, mw.title.getCurrentTitle().subpageText)
	end
	for i = 1, #args["s"] do insert_st(identity.simp, args["s"][i]) end
	for i = 1, #args["t"] do insert_st(identity.trad, args["t"][i]) end
	if #identity.trad ~= 1 and #identity.simp == 0 then
		table.insert(identity.simp, identity.trad[1])
	end

	local test_word = identity.trad[1]
	local length = len(test_word)
	local decomposable, word_division

	decomposable, word_division = calculate_word_division(args["type"], identity.trad, length)

	local char_set = {}
	char_set.simp = {}
	char_set.trad = {}

	char_set = calculate_char_set(identity, word_division, char_set)

	local el_table = mw.html.create("table")
		:addClass("zhForms")
		:addClass(is_left(length, math.max(#identity.trad, #identity.simp)) and "zhForms--left" or nil)
	:done()

	local el_glosses = mw.html.create("tr")
		:addClass("zhForms__glosses")
		:tag("td")
		:done()
	:done()
	if args["gloss"] then
		if args["gloss"] == "-" then
			el_glosses
				:tag("th")
					:addClass("zhForms__gloss")
					:attr("colspan", length)
					:wikitext("<i>phonetic</i>")
				:done()
			:done()
		elseif length == "1" then
		else
		end
	end

	el_table
		:node(el_glosses)
	:done()

	return tostring(el_table) .. add_template_styles_tag(frame)
end

return export