Jump to content

Module:User:Erutuon/zh-der

From Wiktionary, the free dictionary

Used by {{zh-der/fast}}.


local export = {}

local wide_slash = "/"
local function link(term)
	return '<span class="Hani">[[' .. term .. '#Chinese|' .. term .. ']]</span>'
end

local function print_template_with_positional_parameters(name, parameters)
	local output = require("Module:array")()
	output:insert("{{" .. name)
	for _, parameter in ipairs(parameters) do
		output:insert("|" .. parameter)
	end
	output:insert("}}")
	return output:concat()
end

function export.add_simp(frame)
	local args = require("Module:table").shallowcopy(frame.args[1] and frame.args or frame:getParent().args)
	local to_simplified = assert(require("Module:zh").ts)
	local extract_pron = assert(require("Module:zh/extract").extract_pron)
	
	if not require("Module:table").isArray(args) then
		error("Please supply a sequence of numbered parameters with no gaps or named parameters.")
	end
	
	require "Module:collation".sort(args, require "Module:languages".getByCode("zh"))
	
	return print_template_with_positional_parameters(
		"zh-der/fast",
		require("Module:fun").map(
			function (form)
				if not form:find("/") then
					local simplified = to_simplified(form)
					if simplified ~= form then
						form = form .. "/" .. simplified
					end
				end
				if not form:find ":" then
					local count
					form, count = form:gsub("^%^", "")
					local word = form:match "^[^/]+"
					if not word then
						error("no non-slash characters at beginning of parameter " .. form)
					end
					local translit = extract_pron(word, "m", count == 1)
					if translit then
						form = form .. ":" .. translit
					end
				end
				return form
			end,
			args))
end

local function split_on_char(str, char)
	local pos = str:find(char)
	if pos then
		local first, second = str:sub(1, pos - 1), str:sub(pos + 1)
		if second == "" then second = nil end
		return first, second
	else
		return str
	end
end

local function parse_term(term)
	local rest, note = split_on_char(term, ";")
	local rest, translit = split_on_char(rest, ":")
	local terms = mw.text.split(rest, "/")
	return terms, translit, note
end

local function test_parse_term()
	local function eq(a, b)
		if #a ~= #b then
			return false
		else
			for i, a_ in ipairs(a) do
				local b_ = b[i]
				if type(a_) == "table" then
					if type(b_) == "table" then
						return eq(a_, b_)
					else
						return false
					end
				elseif a_ ~= b_ then
					return false
				end
			end
			return true
		end
	end
	
	
	assert(eq({ parse_term("揀啊揀,揀著一個賣龍眼/揀啊揀,揀著一个賣龍眼/拣啊拣,拣着一个卖龙眼:kéng--ah kéng, kéng-tio̍h chi̍t ê bē lêng-géng;Hokkien") },
		{ {"揀啊揀,揀著一個賣龍眼", "揀啊揀,揀著一个賣龍眼", "拣啊拣,拣着一个卖龙眼"}, "kéng--ah kéng, kéng-tio̍h chi̍t ê bē lêng-géng", "Hokkien" }))
end

export.test_parse_term = test_parse_term

function export.show(frame)
	local terms = {}
	local args = frame.args[1] and frame.args or frame:getParent().args
	for k, v in pairs(args) do
		if type(k) == "number" then
			terms[k] = v
		end
	end
	
	terms = require("Module:table").compressSparseArray(terms)
	
	-- Add support for transliterations and glosses?
	local Array = require("Module:array")
	local formatted_terms = Array(terms)
		:map(function(term)
			local entries, translit, note = parse_term(term)
			local links = Array(entries):map(link):concat(wide_slash)
			return links
				.. (translit and ' (<i class="tr Latn">' .. translit .. '</i>)' or "")
				.. (note and " (''" .. note .. "'')" or "")
		end)
	
	return require("Module:columns/old").create_table(
		3, -- column number
		formatted_terms,
		false, -- alphabetize
		"#F5F5FF", -- background
		(((#terms > 72 or fold) and not saurus) and true or false), -- whether to collapse
		"derivedterms", -- class applied to table containing terms
		"Derived terms", -- title
		nil, -- column width
		nil, -- line start (??)
		nil -- lang: not needed
	)
end

return export