维护提醒

BWIKI 全站将于 9 月 3 日(全天)进行维护,期间无法编辑任何页面或发布新的评论。

全站通知:

模块:About

来自星露谷物语维基
跳到导航 跳到搜索
[ 创建 | 刷新 ]文档页面
当前模块文档缺失,需要扩充。
local p = {}

-- 辅助函数:检查字符串是否已经是维基链接
local function isWikilink(s)
	-- 一个简单的检查,如果字符串包含[[和]],就认为它是一个链接
	return s:find('[[', 1, true) and s:find(']]', 1, true)
end

function p.main(frame)
	local parentArgs = frame:getParent().args
	local args = {}
	local max_n = 0
	for k, v in pairs(parentArgs) do
		local n = tonumber(k)
		if n and n > 0 and math.floor(n) == n then
			args[n] = mw.text.trim(v)
			if n > max_n then
				max_n = n
			end
		end
	end
	
	local title = mw.title.getCurrentTitle()
	local pagename = title.text
	
	local content_parts = {}
	table.insert(content_parts, '[[File:Disambig gray.svg|25px|link=]]  ')

	if args[1] then
		local page_type
		if parentArgs.section then
			page_type = '章节'
		else
			local nsText = title.nsText
			if nsText == '模板' then
				page_type = '模板'
			elseif nsText == '模块' then
				page_type = '模块'
			else
				page_type = '条目'
			end
		end
		table.insert(content_parts, '本' .. page_type .. '介绍的是' .. args[1])
	end

	local subsequent_parts = {}
	local state = ''

	for i = 2, max_n do
		local v = args[i]
		if v ~= nil then
			if state == 'link' then
				if isWikilink(v) then
					table.insert(subsequent_parts, "“'''" .. v .. "'''”")
				else
					table.insert(subsequent_parts, "“'''[[" .. v .. "]]'''”")
				end
				state = 'end'
			else
				if v == '和' or v == '或' or v == '、' then
					table.insert(subsequent_parts, v)
				else
					if state == 'end' then
						table.insert(subsequent_parts, ';')
					end
					local desc_text = (v ~= '') and v or '其他用法'
					table.insert(subsequent_parts, '关于' .. desc_text .. ',请见')
					state = 'link'
				end
			end
		end
	end
	
	if state == 'link' then
		table.insert(subsequent_parts, "“'''[[" .. pagename .. "(消歧义)]]'''”")
	end
	
	if max_n < 2 and args[1] then
		table.insert(subsequent_parts, '关于其他用法,请见' .. "“'''[[" .. pagename .. "(消歧义)]]'''”")
	end

	local final_parts = {}
	table.insert(final_parts, table.concat(content_parts, ''))

	if #subsequent_parts > 0 then
		if args[1] then
			table.insert(final_parts, '。 ')
		end
		table.insert(final_parts, table.concat(subsequent_parts, ' '))
		table.insert(final_parts, '。')
	elseif args[1] then
		table.insert(final_parts, '。')
	end
	
	local final_content = table.concat(final_parts, '')
	
	return frame:expandTemplate{ title = 'hatnote', args = { final_content } }
end

return p