跳转到内容

模組:Disambiguation

本页使用了标题或全文手工转换
维基百科,自由的百科全书

local p = {}
local mRedirect = require('Module:Redirect')
local ZhConversion = require('Module:ZhConversion')
local disambiguationTemplates = mw.loadData('Module:Disambiguation/templates')
local PrepareText = require('Module:Wikitext Parsing').PrepareText

local function capitalize(s)
	return mw.ustring.upper(mw.ustring.sub(s, 1, 1)) .. mw.ustring.sub(s, 2, -1)
end

-- 中文版追加:二次处理模板标题繁简
local function zh_title(str)
	local input_str = capitalize(str)
	local output_str = ZhConversion.zh_title('Template:' .. input_str)
	return mw.ustring.sub(output_str, 10, -1)
end

local function isDisambiguationTemplate(template)
	return disambiguationTemplates[zh_title(template)] or false
end

p.isDisambiguation = function(content)
	-- false if there is no content
	if content == nil then
		return false
	end

	-- redirects are not disambiguation pages
	if mRedirect.getTargetFromText(content, "wikitext") ~= nil then
		return false
	end

	-- check for disambiguation templates in the content
	local templateNames = {}
	-- remove nowiki content and html comments for this check
	local activecontent = PrepareText(content)
	for template in mw.ustring.gmatch(activecontent, "{{%s*([^|}]-)%s*[|}]") do
		if isDisambiguationTemplate(template) then
			return true
		end
	end

	-- check for magic word
	if string.find(content, "__DISAMBIG__", 1, true) ~= nil then
		return true
	elseif mw.ustring.find(content, "__消除歧义__", 1, true) ~= nil then
		return true
	end

	return false
end

p._isDisambiguationPage = function(page)
	-- Look "(disambiguation)" in the title
	if mw.ustring.find(page, '(消歧义)',0,true) ~= nil then
		return true;
	elseif mw.ustring.find(page, '(消歧義)',0,true) ~= nil then
		return true;
	end
	-- Look for disamiguation template in page content
	local title = mw.title.new(page)
	if not title then return false end
	local content = title:getContent()
	return p.isDisambiguation(content)
end

-- Entry points for templates
p.isDisambiguationPage = function(frame)
	local title = frame.args[1]
	return p._isDisambiguationPage(title) and "yes" or ""
end

return p