มอดูล:ja-compound

จาก วิกิพจนานุกรม พจนานุกรมเสรี
local export = {}

local link = require("Module:ja-link").link
-- [[Module:parameters]]

function export.show(frame)
	local args = require("Module:parameters").process(frame:getParent().args, {
		[1] = {list = true, allow_holes = true},
		["t"] = {list = true, allow_holes = true},
		["rom"] = {list = true, allow_holes = true},
		["pos"] = {list = true, allow_holes = true},
		["lit"] = {list = true, allow_holes = true},
		["linkto"] = {list = true, allow_holes = true},
		["s"] = {list = true, allow_holes = true},
		["sort"] = {},
		["nocat"] = {type = "boolean"},
		["nocap"] = {type = "boolean"}, -- required for use with {{surf}} but ignored
	})
	local len = (args[1].maxindex + 1) / 2
	local result = {}
	local categories, sort = {"Japanese compound terms"}, {}

	local function format_label(labels)
		local labels_f = {}
		for lb in labels:gmatch"[^,]+" do
			local rl = ({
				["rendaku"] = {"[[Appendix:Japanese glossary#rendaku|rendaku]]", "Japanese terms with rendaku"},
				["renjo"] = {"[[Appendix:Japanese glossary#renjō|renjō]]", "Japanese terms with renjō"},
			})[lb]
			if rl then
				table.insert(labels_f, rl[1])
				table.insert(categories, rl[2])
			else
				table.insert(labels_f, lb)
			end
		end
		return table.concat(labels_f, ", ") .. " form "
	end

	for i = 1, len do
		local kana_this = args[1][i * 2] or args[1][i * 2 - 1]
		local kana_this_raw = kana_this:gsub("[ ^%.%%%-]", "")

		local b_pre, b_suf = kana_this:match"%-$", kana_this:match"^%-"
		local cat_form = args[1][i * 2 - 1] and args[1][i * 2 - 1]:gsub("[ ^%.%%%-]", "") or kana_this_raw
		if b_pre then
			if b_suf then
				table.insert(categories, "Japanese terms infixed with " .. cat_form)
			else
				table.insert(categories, "Japanese terms prefixed with " .. cat_form)
			end
		elseif b_suf then table.insert(categories, "Japanese terms suffixed with " .. cat_form) end

		local data_pos
		if args.s[i] then
			local sandhi_lemma, sandhi_kana, sandhi_label, _s
			_s, sandhi_label = args.s[i]:match"^(.-)%?(.-)$"
			_s = _s or args.s[i]
			sandhi_lemma, sandhi_kana = _s:match"^(.-):(.-)$"
			if sandhi_lemma == "" then
				sandhi_lemma = args[1][i * 2 - 1]
			elseif sandhi_lemma == nil then
				sandhi_lemma = _s
			end

			data_pos = table.concat{
				args["pos"][i] and args["pos"][i] .. ", " or "",
				sandhi_label and format_label(sandhi_label) or "as ",
				link{
					lemma = sandhi_lemma,
					kana = sandhi_kana,
					linkto = "-",
				},
			}
			table.insert(sort, ((sandhi_kana or sandhi_lemma):gsub("[%^%-%% ]", "")))
		else
			data_pos = args["pos"][i]
			table.insert(sort, (kana_this_raw:gsub("[%^%% ]", "")))
		end
		table.insert(result, link{
			lemma = args[1][i * 2 - 1],
			kana = args[1][i * 2],
			gloss = args["t"][i],
			transliteration = args["rom"][i],
			lit = args["lit"][i],
			linkto = args["linkto"][i],
			pos = data_pos,
		})
	end
	result = table.concat(result, " + ")

	sort = args.sort or table.concat(sort)
	if args.nocat then
		categories = ""
	else
		local lang = require("Module:languages").getByCode("jp")
		categories = require("Module:utilities").format_categories(categories, lang, sort)
	end

	return result .. categories
end

return export