模組:De-noun-old

維基詞典,自由的多語言詞典


local m_links = require("Module:links")
local m_utils = require("Module:utilities")
local lang = require("Module:languages").getByCode("de")

local export = {}
local decls = {}

local archaic_dative_note = "现已罕用。"

PAGENAME = mw.title.getCurrentTitle().text or ''

local function join_ending(stem, ending, s)
	return ((s and mw.ustring.gsub(stem, "ß$", "ss")) or stem) .. ending
end

local function make_plural(pl, head, suffix, s)
	return pl or suffix == '-' and '-' or join_ending(head, suffix, s)
end

local function get_dat_pl(pl)
	return pl .. (mw.ustring.match(pl, "[erl]$") and 'n' or '')
end

decls.m = {
	params = {
		[1] = {},
		[2] = {default = ''},
		["pl"] = {},
		["ns"] = {},
		["gs"] = {},
		["ds"] = {},
		["as"] = {},
		["bs"] = {},
		["vs"] = {},
		["np"] = {},
		["gp"] = {},
		["dp"] = {},
		["ap"] = {},
		["n"] = {default = "full"},
		["prop"] = {type = "boolean", default = false},
		["ss"] = {type = "boolean", default = false},
		["notes"] = {},
	},
}
setmetatable(decls.m, {__call = function(self, args, data)
	data.forms["ns"] = {data.head}
	if args[1] == nil then
		data.forms["gs"] = {data.head}
		data.forms["ds"] = {data.head}
		data.forms["as"] = {data.head}
	elseif args[1] == "n" or args[1] == "ns" then
		data.forms["gs"] = {join_ending(data.head, args[1], args.ss)}
		data.forms["ds"] = {join_ending(data.head, 'n', args.ss)}
		data.forms["as"] = {join_ending(data.head, 'n', args.ss)}
	elseif args[1] == "en" or args[1] == "ens" then
		data.forms["gs"] = {join_ending(data.head, args[1], args.ss)} 
		data.forms["ds"] = {join_ending(data.head, 'en', args.ss)}
		data.forms["as"] = {join_ending(data.head, 'en', args.ss)}
	elseif args[1] == "s" then
		data.forms["gs"] = {join_ending(data.head, 's', args.ss)}
		data.forms["ds"] = {data.head}
		data.forms["as"] = {data.head}
	elseif args[1] == "es" then
		data.forms["gs"] = {join_ending(data.head, 'es', args.ss)}
		data.forms["ds"] = {data.head, join_ending(data.head, 'e', args.ss), notes = {[2] = archaic_dative_note}}
		data.forms["as"] = {data.head}
	elseif args[1] == "ses" then
		data.forms["gs"] = {join_ending(data.head, 'ses', args.ss)}
		data.forms["ds"] = {data.head, join_ending(data.head, 'se', args.ss), notes = {[2] = archaic_dative_note}}
		data.forms["as"] = {data.head}
	elseif args[1] == "(e)s" then
		data.forms["gs"] = {join_ending(data.head, 'es', args.ss), join_ending(data.head, 's', args.ss)}
		data.forms["ds"] = {data.head, join_ending(data.head, 'e', args.ss), notes = {[2] = archaic_dative_note}}
		data.forms["as"] = {data.head}
	elseif args[1] == "(s)" then
		data.forms["gs"] = {join_ending(data.head, 's', args.ss), data.head}
		data.forms["ds"] = {data.head}
		data.forms["as"] = {data.head}
	elseif args[1] == "'" then
		data.forms["gs"] = {join_ending(data.head, "'", args.ss)}
		data.forms["ds"] = {data.head}
		data.forms["as"] = {data.head}
	elseif args[1] == "{{{1}}}" and mw.title.getCurrentTitle().nsText == "Template" then
		data.forms["gs"] = {data.head .. args[1]}
		data.forms["ds"] = {data.head}
		data.forms["as"] = {data.head}
	else
		error('1st parameter “' .. tostring(args[1]) .. '” unrecognized.')
	end
	
	local pl = make_plural(args.pl, data.head, args[2], args.ss)
	
	data.forms["np"] = {pl}
	data.forms["gp"] = {pl}
	data.forms["dp"] = {get_dat_pl(pl)}
	data.forms["ap"] = {pl}
	data.articles = {
		art_ind_ns = "ein", art_def_ns = "der", art_pl_np = "die",
		art_ind_gs = "eines", art_def_gs = "des", art_pl_gp = "der",
		art_ind_ds = "einem", art_def_ds = "dem", art_pl_dp = "den",
		art_ind_as = "einen", art_def_as = "den", art_pl_ap = "die",
		art_ind_bs = "einen", art_def_bs = "den", art_pl_bp = "?",
		art_ind_vs = "einen", art_def_vs = "den", art_pl_vp = "?",
	}
end
})

