置顶公告:【置顶】关于临时开启评论区所有功能的公告(2022.10.22) | 【置顶】关于本站Widget恢复使用的公告
  • 你好~!欢迎来到萌娘百科镜像站!如需查看或编辑,请联系本站管理员注册账号。
  • 本镜像站和其他萌娘百科的镜像站无关,请注意分别。

Module:Infobox

贴贴♀百科,万娘皆可贴的百科全书!转载请标注来源页面的网页链接,并声明引自贴贴百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [创建] [刷新]
local module = { }
local inner_args = { ["wrapper"] = true, ["class"] = true, ["m-style"] = true, ["font-size"] = true, ["text-align"] = true, ["width"] = true, ["max-width"] = true, ["float"] = true, ["bg-color"] = true, ["m-bcolor"] = true, ["m-color"] = true,
	["notitle"] = true, ["title-main"] = true, ["top-style"] = true, ["top-bcolor"] = true, ["标题背景色"] = true, ["標題背景色"] = true, ["top-color"] = true, ["标题文字色"] = true, ["標題文字色"] = true, ["Title"] = true,
	["标题"] = true, ["標題"] = true, ["tabs"] = true, ["image-style"] = true, ["image"] = true, ["size"] = true, ["图片大小"] = true, ["圖片大小"] = true, ["caption"] = true, ["图片说明"] = true, ["圖片說明"] = true, ["图片信息"] = true,
	["圖片信息"] = true, ["image-lostmessage"] = true, ["l-style"] = true, ["l-width"] = true, ["l-bcolor"] = true, ["左栏背景色"] = true, ["左欄背景色"] = true, ["l-color"] = true, ["左栏文字色"] = true, ["左欄文字色"] = true,
	["r-style"] = true, ["t-style"] = true, ["t-bcolor"] = true, ["底栏背景色"] = true, ["底欄背景色"] = true, ["t-color"] = true, ["底栏文字色"] = true, ["底欄文字色"] = true, ["i-style"] = true }

