モジュール:Navbox/former/sandbox

require('strict')
local p = {}
local cfg = mw.loadData('Module:Navbox/configuration')
--
Define Arguments
local getArgs
local args = {}
local border
local child, none = false, false
local rowspan = 0
local odd, even = 'odd', 'even'
local list, liststyle, listclass = {}, {}, {}
local group, groupstyle = {}, {}
local colheader, colheadercolspan, colheaderstyle = {}, {}, {}
local col, colstyle, colwidth = {}, {}, {}
local colfooter, colfootercolspan, colfooterstyle = {}, {}, {}
local abbr, state = {}, {}
local sect, section = {}, {}
local secttitlestyle = {}
local content, contentstyle = {}
local image, imageleft = {}, {}
local function defArgs(frame, wrappers)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	args = getArgs(frame, {wrappers = wrappers})
	border = args.border or args1
	child, none = (child or border == 'subgroup' or border == 'child'), (border == 'none')
	
	local sortable_mt = {
		__lt = function(a, b) return a.index  plainlist is best-effort to preserve old Common.css ordering.
		-- this ordering is not a guarantee because most navboxes will emit only
		-- one of these classes hlist_note
		return hlist_styles .. plainlist_styles
	end
	
	local function add_navbox_styles(hiding_templatestyles)
		local frame = mw.getCurrentFrame()
		-- This is a lambda so that it doesn't need the frame as a parameter
		local function add_user_styles(templatestyles)
			if templatestyles and templatestyles ~=  then
				return frame:extensionTag{
					name = 'templatestyles', args = { src = templatestyles }
				}
			end
			return 
		end
	
		-- get templatestyles. load base from config so that Lua only needs to do
		-- the work once of parser tag expansion
		local base_templatestyles = cfg.templatestyles
		local templatestyles = add_user_styles(argscfg.arg.templatestyles)
		local child_templatestyles = add_user_styles(argscfg.arg.child_templatestyles)
	
		-- The 'navbox-styles' div exists to wrap the styles to work around T200206
		-- more elegantly. Instead of combinatorial rules, this ends up being linear
		-- number of CSS rules.
		return mw.html.create('div')
			:addClass(cfg.class.navbox_styles)
			:wikitext(
				add_list_styles() .. -- see hlist_note applied to 'before base_templatestyles'
				base_templatestyles ..
				templatestyles ..
				child_templatestyles ..
				table.concat(hiding_templatestyles)
			)
			:done()
	end
	
	-- work around phab:T303378
	-- for each arg: find all the templatestyles strip markers, insert them into a
	-- table. then remove all templatestyles markers from the arg
	local function move_hiding_templatestyles(args)
		local gfind = string.gfind
		local gsub = string.gsub
		local templatestyles_markers = {}
		local strip_marker_pattern = '(\127^\127*UNIQ%-%-templatestyles%-%x+%-QINU^\127*\127)'
		for k, arg in pairs(args) do
			for marker in gfind(arg, strip_marker_pattern) do
				table.insert(templatestyles_markers, marker)
			end
			argsk = gsub(arg, strip_marker_pattern, )
		end
		return templatestyles_markers
	end
	
	local hiding_templatestyles = move_hiding_templatestyles(args)
	local listnums = {}
	
	for k, _ in pairs(args) do
		if type(k) == 'string' then
			local listnum = k:match(cfg.pattern.listnum)
			if listnum then table.insert(listnums, tonumber(listnum)) end
		end
	end
	table.sort(listnums)
	
	local nav
	local bodyTable
	if baseTag == nil or baseTag ==  then
		baseTag = mw.html.create()
	end
	if child then
		baseTag:wikitext()
		nav = baseTag
	elseif not none then
		baseTag:node(add_navbox_styles(hiding_templatestyles))
		nav = baseTag:tag('div')
			:attr('role', 'navigation')
			:addClass('navbox')
			:addClass(args.navboxclass)
			:cssText(args.bodystyle)
			:cssText(args.style)
			:css('padding', '3px')
		if args.title or args.above then
			nav:attr('aria-labelledby', mw.uri.anchorEncode(args.title or args.above))
		else
			nav:attr('aria-label', 'Navbox')
		end
	else
		baseTag:node(add_navbox_styles(hiding_templatestyles))
		nav = baseTag
	end
	
	bodyTable = nav:tag('table')
		:addClass('nowraplinks')
		:addClass(args.bodyclass)
	if args.title and (args.state ~= 'plain' and args.state ~= 'off') then
		if args.state == 'collapsed' then args.state = 'mw-collapsed' end
		bodyTable
			:addClass('mw-collapsible')
			:addClass(args.state or 'autocollapse')
	end
	bodyTable:css('border-spacing', 0)
	if child or border == 'none' then
		bodyTable
			:addClass('navbox-subgroup')
			:cssText(args.bodystyle)
			:cssText(args.style)
	else  -- regular navbox - bodystyle and style will be applied to the wrapper table
		bodyTable
			:addClass('navbox-inner')
			:css('border-spacing', 0)
			:css('background', 'transparent')
			:css('color', 'inherit')
	end
	bodyTable:cssText(args.innerstyle)
	
	return baseTag, bodyTable
end
--
title and navbar
local function title(tbl)
	if not args.title then return tbl end
	local titleRow = tbl:tag('tr')
	local titleCell = titleRow:tag('th'):attr('scope', 'col')
	local titleColspan = 2
	if args.imageleft then titleColspan = titleColspan + 1 end
	if args.image then titleColspan = titleColspan + 1 end
	if args.titlegroup then titleColspan = titleColspan - 1 end
	titleCell
		:cssText(args.basestyle)
		:cssText(args.titlestyle)
		:addClass('navbox-title')
		:attr('colspan', titleColspan)
	
	-- extract text color from css, which is the only permitted inline CSS for the navbar
	local function extract_color(css_str)
		-- return nil because navbar takes its argument into mw.html which handles
		-- nil gracefully, removing the associated style attribute
		return mw.ustring.match(';' .. css_str .. ';', '.*;%s*(CcOoLlOoRr%s*:%s*.-)%s*;') or nil
	end
	
	local function renderNavBar(titleCell)
		if has_navbar() then
			local navbar = require('Module:Navbar/sandbox')._navbar
			titleCell:wikitext(navbar{
				cfg.navbar.name = argscfg.arg.name,
				cfg.navbar.mini = 1,
				cfg.navbar.fontstyle = extract_color(
					(argscfg.arg.basestyle or ) .. ';' .. (argscfg.arg.titlestyle or )
				)
			})
		end
	end
	
	renderNavBar(titleCell)
	
	titleCell
		:tag('div')
			:attr('id', mw.uri.anchorEncode(args.title))
			:addClass(args.titleclass)
			:css('font-size', '110%')	-- :en:Module:Navboxでは114%
			:css('margin', '0 6em')	-- :en:Module:Navboxでは横マージンが4em（日本語版のNavbarは幅が4emを超える）
			:wikitext(processItem(args.title))
	return tbl
end
local function getAboveBelowColspan()
	local ret = 2
	if args.imageleft then ret = ret + 1 end
	if args.image then ret = ret + 1 end
	return ret
end
--
above
local function above(tbl)
	if not args.above then return tbl end
	tbl:tag('tr')
		:tag('td')
			:addClass('navbox-abovebelow')
			:addClass(args.aboveclass)
			:cssText(args.basestyle)
			:cssText(args.abovestyle)
			:attr('colspan', getAboveBelowColspan())
			-- id for aria-labelledby attribute, if no title
			:attr('id', (not args.title) and mw.uri.anchorEncode(args.above) or nil)
			:wikitext(processItem(args.above, args.nowrapitems))
	return tbl
end
	
--
body
--first group/list and images
local function body1(tbl)
	local row = tbl:tag('tr')
	if args.imageleft then
		row
			:tag('td')
				:addClass('noviewer')
				:addClass('navbox-image')
				:addClass(args.imageclass)
				:css('width', '1px')
				:css('padding', '0 2px 0 0')
				:cssText(args.imageleftstyle)
				:attr('rowspan', rowspan)
				:tag('div')
					:wikitext(processItem(args.imageleft))
	end
	local j = list1.index
	if groupj then
		local groupCell = row:tag('th')
		groupCell
			:attr('scope', 'row')
			:addClass('navbox-group')
			:addClass(args.groupclass)
			:cssText(args.basestyle)
			:css('width', args.groupwidth or '1%')
		groupCell
			:cssText(args.groupstyle)
			:cssText(groupstylej)
			:wikitext(groupj)
	end
	local listCell = row:tag('td')
	if groupj then
		listCell
			:addClass('navbox-list-with-group')
	else
		listCell:attr('colspan', 2)
	end
	if not args.groupwidth then
		listCell:css('width', '100%')
	end
	local rowstyle
	if odd == 'odd' then
		rowstyle = args.oddstyle
	else
		rowstyle = args.evenstyle
	end
	listCell
		:css('padding', '0')
		:cssText(args.liststyle)
		:cssText(rowstyle)
		:cssText(liststylej)
		:addClass('navbox-list')
		:addClass('navbox-' .. (args.evenodd == 'swap' and even or args.evenodd or odd))
		:addClass(args.listclass)
		:addClass(listclassj)
		:tag('div')
			:css('padding', (args.list1padding or args.listpadding or '0 0.25em'))
			:wikitext(processItem(list1.content, args.nowrapitems))
	if args.image then
		row
			:tag('td')
				:addClass('noviewer')
				:addClass('navbox-image')
				:addClass(args.imageclass)
				:css('width', '1px')
				:css('padding', '0 0 0 2px')
				:cssText(args.imagestyle)
				:attr('rowspan', rowspan)
				:tag('div')
					:wikitext(processItem(args.image))
	end
	return tbl
end
--remaining groups/lists
local function body2(tbl)
	for i = 2, #list do
		odd, even = even, odd
		local j = listi.index
		local row = tbl:tag('tr')
		if groupj then
			local groupCell = row:tag('th')
			groupCell
				:attr('scope', 'row')
				:addClass('navbox-group')
				:addClass(args.groupclass)
				:cssText(args.basestyle)
				:css('width', args.groupwidth or '1%')
			groupCell
				:cssText(args.groupstyle)
				:cssText(groupstylej)
				:wikitext(groupj)
		end
		local listCell = row:tag('td')
		if groupj then
			listCell:addClass('navbox-list-with-group')
		else
			listCell:attr('colspan', 2)
		end
		if not args.groupwidth then
			listCell:css('width', '100%')
		end
		local rowstyle
		if odd == 'odd' then
			rowstyle = args.oddstyle
		else
			rowstyle = args.evenstyle
		end
		listCell
			:css('padding', '0')
			:cssText(args.liststyle)
			:cssText(rowstyle)
			:cssText(liststylej)
			:addClass('navbox-list')
			:addClass('navbox-' .. (args.evenodd == 'swap' and even or args.evenodd or odd))
			:addClass(args.listclass)
			:addClass(listclassj)
			:tag('div')
				:css('padding', (args.listpadding or '0 0.25em'))
				:wikitext(processItem(listi.content, args.nowrapitems))
	end
	return tbl
end
--
below
local function below(tbl)
	if not args.below then return tbl end
	tbl:tag('tr')
		:tag('td')
			:addClass('navbox-abovebelow')
			:addClass(args.belowclass)
			:cssText(args.basestyle)
			:cssText(args.belowstyle)
			:attr('colspan', getAboveBelowColspan())
			:wikitext(processItem(args.below, args.nowrapitems))
	return tbl
end
--
Template:Navbox
function p.navbox(frame)
	defArgs(frame, {'Template:Navbox', 'Template:Navbox subgroup'})
	local res
	local firstTableTag
	res, firstTableTag = top()
	firstTableTag = title(firstTableTag)
	firstTableTag = above(firstTableTag)
	if list1 then
		firstTableTag = body1(firstTableTag)
	end
	firstTableTag = body2(firstTableTag)
	firstTableTag = below(firstTableTag)
	if child then
		res:wikitext()
	end
	return tostring(res)
end
--
Template:Navbox subgroup
function p.subgroup(frame)
	child = true
	return p.navbox(frame)
end
--
Template:Navbox with columns
function p.with_columns(frame)
	defArgs(frame, 'Template:Navbox with columns')
	table.sort(col)
	local res
	local firstTableTag
	res, firstTableTag = top()
	
	firstTableTag = title(firstTableTag)
	firstTableTag = above(firstTableTag)
	
	if col1 then
		local j = col1.index
		local cols = mw.html.create('table')
			:addClass('navbox-columns-table')
			:css('border-spacing', '0px')
			:css('text-align', 'left')
			:cssText((colheaderj or args.fullwidth) and 'width:100%' or 'width:auto; margin-left:auto; margin-right:auto')
			:cssText(args.coltablestyle)
		local row
		
		--Header row
		if colheaderj then
			row = cols:tag('tr')
			for i = 1, #col do
				local j = coli.index
				if colheaderj then
					local td = row:tag('td')
						:addClass('navbox-abovebelow')
						:cssText((i > 1) and 'border-left:2px solid #fdfdfd' or nil)
						:css('font-weight', 'bold')
						:cssText(args.colheaderstyle)
						:cssText(colheaderstylej)
						:wikitext(colheaderj)
					if tonumber(colheadercolspanj) then
						td:attr('colspan', colheadercolspanj)
					end
				end
			end
		end
		
		--Main columns
		row = cols:tag('tr'):css('vertical-align', 'top')
		if not (colheaderj or colfooterj or args.fullwidth) then
			local paddingoff = args.padding and string.find(args.padding, '^0ep?mx?%?;?')
			if not paddingoff then
				row:tag('td')
					:css('width', args.padding or '5em')
					:wikitext('   ')
			end
		end
		for i = 1, #col do
			local j = coli.index
			row:tag('td')
				:addClass('navbox-list')
				:cssText((i > 1) and 'border-left:2px solid #fdfdfd' or nil)
				:css('padding', '0px')
				:cssText(args.colstyle)
				:cssText(args.oddcolstyle)
				:cssText(colstylej)
				:css('width', colwidthj or args.colwidth or '10em')
				:tag('div')
					:newline()
					:wikitext(coli.content)
					:newline()
			args.oddcolstyle, args.evencolstyle = args.evencolstyle, args.oddcolstyle
		end
		
		--Footer row
		if colfooterj then
			row = cols:tag('tr')
			for i = 1, #col do
				local j = coli.index
				if colfooterj then
					local td = row:tag('td')
						:addClass('navbox-abovebelow')
						:cssText((i > 1) and 'border-left:2px solid #fdfdfd' or nil)
						:css('font-weight', 'bold')
						:cssText(args.colfooterstyle)
						:cssText(colfooterstylej)
						:wikitext(colfooterj)
					if tonumber(colheadercolspanj) then
						td:attr('colspan', colheadercolspanj)
					end
				end
			end
		end
		table.insert(list, {index=1, content=tostring(cols)})
	end
	
	args.list1padding = '0px'
	args.liststyle = 'background:transparent;color:inherit;'
	
	firstTableTag = body1(firstTableTag)
	firstTableTag = body2(firstTableTag)
	firstTableTag = below(firstTableTag)
	return tostring(res)
end
--
Template:Navbox with collapsible groups
function p.with_collapsible_groups(frame)
	local res
	local firstTableTag
	defArgs(frame, 'Template:Navbox with collapsible groups')
	table.sort(content)
	res, firstTableTag = top()
	firstTableTag = title(firstTableTag)
	firstTableTag = above(firstTableTag)
	
	local i = 1
	local function funcList(rowTag)
		listi = listi or contenti
		local j = listi.index
		args.state = statej or args.selected and (args.selected == abbrj or args.selected == groupj) and  or 'mw-collapsed'
		args.name = nil
		args.titlestyle = (args.basestyle or ) .. ';' .. (args.groupstyle or ) .. ';' .. (args.secttitlestyle or ) .. ';' .. (groupstylej or )
		args.liststyle = (args.liststyle or ) .. ';' .. (args.contentstyle or ) .. ';' .. (liststylej or )
		args.title, groupj = groupj, nil
		args.image = imagej
		args.imageleft = imageleftj
		local baseTag = rowTag:tag('td')
			:cssText(args.groupwidth and  or 'width:100%;')
			:css('padding', '0')
			:cssText(args.liststyle)
			:cssText(odd == 'odd' and (args.oddstyle or ) or (args.evenstyle or ))
			:cssText(liststylej)
			:addClass('navbox-list')
			:addClass('navbox-' .. (args.evenodd == 'swap' and even or args.evenodd or odd))
			:addClass(args.listclass)
			:tag('div')
		if args.title then
			local tableTag
			none = true
			baseTag, tableTag = top(baseTag)
			tableTag = title(tableTag)
			tableTag = body1(tableTag)
		if child then
		    baseTag:wikitext()
			end
		else
			baseTag:wikitext(listi.content)
		end
	end
	
	--i = 1
	local row = firstTableTag:tag('tr')
	if args.imageleft then
		row:tag('td')
			:addClass('navbox-image')
			:addClass(args.imageclass)
			:css('width', '0')
			:css('padding', '0 2px 0 0')
			:cssText(args.imageleftstyle)
			:attr('rowspan', rowspan)
			:wikitext(args.imageleft)
	end
	funcList(row)
	if args.image then
		row:tag('td')
			:css('width', '0%')
			:css('padding', '0 0 0 2px')
			:cssText(args.imagestyle)
			:attr('rowspan', rowspan)
			:wikitext(args.image)
	end
	--i > 2
	for i = 2, #list do
		row = firstTableTag:tag('tr')
		list1 = listi
		funcList(row)
	end
	
	firstTableTag = below(firstTableTag)
	if child then
		res:wikitext()
	end
	child, none = (child or border == 'subgroup' or border == 'child'), (border == 'none') --再定義
	return tostring(res)
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
				argsi = nil
			end
		end
		local numD = numend - numstart + 1
		for i = 1, numD, numstep do
			argsi + numtab + 1 = numstart + i - 1
		end
	end
	local res = mw.html.create('table')
	res
		:css('width', '100%')
		:css('border-spacing', 0)
		:css('text-align', 'center')
	local h = 1
	repeat
		h = h + 1
		local row = res:tag('tr')
		for i = 2, 11 do
			local tdTag = row:tag('td')
				:css('width', '10%')
			if (tonumber(argsi) or -1) > 0 then
				tdTag:wikitext( .. fmtLink(args1, argsi) .. '|' .. argsi .. )
			else
				tdTag:wikitext(argsi)
			end
			argsi, argsi + 10 = argsi + 10, argsi + h * 10
		end
	until not args2
	return tostring(res)
end
local function years(frame, fmtLink, wrappers)
	defArgs(frame, wrappers)
	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 )
		local res
		local firstTableTag
		res, firstTableTag = top()
		if args.title then 
			firstTableTag = title(firstTableTag)
		end
		if args.above then
			firstTableTag = above(firstTableTag)
		end
		if not list1 and args1 then list1 = {index = 1, content = calc_years(args, fmtLink)} end
		if list1 then
			firstTableTag = body1(firstTableTag)
		end
		firstTableTag = body2(firstTableTag)
		if args.below then
			firstTableTag = below(firstTableTag)
		end
		return tostring(res)
	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, 'Template:NavboxYears')
end
function p.years2(frame)
	local fmtLink = function(str, num) return num .. str end
	return years(frame, fmtLink, 'Template:NavboxYears2')
end
return p