decls.n = {
	params = {
		[1] = {},
		[2] = {default = ''},
		["pl"] = {},
		["ns"] = {},
		["gs"] = {},
		["ds"] = {},
		["as"] = {},
		["bs"] = {},
		["vs"] = {},
		["np"] = {},
		["gp"] = {},
		["dp"] = {},
		["ap"] = {},
		["n"] = {default = "full"},
		["prop"] = {type = "boolean", default = false},
		["ss"] = {type = "boolean", default = false},
		["notes"] = {},
	},
}
setmetatable(decls.n, {__call = function(self, args, data)
	data.forms["ns"] = {data.head}
	if args[1] == nil then
		data.forms["gs"] = {data.head}
		data.forms["ds"] = {data.head}
		data.forms["as"] = {data.head}
	elseif args[1] == "s" then
		data.forms["gs"] = {join_ending(data.head, 's', args.ss)}
		data.forms["ds"] = {data.head}
		data.forms["as"] = {data.head}
	elseif args[1] == "es" then
		data.forms["gs"] = {join_ending(data.head, 'es', args.ss)}
		data.forms["ds"] = {data.head, join_ending(data.head, 'e', args.ss), notes = {[2] = archaic_dative_note}}
		data.forms["as"] = {data.head}
	elseif args[1] == "ses" then
		data.forms["gs"] = {join_ending(data.head, 'ses', args.ss)}
		data.forms["ds"] = {data.head, join_ending(data.head, 'se', args.ss), notes = {[2] = archaic_dative_note}}
		data.forms["as"] = {data.head}
	elseif args[1] == "(e)s" then
		data.forms["gs"] = {join_ending(data.head, 'es', args.ss), join_ending(data.head, 's', args.ss)}
		data.forms["ds"] = {data.head, join_ending(data.head, 'e', args.ss), notes = {[2] = archaic_dative_note}}
		data.forms["as"] = {data.head}
	elseif args[1] == "(s)" then
		data.forms["gs"] = {join_ending(data.head, 's', args.ss), data.head}
		data.forms["ds"] = {data.head}
		data.forms["as"] = {data.head}
	elseif args[1] == "ens" then
		data.forms["gs"] = {join_ending(data.head, 'ens', args.ss)}
		data.forms["ds"] = {join_ending(data.head, 'en', args.ss), data.head}
		data.forms["as"] = {data.head}
	elseif args[1] == "n" then
		data.forms["gs"] = {join_ending(data.head, 'n', args.ss)}
		data.forms["ds"] = {join_ending(data.head, 'n', args.ss)}
		data.forms["as"] = {data.head}
	elseif args[1] == "'" then
		data.forms["gs"] = {join_ending(data.head, "'", args.ss)}
		data.forms["ds"] = {data.head}
		data.forms["as"] = {data.head}
	elseif args[1] == "{{{1}}}" and mw.title.getCurrentTitle().nsText == "Template" then
		data.forms["gs"] = {join_ending(data.head, args[1], args.ss)}
		data.forms["ds"] = {data.head}
		data.forms["as"] = {data.head}
	else
		error('1st parameter “' .. tostring(args[1]) .. '” unrecognized.')
	end
	
	local pl = make_plural(args.pl, data.head, args[2], args.ss)
	
	data.forms["np"] = {pl}
	data.forms["gp"] = {pl}
	data.forms["dp"] = {get_dat_pl(pl)}
	data.forms["ap"] = {pl}
	data.articles = {
		art_ind_ns = "ein", art_def_ns = "das", art_pl_np = "die",
		art_ind_gs = "eines", art_def_gs = "des", art_pl_gp = "der",
		art_ind_ds = "einem", art_def_ds = "dem", art_pl_dp = "den",
		art_ind_as = "ein", art_def_as = "das", art_pl_ap = "die",
		art_ind_bs = "ein", art_def_bs = "das", art_pl_bp = "?",
		art_ind_vs = "ein", art_def_vs = "das", art_pl_vp = "?",
	}
end
})

