モジュール:Navbox/former

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

p = {}

--[[
Template:Tnavbar
]]
function p.tnavbar(frame)
	local args = frame.args
	if not args[1] then return '' end
	
	local function tf(x)
		return x == '1' and true or false
	end
	args.plain = tf(args.plain)
	args.div = tf(args.div)
	args.nodiv = tf(args.nodiv)
	args.mini = tf(args.mini)
	args.viewplain = tf(args.viewplain)
	if args.miniv == '1' then args.mini, args.viewplain = true, true end
	args.fontstyle = args.fontcolor ~= '' and ((args.fontstyle or '') .. ';color:' .. args.fontcolor .. ';') or args.fontstyle or ''
	
	local r = {open = '<div class="noprint plainlinks navbar hlist" style="white-space:nowrap;font-size:60%;font-weight:normal;', content = '', close = '</div>'} --出力用
	if args.nodiv then
		r.open = r.open .. 'display:inline;padding:0 0.5em;'
	else
		r.open = r.open .. 'background-color:transparent;padding:0;color:#000;'
	end
	r.open = r.open .. args.fontstyle .. (args.style or '') .. '">'
	
	if not (args.plain or args.mini or args.viewplain) then
		r.content = '<span style="font-size:125%;">このテンプレートを:&nbsp;</span>'
	end
	
	local disp = args.mini and {'表', '話', '編', '歴'} or {'表示', 'ノート', '編集', '履歴'}
	local ns = {'Template:', 'Template‐ノート:', 'Template:', 'Template:'}
	local query = {nil, nil, 'action=edit', 'action=history'}
	local title = {'このテンプレートを表示します', 'このテンプレートのノートを表示します', 'このテンプレートを編集します。保存の前にプレビューを忘れずに。', 'このテンプレートの過去の版を表示します'}
	local color = {'', 'color:#002bb8;', 'color:#002bb8;', 'color:#002bb8;'}
	local i = 0
	local i_end = args.viewplain and 1 or 4
	r.content = r.content .. '<ul style="display:inline;">'
	for i = 1, i_end do
		local l = {open = '', link = '', close = ''}
		if query[i] then
			l.open = '['
			l.link = mw.uri.decode(tostring(mw.uri.canonicalUrl(ns[i] .. args[1], query[i]))) .. ' '
			l.close = ']'
		else
			l.open = '[['
			l.link = ns[i] .. args[1] .. '|'
			l.close = ']]'
		end
		r.content = r.content .. '<li>' .. l.open .. l.link .. '<span title="' .. title[i] .. '" style="font-size:125%;' .. color[i] .. args.fontstyle .. '">' .. disp[i] .. '</span>' .. l.close
	end
	r.content = r.content .. '</ul>'
	
	return r.open .. r.content .. r.close
end


