モジュール:Infobox/former

2016年7月7日 (木) 23:57時点におけるtemplate>Waiesuによる版

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

--[[
Infobox モジュール案
{{#invoke:サンドボックス:Waiesu/infobox|main}}
]]

local p = {}

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {parentOnly = true})
	local child = (args.child == 'yes')
	local h = {subheader = {{}}, image = {{}}}
	local body = {}
	local link = args.tnavbar or args.name
	local result = ''
	
	local function args2tbl(str, k, v)
		if string.match(k, str .. '$') then
			h[str][1] = {v}
		elseif string.match(k, str .. '%d+') then
			local num = tonumber(string.match(k, '%d*$'))
			h[str][num] = {v}
		elseif string.find(k, 'style') then
			if string.match(k, 'style$') then
				h[str]['style'] = v
			else
				local num = tonumber(string.match(k, '%d*$'))
				h[str][num]['style'] = v
			end
		elseif string.find(k, 'rowclass') then
			if string.match(k, 'rowclass$') then
				h[str]['rowclass'] = v
			else
				local num =tonumber(string.match(k, '%d*$'))
				h[str][num]['rowclass'] = v
			end
		elseif string.match(k, 'class$') then
			h[str]['class'] = v
		end
	end
	
	for k, v in pairs(args) do
		--subheader
		if string.find(k, 'subheader') then
			args2tbl('subheader', k, v)
		--image
		elseif string.find(k, 'image') then
			args2tbl('image', k, v)
		elseif string.find(k, 'caption') then
			if string.match(k, 'caption$') then
				h['image'][1]['caption'] = '<div style="' .. (args.captionstyle or '') .. '">' .. v .. '</div>'
			elseif string.match(k, 'caption%d+') then
				local num = tonumber(string.match(k, '%d*$'))
				h['image'][num] = h['image'][num] or {}
				h['image'][num]['caption'] = '<div style="' .. (args.captionstyle or '') .. '">' .. v .. '</div>'
			end
		--その他
		elseif string.match(k, '^%D+%d+$') then
			local str, num = string.match(k, '^(%D+)(%d+)$')
			num = tonumber(num)
			body[num] = body[num] or {}
			body[num][str] = v
		end
	end
	
	local function row(header, headerstyle, label, labelstyle, data, datastyle, class, rowclass, id, itemprop, rowitemprop, itemtype, rowitemtype, itemref, rowitemref)
		local result =''
		if header then
			result = '<tr><th scope="col" colspan="2" class="' .. (class or '') .. '" style="text-align:center; ' .. (headerstyle or '') .. '"' .. (rowitemtype and (' itemscope itemtype="' .. rowitemtype .. '"') or '') .. ' itemref="' .. (rowitemref or '') .. '">' .. header .. '</th></tr>'
		elseif data then
			result = '<tr class="' .. (rowclass or '') .. '" itemprop="' .. (rowitemprop or '') .. '"' .. (rowitemtype and (' itemscope itemtype="' .. rowitemtype .. '"') or '') .. ' itemref="' .. (rowitemref or '') .. '">'
			if label then
				result = result .. '<th scope="row" style="text-align:left; white-space:nowrap; ' .. (labelstyle or '') .. '">' .. label .. '</th><td class="' .. (class or '') .. '" style="' .. (datastyle or '') .. '" itemprop="' .. (itemprop or '') .. '"' .. (itemtype and (' itemscope itemtype="' .. itemtype .. '"') or '') .. ' itemref="' .. (itemref or '') .. '">'
			else
					result = result .. '<td colspan="2" class="' .. (class or '') .. '" style="text-align:center; ' .. (datastyle or '') .. '" itemprop="' .. (itemprop or '') .. '"' .. (itemtype and (' itemscope itemtype="' .. rowitemtype .. '"') or '') .. ' itemref="' .. (itemref or '') .. '">'
			end
			result = result .. '\n' .. data .. '</td></tr>'
		end
		return result
	end
	
	if not child then
		result = '<table class="infobox ' .. (args.bodyclass or '') .. '" style="width:22em; ' .. (args.bodystyle or '') .. '"' .. (args.bodyitemtype and (' itemscope itemtype="' .. args.bodyitemtype .. '"') or '') .. ' itemref="' .. (args.bodyitemref or '') .. '">'
		if args.title then
			result = result .. '<caption itemprop="name" class="' .. (args.titleclass or '') .. ' style="' .. (args.titlestyle  or '') .. '">' .. args.title .. '</caption>'
		end
		if args.above then
			result = result .. '<tr><th colspan="2" class="' .. (args.aboveclass or '') .. '" style="text-align:center; font-size:125%; font-weight:bold; ' .. (args.abovestyle or '') .. '" itemprop="' .. (args.aboveitemprop or '') .. '"' .. (args.aboveitemtype and (' itemscope itemtype="' .. args.aboveitemtype .. '"') or '') .. ' itemref="' .. (args.aboveitemref or '') .. '">' .. args.above ..'</th></tr>'
		end
	else
		if args.title then
			result = '<b itemprop="name' .. '"' .. (args.bodyitemtype and (' itemscope itemtype="' .. args.bodyitemtype .. '"') or '') .. ' itemref="' .. (args.bodyitemref or '') .. '">' .. args.title .. '</b>'
		end
	end

	if h.subheader[1] then
		for k, v in ipairs(h.subheader) do
			result = result .. row(nil, nil, nil, nil, v[1], v.style or h.subheader.style, h.subheader.class, v.rowclass, nil, nil, nil, nil, nil, nil, nil)
		end
	end
	if h.image[1] then
		for k, v in ipairs(h.image) do
			result = result .. row(nil, nil, nil, nil, v[1] and (v[1] .. (v.caption or '')), v.style or h.image.style, h.image.class, v.rowclass, nil, nil, nil, nil, nil, nil, nil)
		end
	end
	
	for k, v in ipairs(body) do
		result = result .. row(v.header, args.headerstyle, v.label, args.labelstyle, v.data, args.datastyle, v.class, v.rowclass, v.id, v.itemprop, v.rowitemprop, v.itemtype, v.rowitemtype, v.itemref, v.rowitemref)
	end
	
	if args.below then
		result = result .. '<tr><td colspan="2" class="' .. (args.belowclass or '') .. '" style="text-align:center; ' .. (args.belowstyle or '') .. '">' .. args.below .. '</td></tr>'
	end
	
	if link then
		link = string.gsub(link, ':?[Tt]emplate:', '')
		if not string.find(link, ':') then
			link = 'Template:' .. link
		end
		result = result .. '<tr class="noprint"><td colspan=2 style="text-align:right; font-size:85%;">[[' .. link .. '|テンプレートを表示]]</td></tr>'
	end
	
	if not child then
		result = result .. '</table>'
	end
	
	return result
end

return p