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

Module:Sandbox/佳沛降解剂/Infobox

猛汉♂百科,万男皆可猛的百科全书!转载请标注来源页面的网页链接,并声明引自猛汉百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [创建] [刷新]
local p = {}

local others = require "module:others"
local newindex = others.newindex
local index = others.index
local addNewline = others.addNewLine

function p.process(args)
	local data = {}
	
	local function process(k,v)
		if  k == "image"
		then -- 首先检查特定参数
			data[k] = v
			return
		end --检查成功即跳出函数
		
		if type(k) ~= 'string' then return end
		-- 以下代码只检查字符串键
		for _, prefix in ipairs
		{'title','label','data','body','header','caption',"content"}
		do --对于以上前缀,检查其后缀(可以为空),默认为content
			if k:find "%d" then break end -- include no number
			local suffix = k:match("^" .. prefix .. "(%l*)$")
			if suffix == "" then suffix = "content" end
			if suffix then
				newindex(data,v,prefix,suffix)
				return
			end
			
		end
		
		for _, prefix in ipairs {'header','data','label',"row"} do
			if not k:find("%d") then break end 
			--检查带数字键,没有数字直接退出循环
			local num, suffix = k:match('^' .. prefix .. '(%d+%.?%d*)(%l*)')
			if suffix == ""then
				suffix = "content"
			end
			if suffix then
				num = tonumber(num)
				newindex(data,v,"rows",num,prefix,suffix)
				--这里单独存放在data.rows便于判断是否有任意一行
				return
			end
			
		end
	end
	for k,v in pairs(args) do
		process(k,v)
	end
	return data
end

function p.render(data)
	local root = mw.html.create()
	
	-- 渲染图片及其说明,仅限一张
	local image
	if data.image then
		local imagecell = root:tag "div"
			:addClass "newstyle-infobox-image-cell"
		image = imagecell:tag "div" 
			:addClass "newstyle-infobox-image"
			:wikitext(data.image)
	end
	
	if image and index(data,"caption","content") then 
		--如果有图片并且说明文字有内容
		image:tag "div" 
			:addClass "newstyle-infobox-caption"
			:wikitext(data.caption.content) --已经确认了存在就不用再检测
			:addClass(data.caption.class)
			:cssText(data.caption.style)
	end
	
	-- 图片之外部分
	local content = root:tag "div"
		:addClass("newstyle-infobox-content")
	
	-- 渲染标题,必须要有内容
	if index(data,"title","content") then
		content:tag "div"
			:addClass "newstyle-infobox-title"
			:wikitext(data.title.content)
			:addClass(data.title.class)
			:cssText(data.title.style)
	end
	
	-- 渲染框
	if data.rows then
		--检测是否有框的需求
		local rows = content:tag "div"
			:addClass "newstyle-infobox-rows"
			:addClass(index(data,"body","class"))
			:cssText(index(data,"body","style"))
		local keys = {} --考虑到断层参数的排序以及小数,先进行人工排序再迭代
		for k,v in pairs(data.rows) do
			if index(v,'header','content') or index(v,'data','content') then
				table.insert(keys,k)
			end
		end
		table.sort(keys)
		for i,k in ipairs(keys) do
			local v = data.rows[k]
			local header = v.header
			local label = v.label
			local d = v.data
			local r = v.row
			
			--渲染行
			local row = rows:tag "div"
				:addClass "newstyle-infobox-row"
				:addClass(index(r,"class"))
				:cssText(index(r,"style"))
				:addClass(index(data,"row","class"))
				:cssText(index(data,"row","style"))
			if index(header,"content") then
				--如果有header
				row:addClass "newstyle-infobox-header-row"
					:wikitext(header.content)
					:cssText(header.style):cssText(index(data,"header","style"))
					:addClass(header.class):addClass(index(data,"header","class"))
			elseif index(d,"content") then
				--如果没有,渲染带data的行
				row:addClass "newstyle-infobox-data-row"
				if index(label,"content") then
					--如果有标签内容
					row:tag("div")
						:addClass "newstyle-infobox-label"
						:addClass(label.class):addClass(index(data,"label","class"))
						:cssText(label.style):cssText(index(data,"label","style"))
						:wikitext(label.content)
				end
					
					--渲染data格
					
				local c
				if not index(label,"content") then c = "data-without-label" end
				row:tag("div")
					:addClass("newstyle-infobox-data")
					:addClass(c)
					:cssText(d.style):cssText(index(data,"data","style"))
					:addClass(d.class):addClass(index(data,"data","class"))
					:newline()
					:wikitext(d.content)
			end
		end
	end
	-- 渲染介绍
	if index(data,"content","content") then
		content:tag "div"
			:addClass(data.content.class)
			:addClass "hantsect"
			:cssText(data.content.style)
			:wikitext(addNewline(data.content.content))
	end
	
	return tostring(root)
end

function p.infobox(frame)
	local args = {}
	for k, v in pairs(frame:getParent().args) do
		if v and v ~= "" then
			args[k] = v
		end
	end
	
	return p.render(p.process(args))
end

return p