--[[
Define Arguments
]]
local function defArgs(frame)
	--グローバル変数として定義
	args = require('Module:Arguments').getArgs(frame, {parentOnly = true})
	border = args.border or args[1]
	child, none = (border == 'subgroup' or border == 'child'), (border == 'none')
	collapsible = (args.state == 'plain' or args.state == 'off') and '' or 'collapsible '
	colspan = 2 + (args.imageleft and 1 or 0) + (args.image and 1 or 0)
	
	if args.basestyle then
		basestyle = args.basestyle .. ';'
	else
		basestyle = ''
	end
	bdcolor = args.style and string.match(args.style, 'background.-:(.-);') or '#fff'	--ボーダー色
	trborder = '<tr style="border-width:2px;border-style:solid none;border-color:' .. bdcolor .. '">' --ボーダーつきtr
	
	list, liststyle = {}, {}
	group, groupstyle = {}, {}
	
	for k, v in pairs(args) do
		if string.match(k, 'list%d+$') then
			list[tonumber(string.match(k, 'list(%d+)$'))] = '\n' .. v
			v = nil
		elseif string.match(k, 'list%d+style') then
			liststyle[tonumber(string.match(k, 'list(%d+)style'))] = v
			v = nil
		elseif string.match(k, 'group%d+$') then
			group[tonumber(string.match(k, 'group(%d+)$'))] = v
			v = nil
		elseif string.match(k, 'group%d+style') then
			groupstyle[tonumber(string.match(k, 'group(%d+)style'))] = v
			v = nil
		end
	end
	
	rowspan = (#list - 1) * 2 - 1
	odd, even = 'odd', 'even'
	
	result = '' --出力用
end

--[[
top
]]
local function top()
	if child then
		result = result .. '</div>'
	elseif not none then
		result = result .. '<table class="navbox" style="border-collapse:collapse;' .. (args.bodystyle or '') .. ';' .. (args.style or '') .. '"><tr><td>'
	end
	
	result = result .. '<table class="' .. (args.nowraplinks or '') .. (args.bodyclass or '')
	if args.title then
		result = result .. ' ' .. collapsible .. (args.state or ' autocollapse')
	end
	if child or none then
		result = result .. ' navbox-subgroup" style="margin:-2px 0;' .. (args.bodystyle or '') .. (args.style or '')
	else
		result = result .. '" style="background:transparent;color:inherit;border:1px solid ' .. bdcolor
	end
	
	result = result .. ';width:100%;border-spacing:0;border-collapse:collapse;' .. (args.innerstyle or '') .. '">'
end

--[[
title and navbar
]]
local function title()
	result = result .. trborder
	if args.titlegroup then
		result = result .. '<td class="navbox-group ' .. (args.titlegroupclass or '') .. '" style="' .. basestyle  .. (args.groupstyle or '') .. ';' .. (args.titlegroupstyle or '') .. '">' .. args.titlegroup .. '</td><th style="border-left:2px solid #fdfdfd;width:100%;'
	else
		result = result .. '<th style="'
	end
	result = result .. basestyle .. (args.titlestyle or '') .. '" colspan=' .. (colspan - (args.titlegroup and 1 or 0)) .. ' class="navbox-title">'
	
	if (args.navbar == 'plain' or args.navbar == 'off') or (not args.name and (child or none)) then
		if args.navbar == 'off' then
			if args.state == 'plain' then
				result = result .. '<div style="float:right;width:6em;">&nbsp;</div>'
			end
		else
			if args.state ~= 'plain' then
				result = result .. '<div style="float:left;width:6em;text-align:left;">&nbsp;</div>'
			end
		end
	else
		local tbl = {args = {args.name, mini = '1', fontstyle = basestyle .. (args.titlestyle or '') .. 'border:none;', fontcolor = ''}}
		result = result .. '<div style="float:left;width6em;text-align:left;">' .. p.tnavbar(tbl) .. '</div>'
		if args.state == 'plain' then
			result = result .. '<div style="float:right;width:6em;">&nbsp;</div>'
		end
	end
	
	if args.titleclass then
		args.titleclass = ' class="' .. args.titleclass .. '"'
	else
		args.titleclass = ''
	end
	result = result .. '<span ' .. args.titleclass .. ' style="font-size:' .. ((child or none) and '100' or '110') .. '%;">' .. args.title .. '</span></th></tr>'
end
	
--[[
above
]]
local function above()
	result = result .. trborder .. '<td class="navbox-abovebelow ' .. (args.aboveclass or '') .. '" style="' .. basestyle .. (args.abovestyle or '') .. '" colspan=' .. colspan .. '>\n' .. args.above .. '</td></tr>'
end
	
--[[
body
]]
--first group/list and images
local function body1()
	result = result .. trborder
	if args.imageleft then
		result = result .. '<td style="width:0;padding:0 2px 0 0;' .. (args.imageleftstyle or '') .. '" rowspan=' .. rowspan .. '>' .. args.imageleft .. '</td>'
	end
	if group[1] then
		result = result .. '<td class="navbox-group ' .. (args.groupclass or '') .. '" style="border-right:2px solid ' .. bdcolor .. ';' .. basestyle .. (args.groupwidth and ('width:' .. args.groupwidth .. ';') or '') .. (args.groupstyle or '') .. ';' .. (groupstyle[1] or '') .. '">' .. group[1] .. '</td><td style="text-align:left;'
	else
		result = result .. '<td colspan=2 style="'
	end
	result = result .. (args.groupwidth and '' or 'width:100%;') .. 'padding:0;' .. (args.liststyle or '') .. ';' .. (odd == 'odd' and (args.oddstyle or '') or (args.evenstyle or '')) .. ';' .. (liststyle[1] or '') .. '" class="navbox-list navbox-' .. (args.evenodd == 'swap' and even or args.evenodd or odd) .. ' ' .. (args.listclass or '') .. '"><div style="padding:' .. (args.list1padding or args.listpadding or '0 0.25em') .. '">' .. list[1] .. '</div></td>'
	if args.image then
		result = result .. '<td style="width:0%;padding:0 0 0 2px;' .. (args.imagestyle or '') .. '" rowspan=' .. rowspan .. '>' .. args.image .. '</td>'
	end
	result = result .. '</tr>'
end

--remaining groups/lists
local function body2()
	list[1] = nil
	for i in pairs(list) do
		odd, even = even, odd
		result = result .. trborder
		if group[i] then
			result = result .. '<td class="navbox-group ' .. (args.groupclass or '') .. '" style="border-right:2px solid ' .. bdcolor .. ';' .. basestyle .. (args.groupwidth and ('width:' .. args.groupwidth .. ';') or '') .. (args.groupstyle or '') .. ';' .. (groupstyle[i] or '') .. '">' .. group[i] .. '</td><td style="text-align:left;'
		else
			result = result .. '<td colspan=2 style="'
		end
		result = result .. (args.groupwidth and '' or 'width:100%;') .. 'padding:0;' .. (args.liststyle or '') .. ';' .. (odd == 'odd' and (args.oddstyle or '') or (args.evenstyle or '')) .. ';' .. (liststyle[i] or '') .. '" class="navbox-list navbox-' .. (args.evenodd == 'swap' and even or args.evenodd or odd) .. ' ' .. (args.listclass or '') .. '"><div style="padding:' .. (args.listpadding or '0 0.25em') .. '">' .. list[i] .. '</div></td></tr>'
	end
end

--[[
below
]]
local function below()
	result = result .. trborder .. '<td class="navbox-abovebelow ' .. (args.belowclass or '') .. '" style="' .. basestyle .. (args.belowstyle or '') .. '" colspan=' .. colspan .. '>\n' .. args.below .. '</td></tr>'
end
	
--[[
close
]]
local function close()
	result = result .. '</table>'
	
	if child then
		result = result .. '<div>'
	elseif not none then
		result = result .. '</table>'
	end
end

--[[
Template:Navbox
]]
function p.navbox(frame)
	defArgs(frame)
	top()
	if args.title then title() end
	if args.above then above() end
	if list[1] then body1() end
	body2()
	if args.below then below() end
	close()
	return result
end

--[[
Template:Navbox subgroup
]]
function p.subgroup(frame)
	defArgs(frame)
	if not border then child = true end
	args.groupstyle = 'padding:' .. (args.grouppadding or '0 0.75em') .. ';' .. (args.groupstyle or '')
	top()
	if args.title then title() end
	if args.above then above() end
	if list[1] then body1() end
	body2()
	if args.below then below() end
	close()
	return result
end

--[[
Template:Navbox with columns
]]
function p.with_columns(frame)
	defArgs(frame)
	local colheader, colheadercolspan, colheaderstyle = {}, {}, {}
	local col, colstyle, colwidth = {}, {}, {}
	local colfooter, colfootercolspan, colfooterstyle = {}, {}, {}
	for k, v in pairs(args) do
		if string.match(k, 'col%d+header$') then
			colheader[tonumber(string.match(k, 'col(%d+)header$'))] = v
		elseif string.match(k, 'col%d+headercolspan') then
			colheadercolspan[tonumber(string.match(k, 'col(%d+)headercolspan'))] = v
		elseif string.match(k, 'col%d+headerstyle') then
			colheaderstyle[tonumber(string.match(k, 'col(%d+)headerstyle'))] = v
		elseif string.match(k, 'col%d+$') then
			col[tonumber(string.match(k, 'col(%d+)$'))] = v
		elseif string.match(k, 'col%d+style') then
			colstyle[tonumber(string.match(k, 'col(%d+)style'))] = v
		elseif string.match(k, 'col%d+width') then
			colwidth[tonumber(string.match(k, 'col(%d+)width'))] = v
		end
	end
	top()
	if args.title then title() end
	if args.above then above() end
	
	local cols = '</div><table class="navbox-columns-table" style="border-spacing:0;border-collapse:collapse;text-align:left;' .. ((colheader[1] or args.fullwidth) and 'width:100%;' or 'margin:0 auto;') .. (args.coltablestyle or '') .. '">'
	--Header row
	if colheader[1] then
		cols = cols .. '<tr class="navbox-abovebelow" style="border-bottom:2px solid #fdfdfd;font-weight:bold;' .. (args.colheaderstyle or '') .. '"><td colspan=' .. (colheadercolspan[1] or '1') .. ' style="border-right:2px solid #fdfdfd;' .. (colheaderstyle[1] or '') .. '">' .. colheader[1] .. '</td>'
		colheader[1] = nil
		for i in pairs(colheader) do
			cols = cols .. '<td colspan=' .. (colheadercolspan[i] or '1') .. ' style="border-left:2px solid #fdfdfd;' .. (colheaderstyle[i] or '') .. '">' .. colheader[i] .. '</td>'
		end
		cols = cols .. '</tr>'
	end
	--Main columns
	if col[1] then
		cols = cols .. '<tr style="vertical-align:top;' .. (args.colstyle or '') .. '">'
		if colheader[1] or colfooter[1] or args.fullwidth then
			if args.padding then args.padding = string.gsub(args.padding, '^0[ep]?[mx]?%?;?', 'off') end
			if args.padding == 'off' then
				cols = cols .. '<td style="width:' .. (args.padding or '5em;') .. '">&nbsp;&nbsp;&nbsp;</td>'
			end
		end
		cols = cols .. '<td style="border-right:2px solid #fdfdfd;padding:0;' .. (args.oddcolstyle or '') .. ';' .. (colstyle[1] or '') .. ';width:' .. (colwidth[1] or args.colwidth or '10em;') .. '"><div>' .. col[1] .. '</div></td>'
		col[1] = nil
		for i in pairs(col) do
			cols = cols .. '<td style="border-left:2px solid #fdfdfd;padding:0;' .. (args.oddcolstyle or '') .. ';' .. (colstyle[i] or '') .. ';width:' .. (colwidth[i] or args.colwidth or '10em;') .. '"><div>' .. col[i] .. '</div></td>'
			args.oddcolstyle, args.evencolstyle = args.evencolstyle, args.oddcolstyle
		end
		cols = cols .. '</tr>'
	end
	--Footer row
	if colfooter[1] then
		cols = cols .. '<tr class="navbox-abovebelow" style="font-weight:bold;' .. (args.colfooterstyle or '') .. '"><td colspan=' .. (colfootercolspan[1] or '1') .. ' style="border-right:2px solid #fdfdfd;' .. (colfooterstyle[1] or '') .. '">' .. colfooter[1] .. '</td>'
		colfooter[1] = nil
		for i in pairs(colfooter) do
			cols = cols .. '<td colspan=' .. (colfootercolspan[i] or '1') .. ' style="border-left:2px solid #fdfdfd;' .. colfooterstyle[i] .. '">' .. colfooter[i] .. '</td>'
		end
		cols = cols .. '</tr>'
	end
	cols = cols .. '</table><div>'
	table.insert(list, 1, cols)
	
	body1()
	body2()
	if args.below then below() end
	close()
	return result
end

--[[
Template:Navbox with collapsible groups
]]
function p.with_collapsible_groups(frame)
	defArgs(frame)
	local abbr, state = {}, {}
	local sect, section = {}, {}
	local secttitlestyle = {}
	local content, contentstyle = {}
	local image, imageleft = {}, {}
	for k, v in pairs(args) do
		if string.match(k, 'abbr%d+$') then
			abbr[tonumber(string.match(k, 'abbr(%d+)$'))] = v
		elseif string.match(k, 'state%d+$') then
			state[tonumber(string.match(k, 'state(%d+)$'))] = v
		elseif string.match(k, 'sect%d+$') then
			group[tonumber(string.match(k, 'sect(%d+)$'))] = v
		elseif string.match(k, 'section%d+') then
			group[tonumber(string.match(k, 'section(%d+)'))] = v
		elseif string.match(k, 'sect%dtitlestyle') then
			groupstyle[tonumber(string.match(k, 'sect(%d+)titlestyle'))] = v
		elseif string.match(k, 'content%d+$') then
			list[tonumber(string.match(k, 'content(%d+)$'))] = v
		elseif string.match(k, 'content%d+style') then
			liststyle[tonumber(string.match(k, 'content(%d+)style'))] = v
		elseif string.match(k, 'image%d+') then
			image[tonumber(string.match(k, 'image(%d+)'))] = v
		elseif string.match(k, 'imageleft%d+') then
			imageleft[tonumber(string.match(k, 'imageleft(%d+)'))] = v
		end
	end
	top()
	if args.title then title() end
	if args.above then above() end
	
	local function funcList()
		collapsible = (args.selected == abbr[i] or args.selected == group[i]) and '' or 'collapsible '
		args.name = nil
		args.state = collapsible .. (state[i] or 'collapsed')
		args.titlestyle = basestyle .. (args.groupstyle or '') .. ';' .. (args.secttitlestyle or '') .. ';' .. (groupstyle[i] or '')
		args.liststyle = (args.liststyle or '') .. ';' .. (args.contentstyle or '') .. ';' .. (liststyle[i] or '')
		args.title, group[i] = group[i], nil
		args.image = image[i]
		args.imageleft = imageleft[i]
		colspan = 2 + (args.imageleft and 1 or 0) + (args.image and 1 or 0)
		rowspan = 1
		result = result .. '<td style="' .. (args.groupwidth and '' or 'width:100%;') .. 'padding:0;' .. (args.liststyle or '') .. ';' .. (odd == 'odd' and (args.oddstyle or '') or (args.evenstyle or '')) .. ';' .. (liststyle[i] or '') .. '" class="navbox-list navbox-' .. (args.evenodd == 'swap' and even or args.evenodd or odd) .. ' ' .. (args.listclass or '') .. '"><div>'
		if args.title then
			if i ~= 1 then list[1], list[i] = list[i], nil end
			none = true
			top()
			title()
			body1()
			close()
		else
			result = result .. (list[i] or '')
		end
		result = result .. '</div></td></tr>'
	end
	
	--i = 1
	i = 1
	result = result .. trborder
	if args.imageleft then
		result = result .. '<td style="width:0;padding:0 2px 0 0;' .. (args.imageleftstyle or '') .. '" rowspan=' .. rowspan .. '>' .. args.imageleft .. '</td>'
	end
	funcList()
	if args.image then
		result = result .. '<td style="width:0%;padding:0 0 0 2px;' .. (args.imagestyle or '') .. '" rowspan=' .. rowspan .. '>' .. args.image .. '</td>'
	end
	list[1] = nil
	--i > 2
	for j in pairs(list) do
		result = result .. trborder
		i = j
		funcList()
	end
	
	if args.below then below() end
	child, none = (border == 'subgroup' or border == 'child'), (border == 'none') --再定義
	close()
	return result
end

--[[
Template:NavboxYears
Template:NavboxYears2
]]
local function calc_years(args, fmtLink)
	local numtab = tonumber(args.tab) or 0
	local numstart = tonumber(args.start)
	local numend = tonumber(args['end'])
	local numstep = tonumber(args.step) or 1
	if numstart and numend then
		if numtab > 0 then
			for i = 2, numtab + 1 do
				args[i] = nil
			end
		end
		local numD = numend - numstart + 1
		for i = 1, numD, numstep do
			args[i + numtab + 1] = numstart + i - 1
		end
	end
	local result = '<table style="width:100%;border-spacing:0;border-collapse:collapse;text-align:center;">' 
	local h = 1
	repeat
		h = h + 1
		result = result .. '<tr>'
		for i = 2, 11 do
			result = result .. '<td style="width:10%;">'
			if (tonumber(args[i]) or -1) > 0 then
				result = result .. ('[[' .. fmtLink(args[1], args[i]) .. '|' .. args[i] .. ']]')
			else
				result = result .. (args[i] or '')
			end
			result = result .. '</td>'
			args[i], args[i + 10] = args[i + 10], args[i + h * 10]
		end
		result = result .. '</tr>'
	until not args[2]
	result = result .. '</table>'
	return result
end

local function years(frame, fmtLink)
	defArgs(frame)
	if args.var then fmtLink = function(str, num) return str:gsub(args.var, num) end end
	if args.name then
		args.style = 'width:' .. (args.width or '38em') .. ';' .. (args.style or '')
		top()
		if args.title then title() end
		if args.above then above() end
		if not list[1] and args[1] then list[1] = calc_years(args, fmtLink) end
		if list[1] then body1() end
		body2()
		if args.below then below() end
		close()
		return result
	else
		return calc_years(args, fmtLink)
	end
end

function p.years(frame)
	local fmtLink = function(str, num) return str .. num end
	return years(frame, fmtLink)
end

function p.years2(frame)
	local fmtLink = function(str, num) return num .. str end
	return years(frame, fmtLink)
end

return p