モジュール:サンドボックス/Ef3/Infobox/former

モジュールの解説[作成]
require('strict')
local yesno = require('Module:Yesno')
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType

local Infobox = {}
function Infobox.split(inputstr, sep)
        if sep == nil then
                sep = "%s"
        end
        local t={}
        for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
                table.insert(t, str)
        end
        return t
end

function Infobox.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {parentOnly = true})	--引数取得
	local child = (args.child == 'yes')
	local subbox = (args.subbox == 'yes')
	local h = {subheader = {}, image = {{}}}	--ヘッダー部(subheader, image)テーブル
	local body, sbody = {}, {}	--本体部テーブル, ソート済み本体部テーブル
	local link = args.tnavbar or args.name	--(フッター部)テンプレート名
	--- local result = ''	--結果格納用
	
	--[[
	subheader, image用引数振り分け
	]]
	local function args2tbl(str, k, v)
		local num = k:match('%d*$')
		num = (num == '') and 1 or tonumber(num)
		h[str][num] = h[str][num] or {}
		if k == str then
			h[str][1][1] = v
		elseif k:match(str .. '%d+') then
			h[str][num][1] = v
		elseif k:find('style') then
			if k:match('style$') then
				h[str]['style'] = v
			else
				h[str][num]['style'] = v
			end
		elseif k:find('rowclass') then
			if k:match('rowclass$') then
				h[str]['rowclass'] = v
			else
				h[str][num]['rowclass'] = v
			end
		elseif k:match('class$') then
			h[str]['class'] = v
		end
	end
	
	--[[
	引数振り分け
	]]
	for k, v in pairs(args) do
		--subheader
		if k:find('subheader') then
			args2tbl('subheader', k, v)
		--image
		elseif k:find('image') then
			args2tbl('image', k, v)
		elseif k:find('caption') then
			local caption = mw.html.create("div")
				:node(v)
			if args.captionstyle then caption:cssText(args.captionstyle) end
			caption = tostring(caption)
			if k:match('caption$') then
				h['image'][1]['caption'] = caption
			elseif k:match('caption%d+') then
				local num = tonumber(k:match('%d*$'))
				h['image'][num] = h['image'][num] or {}
				h['image'][num]['caption'] = caption
			end
		--その他(本体部)
		elseif k:match('^%D+%d+$') then
			local str, num = k:match('^(%D+)(%d+)$')
			num = tonumber(num)
			if not body[num] then
				local OddOrEven = (num % 2 ~= 0) and 'odd' or 'even'
				body[num] = {
					num,
					headerstyle = (args.headerstyle or '') .. (args[OddOrEven .. 'headerstyle'] or ''),
					labelstyle = (args.labelstyle or '') .. (args[OddOrEven .. 'labelstyle'] or ''),
					datastyle = (args.datastyle or '') .. (args[OddOrEven .. 'datastyle'] or '')
				}
			end
			body[num][str] = (body[num][str] or '') .. v
		end
	end
	
	--[[
	Template:Infobox/row
	]]
	local function row(header, headerstyle, label, labelstyle, data, datastyle, rowstyle, class, rowclass, id, itemprop, rowitemprop, itemtype, rowitemtype, itemref, rowitemref)
		local tr = mw.html.create('tr')
		tr:cssText(rowstyle)
		if rowstyle then tr:cssText(rowstyle) end
		if rowitemtype then tr:attr('itemscope itemtype', rowitemtype) end
		if rowitemref then tr:attr('itemref', rowitemref) end

		if header then
			local th = tr:tag('th')
				:attr('scope', "col")
				:attr('colspan', "2")
				:cssText('text-align:center')
				:wikitext(header)
				:addClass(class)
				:cssText(headerstyle)
			th:done()
			return tostring(tr)
		elseif data then
			if label then
				tr:tag('th')
					:attr('scope', "row")
					:cssText('text-align:left;white-space:nowrap')
					:wikitext(label)
					:cssText(labelstyle)
					:done()

				local td = tr:tag('td')
					:wikitext(data)
					:cssText(datastyle)
				if itemtype then td:attr('itemscope itemtype', itemtype) end
				if itemref then td:attr('itemref', itemref) end
				td:done()
			else
				local td = tr:tag('td')
					:wikitext(data)
					:cssText('text-align:center')
					:attr('colspan', "2")
					:cssText(datastyle)
				if itemtype then td:attr('itemscope itemtype', itemtype) end
				if itemref then td:attr('itemref', itemref) end
				td:done()
			end
			return tostring(tr)
		else
		end
		return ""
	end
	
	--[[
	Template:Infobox
	]]
	--ヘッダー部
	local infobox = nil

	if child then
		local bag = mw.html.create()
		if args.title then
			bag:tag('b')
				:attr("itemprop", "name")
				:addClass(args.titleclass)
				:cssText(args.titlestyle)
				:wikitext(args.title)
				:done()
		end
		infobox = bag
	else
		local tbl = mw.html.create('table')
			:addClass(subbox and '' or 'infobox')
			:addClass(args.bodyclass)
			:cssText(subbox and 'min-width:100%;width:calc(100% + 6px);margin:-3px' or 'width:22em')
			:cssText(args.bodystyle)
		if args.bodyitemtype then tbl:attr('itemscope itemtype', args.bodyitemtype) end
		if args.bodyitemref then tbl:attr('itemref', args.bodyitemref) end