decls.f = {
	params = {
		[1] = {default = ''},
		["pl"] = {},
		["ns"] = {},
		["gs"] = {},
		["ds"] = {},
		["as"] = {},
		["bs"] = {},
		["vs"] = {},
		["np"] = {},
		["gp"] = {},
		["dp"] = {},
		["ap"] = {},
		["n"] = {default = "full"},
		["prop"] = {type = "boolean", default = false},
		["ss"] = {type = "boolean", default = false},
		["notes"] = {},
	},
}
setmetatable(decls.f, {__call = function(self, args, data)
	data.forms["ns"] = {data.head}
	data.forms["gs"] = {data.head}
	data.forms["ds"] = {data.head}
	data.forms["as"] = {data.head}
	
	local pl = make_plural(args.pl, data.head, args[1], args.ss)
	
	data.forms["np"] = {pl}
	data.forms["gp"] = {pl}
	data.forms["dp"] = {get_dat_pl(pl)}
	data.forms["ap"] = {pl}
	data.articles = {
		art_ind_ns = "eine", art_def_ns = "die", art_pl_np = "die",
		art_ind_gs = "einer", art_def_gs = "der", art_pl_gp = "der",
		art_ind_ds = "einer", art_def_ds = "der", art_pl_dp = "den",
		art_ind_as = "eine", art_def_as = "die", art_pl_ap = "die",
		art_ind_bs = "eine", art_def_bs = "die", art_pl_bp = "?",
		art_ind_vs = "eine", art_def_vs = "die", art_pl_vp = "?",
	}
end
})

decls.pl = {
	params = {
		["np"] = {},
		["gp"] = {},
		["dp"] = {},
		["ap"] = {},
		["prop"] = {type = "boolean", default = false},
		["notes"] = {},
	},
}
setmetatable(decls.pl, {__call = function(self, args, data)
	local pl = data.head
	
	data.forms["np"] = {pl}
	data.forms["gp"] = {pl}
	data.forms["dp"] = {get_dat_pl(pl)}
	data.forms["ap"] = {pl}
	data.articles = {
		art_pl_np = "die",
		art_pl_gp = "der",
		art_pl_dp = "den",
		art_pl_ap = "die",
	}
end
})

local nums = {
	['full'] = {'ns', 'gs', 'ds', 'as', 'np', 'gp', 'dp', 'ap'},
	['sg'] = {'ns', 'gs', 'ds', 'as'},
	['pl'] = {'np', 'gp', 'dp', 'ap'},
	['sg-av'] = {'ns', 'gs', 'ds', 'as', 'bs', 'vs'},
}

