模組:About-distinguish

维基百科,自由的百科全书
文档图示 模块文档[创建]
local mHatnote = require('Module:Hatnote')
local mHatlist = require('Module:Hatnote list')
local mArguments --initialize lazily
local ifmobile = require('Module:If mobile')
local mTableTools = require('Module:TableTools')
local checkType = require('libraryUtil').checkType
local p = {}

function p.aboutDistinguish (frame)
	mArguments = require('Module:Arguments')
	local args = mArguments.getArgs(frame)
	return p._aboutDistinguish(args)
end

function p._aboutDistinguish(args, options)
	-- Type checks and defaults
	checkType('_aboutDistinguish', 1, args, 'table')
	if not args[1] then
		return mHatnote.makeWikitextError(
			'没有提供用法',
			'Template:About-distinguish',
			args.category
		)
	end
	if not args[2] then
		return mHatnote.makeWikitextError(
			'没有提供消歧义的条目',
			'Template:About-distinguish',
			args.category
		)
	end
	checkType('_aboutDistinguish', 2, options, 'table', true)
	options = options or {}
	local defaultOptions = {
		defaultPageType = '页面',
		namespace = mw.title.getCurrentTitle().namespace,
		pageTypesByNamespace = {
			[0] = '条目',
			[14] = '分类'
		},
		sectionString = '章节'
	}
	for k, v in pairs(defaultOptions) do
		if options[k] == nil then options[k] = v end
	end

	-- Set pieces of initial "about" string
	local pageType = (args.section and options.sectionString) or
		options.pageTypesByNamespace[options.namespace] or
		options.defaultPageType
	args = mTableTools.compressSparseArray(args)
	local about = table.remove(args, 1)

	--Get pronoun from Wikidata. Really basic, but it should work.
	local pronouns = {
		['女'] = '她',
		['跨性別女性'] = "她",
		['男'] = '他',
		['跨性别男性'] = '他',
		['default'] = '其'
	}
	local wde = mw.wikibase.getEntity()
	local p31 = (wde and wde:formatPropertyValues('P31').value) == '人類'
	local p21 = wde and wde:formatPropertyValues('P21').value
	local pronoun = p31 and (pronouns[p21] or pronouns['default']) or '其'

	--Assemble everything together and return
	local image = '[[File:Confusion grey.svg|24px|link=Wikipedia:消歧义|alt=]]'
	local mobview = ifmobile._main({nil, '  '})
	local text = string.format(
		'\'\'\'提示:\'\'\'此%s介绍的是%s,请勿将%s与%s混淆。',
		pageType,
		about,
		pronoun,
		mHatlist.orList(args, true, true)
	)
	text = image .. mobview .. text
	return mHatnote._hatnote(text)
end

return p