tbl:wikitext('<!-- ' .. mw.dumpObject(args) .. ' -->')

		if args.title then
			tbl:tag('caption')
				:attr("itemprop", "name")
				:addClass(args.titleclass)
				:cssText(args.titlestyle)
				:wikitext(args.title)
				:done()
		end
		if args.above then
			local above = tbl:tag('tr')
				:tag('th')
					:attr("colspan", "2")
					:addClass(args.aboveclass)
					:cssText("text-align:center;font-size:125%;font-weight:bold")
					:cssText(args.abovestyle)
					:wikitext(args.above)
			if args.aboveitemprop then above:attr('itemprop', args.aboveitemprop) end
			if args.aboveitemtype then above:attr('itemscope itemtype', args.aboveitemtype) end
			if args.aboveitemref then above:attr('itemref', args.aboveitemref) end
			above:done()
		end
		infobox = tbl
	end

	for k, v in pairs(h.subheader) do
		infobox:wikitext(row(nil, nil, nil, nil, v[1], v.style or h.subheader.style, v.rowstyle, h.subheader.class, v.rowclass, nil, nil, nil, nil, nil, nil, nil))
	end
	for k, v in pairs(h.image) do
		infobox:wikitext(row(nil, nil, nil, nil, v[1] and (v[1] .. (v.caption or '')), v.style or h.image.style, v.rowstyle, h.image.class, v.rowclass, nil, nil, nil, nil, nil, nil, nil))
	end
	
	--本体部ソート
	for k, v in pairs(body) do
		sbody[#sbody + 1] = v
	end
	table.sort(sbody,
		function (a, b) return a[1] < b[1] end
	)
	--本体部
	for k, v in ipairs(sbody) do
		infobox:wikitext(row(v.header, v.headerstyle, v.label, v.labelstyle, v.data, v.datastyle, v.rowstyle, v.class, v.rowclass, v.id, v.itemprop, v.rowitemprop, v.itemtype, v.rowitemtype, v.itemref, v.rowitemref))
	end
	
	--フッター部
	if args.below then
		infobox:tag('tr')
			:tag('td')
				:attr('colspan', "2")
				:addClass(args.belowclass)
				:cssText("text-align:center")
				:cssText("args.belowstyle")
				:wikitext(args.below)
	end
	
	if link then
		--Template:Transclude
		link = string.gsub(link, ':?[Tt]emplate:', '')
		if not string.find(link, ':') then
			link = 'Template:' .. link
		end
		infobox:tag('tr')
			:addClass("noprint")
			:tag("td")
				:cssText("text-align:right;font-size:85%")
				:wikitext('[[' .. link .. '|テンプレートを表示]]')
	end
	return tostring(infobox)
end

return Infobox