モジュール:Infobox

提供:Azipedia
2022年8月21日 (日) 16:02時点におけるPerfectBoat (トーク | 投稿記録)による版
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
ナビゲーションに移動 検索に移動

このモジュールについての説明文ページを モジュール:Infobox/doc に作成できます

local p = {}

local g = require("Module:FTBCommon")

-- returns MW code for an infobox header
function p.header(name, attrs)
	if g.isGiven(name) then
		return "|-\n! class=\"infobox-header\" colspan=\"2\" " .. (attrs or "") .. " | " .. name .. "\n"
	else
		return ""
	end
end

-- wrapper for p.header, can be called from MW code
function p.mainHeader(frame)
	local frame, args = g.getFrameAndArgs(frame)
	return p.header(args[1], args[2])
end

-- returns MW code for one infobox row, must be used inside a 2 column wikitable
function p.row(name, value, nameattrs, valueattrs)
	local r = ""
	if (g.isGiven(name) or g.isGiven(value)) and value ~= "nil" then
		if g.isGiven(value) then
			r = r .. "|- class=\"infobox-inforow\"\n| class=\"infobox-infoname\" " .. (nameattrs or "")
		else
			r = r .. "|- \n| colspan=\"2\" class=\"infobox-centertext\" " .. (nameattrs or "")
		end
		r = r .. " | " .. (name or "")
		if g.isGiven(value) then
			r = r .. " || class=\"infobox-infovalue\" " .. (valueattrs or "") .. " | " .. value
		end
		r = r .. "\n"
	end
	return r
end

-- wrapper for p.row, can be called from MW code
function p.mainRow(frame)
	local frame, args = g.getFrameAndArgs(frame)
	return p.row(args[1], args[2], args[3], args[4])
end

function p.mainRowTest(frame)
	local s = ""
	local frame, args = g.getFrameAndArgs(frame)
	s = s .. "frame\n"
	for k, v in pairs(frame) do
		s = s .. tostring(k) .. "=" .. tostring(v) .. "\n"
	end
	s = s .. "frame.args\n"
	for k, v in pairs(frame.args) do
		s = s .. tostring(k) .. "=" .. tostring(v) .. "\n"
	end
	s = s .. "args\n"
	for k, v in pairs(args) do
		s = s .. tostring(k) .. "=" .. tostring(v) .. "\n"
	end
	return s .. p.row(unpack(args, 1, 4))
end

-- condaitional variant of p.row
-- returns regular row if value is given
-- if given value is blank it is replaced by default
-- if default is also not given an empty string is returned
function p.condRow(name, value, default)
	if g.isGiven(value) then
		return p.row(name, value)
	elseif g.isGiven(default) then
		return p.row(name, default)
	else
		return ""
	end
end

-- wrapper for p.infobox, can be called from MW code
function p.main(frame)
	local frame, args = g.getFrameAndArgs(frame)
	local usesdeprecated = false
	
	if g.isGiven(args.name) then
		--
	elseif g.isGiven(args.cname) then
		args.name = args.cname
		args.cname = nil
	elseif g.isGiven(args["Box title"]) then
		args.name = args["Box title"]
		args["Box title"] = nil
		usesdeprecated = true
	else
		args.name = tostring(mw.title.getCurrentTitle())
	end
	
	local r = "{| class=\"infobox\"\n"
	r = r .. p.header(args.name, "class=\"infobox-header infobox-mainheader\"")
	
	if not g.isGiven(args.img) then
		args.img = args.image
		args.image = nil
		usesdeprecated = usesdeprecated or g.isGiven(args.image)
	end
	
	if g.isGiven(args.fullimage) then
		usesdeprecated = true
	end
	
	local imgstr = ""
	if g.isGiven(args.img) then
		if string.find(args.img, ";") then
			imgstr = g.anim(args.img, args.imgwidth)
		else
			if g.isGiven(args.imgwidth) then
				imgstr = "[[File:" .. args.img .. "|" .. args.imgwidth
			else
				imgstr = "[[File:" .. args.img
			end
			imgstr = imgstr .. "|alt=" .. (args.name or args.img) .. "|hover=" .. (args.name or args.img) .. "|" .. (args.name or args.img) .. "]]"
		end
	elseif g.isGiven(args.fullimage) then
		if string.find(args.fullimage, ";") then
			imgstr = g.anim(args.fullimage, args.imgwidth)
		else
			imgstr = args.fullimage
		end
	end
	
	if g.isGiven(imgstr) then
		r = r .. p.row(imgstr, nil, "class=\"infobox-centertext infobox-image\"")
	end
	
	if g.isGiven(args.caption) then
		r = r .. p.row(args.caption, nil, "class=\"infobox-centertext infobox-caption\"")
	end
	r = r .. p.row("名前", args.name)
	
	for i = 1, 10 do
		if g.isGiven(args["Row " .. i .. " title"]) or g.isGiven(args["Row " .. i .. " info"]) then
			r = r .. p.row(args["Row " .. i .. " title"], args["Row " .. i .. " info"])
			usesdeprecated = true
		end
	end
	
	r = r .. (args.rows or "") .. "\n|}"
 
	if usesdeprecated then
		r = r .. "[[Category:Infoboxes with deprecated params]]"
	end
	
	return r
end

return p