local function make_forms(args, data, num)
	if num == 'sg' then
		table.insert(data.title_app, "''不可数''")
	elseif num == 'pl' then
		table.insert(data.title_app, "''仅复数''")
	elseif num == 'sg-av' then
		table.insert(data.title_app, "''拉丁语派生词''")
		table.insert(data.categories, '有拉丁语派生词变格规则的' ..lang:getCanonicalName() .. '名词')
	end
	
	if args.prop then
		table.insert(data.title_app, "''专有名词''")
	end
	
	if #data.title_app > 0 then
		data.output.title_app = ' (' .. table.concat(data.title_app, ', ') .. ')'
	else
		data.output.title_app = ''
	end
	
	local note_num = 1
	local found_red_link = false
	for _, case in ipairs(nums[num]) do
		terms = args[case] ~= nil and mw.text.split(args[case], '/') or data.forms[case] or {}
		
		local notes = terms.notes or {}
		for i, term in ipairs(terms) do
			if mw.ustring.match(term, "^{{{") then
				linked = term
			elseif term == '-' then
				linked = '—'
			elseif term == '~' then
				linked = m_links.full_link({lang = lang, alt = m_links.remove_links(data.forms['n' .. mw.ustring.sub(case, -1)][1]), allowSelfLink = false})
			elseif data.seen_forms[term] then
				linked = m_links.full_link({lang = lang, alt = term, allowSelfLink = false})
			else
				linked = m_links.full_link({lang = lang, term = term, allowSelfLink = false})
				data.seen_forms[term] = true
				if not found_red_link then
					local title_obj = mw.title.new(lang:makeEntryName(term))
					if title_obj and not title_obj.exists then
						found_red_link = true
					end
				end
			end
			
			if notes[i] then
				linked = linked .. '<sup>' .. tostring(note_num) .. '</sup>'
				table.insert(data.notes, '<sup>' .. tostring(note_num) .. '</sup>' .. notes[i])
				note_num = note_num + 1
			end
			terms[i] = linked
		end
		data.output[case] = table.concat(terms, ',<br/>')
	end
	
	if args.notes then
		table.insert(data.notes, args.notes)
	end
	data.output.notes = table.concat(data.notes)
	data.output.notes = data.output.notes ~= '' and '<div style="text-align:left; font-style: italic;">' .. data.output.notes .. '</div>' or ''
	
	if found_red_link then
	--	table.insert(data.categories,'变位表中有红名链接的'.. lang:getCanonicalName() .. '名词') --暂时禁用
	end
	
	for name, art in pairs(data.articles) do
		data.output[name] = (args.prop == true and '(' .. art .. ')') or art
	end
	
	data.output.title = data.output[nums[num][1]]
end

local function make_table(args, data, wikicode, num)
	make_forms(args, data, num)
	
	local function repl(param)
		return data.output[param]
	end
	
	return mw.ustring.gsub(wikicode, "{{{([a-z0-9_]+)}}}", repl) .. m_utils.format_categories(data.categories, lang)
end

function export.show(frame)
	local parent_args = frame:getParent().args

	local decl_type = frame.args[1] or ''
	
	if not decls[decl_type] then
		error("Unknown declension type '" .. tostring(decl_type) .. "'")
	end
	
	if mw.title.getCurrentTitle().nsText ~= "" then
		decls[decl_type].params.head = {default = PAGENAME}
	end
	local args = require("Module:parameters").process(parent_args, decls[decl_type].params)
	
	local seen_forms = {}
	local data = {
		forms = {},
		seen_forms = {[PAGENAME] = true},
		head = args.head or PAGENAME,
		notes = {},
		title_app = {},
		output = {},
		categories = {},
		articles = {},
	}
	
	local num = args.n
	
	if decl_type == 'pl'then
		num = 'pl'
	elseif args.bs or args.vs then
		num = 'sg-av'
	end
	
	if args[1] == '-' or args[2] == '-' then
		error('This usage of "-" is deprecated in this template. Please use “n=sg” if the noun is singular only or “n=pl” if plural only.')
	end
	
	if (not args[1] or args[1] == "") and mw.title.getCurrentTitle().nsText == "Template" then
		setmetatable(args, {__index = function(self, key)
			return "{{{" .. key .. "}}}"
		end
		})
	end
	
	-- Generate the forms
	decls[decl_type](args, data)
	data.decl = decl_type
	
	local wikicode = frame:expandTemplate{title= 'de-decl-noun-table-' .. num, args = {}}
	
	return make_table(args, data, wikicode, num)
end

return export