function module.Modern(frame)
	local frame_self = frame
	local parent = frame:getParent()
	local title = parent:getTitle()
	local _chk_arg_self = function(...)
		local ret = nil
		for _, arg_name in ipairs{...} do
			ret = frame_self.args[arg_name]
			if ret then
				ret = mw.text.trim(ret)
				if ret ~= "" then return ret end
			end
		end
		return nil
	end
	local _chk_arg, _chk_arg_caller = _chk_arg_self, _chk_arg_self
	if parent and ((title == "Template:InfoboxModern") or (frame.args["wrapper"]=="1" and mw.title.new(title).namespace == 10)) then
		frame = parent
		_chk_arg_caller = function(...)
			local ret = nil
			for _, arg_name in ipairs{...} do
				ret = parent.args[arg_name]
				if ret then
					ret = mw.text.trim(ret)
					if ret ~= "" then return ret end
				end
			end
			return nil
		end
		_chk_arg = function(...)
			return _chk_arg_caller(...) or _chk_arg_self(...)
		end
	end
	
	local html = mw.html.create()
	
	local box = html:tag("table")
		:addClass("infobox"):addClass(_chk_arg("class") or "")
		:attr("align", "right")
	local style_main = _chk_arg("m-style")
	if style_main then
		box:cssText(style_main)
	else
		box:css("font-size", _chk_arg("font-size") or "90%")
			:css("text-align", _chk_arg("text-align") or "center")
			:css("width", _chk_arg("width") or "280px")
			:css("max-width", _chk_arg("max-width") or "100%")
			:css("float", _chk_arg("float") or "right")
			:css("background-color", _chk_arg("bg-color", "m-bcolor") or "#FFF")
			:css("color", _chk_arg("m-color") or "#000")
	end
	if not _chk_arg("notitle") then
		local title_main = _chk_arg_caller("title-main")
		if title_main then
			box:wikitext(title_main)
		else
			local td_title = box:tag("tr"):addClass("infobox-title"):tag("td"):css("padding", "0"):attr("colspan", "2")
			local style_top = _chk_arg("top-style")
			if style_top then
				td_title:cssText(style_top)
			else
				td_title:css("background-color", _chk_arg("top-bcolor", "标题背景色", "標題背景色") or"#26ca9b")
					:css("color", _chk_arg("m-color", "top-color", "标题文字色", "標題文字色") or"#FFF")
					:css("font-size", "100%")
					:css("padding", "1em")
			end
			td_title:wikitext(_chk_arg("Title", "title", "标题", "標題") or mw.title.getCurrentTitle().fullText)
		end
	end
	local arg_tabs = _chk_arg_caller("tabs")
	local arg_image = _chk_arg_caller("image")
	local arg_image_lostmessage = _chk_arg("image-lostmessage")
	if arg_tabs or arg_image or arg_image_lostmessage then
		local td_top = box:tag("tr"):tag("td"):attr("colspan", "2")
		if arg_tabs then
			td_top:wikitext(arg_tabs)
		else
			local style_image = _chk_arg("image-style")
			if style_image then
				td_top:cssText(style_image)
			end
			if arg_image then
				local size = _chk_arg("size", "图片大小", "圖片大小")
				if (not size) or (tonumber(mw.ustring.sub(size, 1, -3)) < 280) then
					size = "280px"
				end
				td_top:wikitext("[[Image:" .. arg_image .. "|" .. size .. "|class=infobox-image]]")
				local arg_caption = _chk_arg("caption", "图片说明", "圖片說明", "图片信息", "圖片信息")
				if arg_caption then
					td_top:tag("br"):done()
						:wikitext(arg_caption)
				end
			else
				td_top:wikitext(arg_image_lostmessage)
			end
		end
	end
	local num_names, item_type, num_insert = 1, 0, nil
	local arg_queue, arg_names = { }, { }
	for _, value in ipairs(frame_self.args) do
		local arg_trim = mw.text.trim(value)
		if num_insert == nil and arg_trim == "<customs>" then
			num_insert = #arg_queue
		else
			local arg_split = mw.text.split(arg_trim, ",", true)
			if mw.ustring.sub(arg_split[1], 1, 1) == "-" then
				arg_split[1] = mw.ustring.sub(arg_split[1], 2)
				item_type = 1
			else
				item_type = 0
			end
			arg_names[arg_split[1]] = arg_split[1]
			table.insert(arg_queue, { ["name"] = arg_split[1], ["type"] = item_type } )
			for index = 2, #arg_split do
				local alias = arg_split[index]
				arg_names[alias] = arg_split[1]
				table.insert(arg_queue, { ["name"] = alias, ["alias"] = num_names } )
			end
			num_names = num_names + #arg_split
		end
	end
	local arg_exclude = frame_self.args["exclude"]
	if arg_exclude then arg_exclude = mw.text.trim(arg_exclude) end
	if arg_exclude and arg_exclude ~= "" then
		local arg_split = mw.text.split(arg_exclude, ";", true)
		for _, value in ipairs(arg_split) do
			inner_args[value] = true
		end
	end
	-- Todo: CustomArgs by {{{order}}}
	local arg_order = frame_self.args["order"]
	if arg_order then arg_order = mw.text.trim(arg_order) end
	if arg_order and arg_order ~= "" then
		local arg_split = mw.text.split(arg_order, ";", true)
		for _, value in ipairs(arg_split) do
			if not (inner_args[value] or arg_names[value] or value == "") then
				if num_insert ~= nil then
					table.insert(arg_queue, num_insert, { ["name"] = value } )
				else
					table.insert(arg_queue, { ["name"] = value } )
				end
				arg_names[value] = value
			end
		end
	end
	local args_input = { }
	for key, value in pairs(frame.args) do
		value = mw.text.trim(value)
		if (not (tonumber(key) or inner_args[key])) and (value ~= "") then
			args_input[key] = value
		end
	end
	for key, value in pairs(args_input) do
		if not arg_names[key] then
			if num_insert ~= nil then
				table.insert(arg_queue, num_insert, { ["name"] = value } )
			else
				table.insert(arg_queue, { ["name"] = value } )
			end
		end
	end
	local style_left = _chk_arg("l-style")
	local width_left = _chk_arg("l-width")
	local bcolor_left = _chk_arg("l-bcolor", "左栏背景色", "左欄背景色")
	local color_left = _chk_arg("l-color", "左栏文字色", "左欄文字色")
	local style_right = _chk_arg("r-style")
	local style_rebar = _chk_arg("t-style")
	local bcolor_rebar = _chk_arg("t-bcolor", "底栏背景色", "底欄背景色")
	local color_rebar = _chk_arg("t-color", "底栏文字色", "底欄文字色")
	local style_text = _chk_arg("i-style")
	local InsertItem = function(key, value)
		local tr_item = box:tag("tr")
		local td_item = tr_item:tag("td")
		if style_left then
			td_item:cssText(style_left)
		else
			td_item:css("width", width_left or "80px")
				:css("background-color", bcolor_left or "#E0FFFF")
				:css("color", color_left or "#000")
		end
		td_item:wikitext(key)
		td_item = tr_item:tag("td")
		if style_right then
			td_item:cssText(style_right)
		end
		td_item:wikitext(value)
	end
	local InsertRebar = function(value)
		local td_item = box:tag("tr"):tag("td"):attr("colspan", "2")
		if style_rebar then
			td_item:cssText(style_rebar)
		else
			td_item:css("background-color", bcolor_rebar or "#62BCA9")
				:css("color", color_rebar or "#FFF")
		end
		td_item:wikitext(value)
	end
	local InsertText = function(value)
		local td_item = box:tag("tr"):tag("td"):attr("colspan", "2")
		if style_text then
			td_item:cssText(style_text)
		else
			td_item:css("text-align", "left")
		end
		td_item:wikitext(value)
	end
	-- mw.logObject(arg_queue)
	local temp_name, temp_type = nil, nil
	for _, arg_item in ipairs(arg_queue) do
		if args_input[arg_item.name] then
			if arg_item.alias then
				temp_name = arg_queue[arg_item.alias].name
				temp_type = arg_queue[arg_item.alias].type
			else
				temp_name = arg_item.name
				temp_type = arg_item.type
			end
			if temp_name then
				if temp_type == 1 then
					InsertRebar(temp_name)
					InsertText(args_input[arg_item.name])
				else
					InsertItem(temp_name, args_input[arg_item.name])
				end
				if arg_item.alias then
					arg_queue[arg_item.alias].name = nil
				end
			end
			args_input[arg_item.name] = nil
			arg_item.name = nil
		end
	end
	
	return html
end

return module