memory alpha
Module documentation ()

This module implements the {{nav}} template.

Module source

local p = {}

local function templateStyles(tsPage)
	return mw.getCurrentFrame():extensionTag{
		name = 'templatestyles',
		args = {src = tsPage}
	}
end

local function navbarLink(abbr, title, specialPage)
	local args = mw.getCurrentFrame():getParent().args
	local target = 'Template:' .. args['name']
	local dt = '<abbr title="' .. title .. '">' .. abbr .. '</abbr>'
	if specialPage then
		target = 'Special:' .. specialPage .. '/' .. target
	end
	return '[[' .. target .. '|' .. dt .. ']]'
end

function p.navbox(frame)
	local args = frame:getParent().args
	local labels = {}
	local lists = {}
	local cols = 1
	local state = 'autocollapse'
	local templatestyles = ''
	local categories = ''
	
	templatestyles = templatestyles .. templateStyles('Module:Nav/styles.css')
	templatestyles = templatestyles .. templateStyles('Hlist/styles.css')
	
	for k, v in pairs(args) do
		if mw.ustring.gsub(k, '%d*$', '') == 'label' and v ~= '' then
			table.insert(labels, k)
		end
		if mw.ustring.gsub(k, '%d*$', '') == 'ts' and v ~= '' then
			templatestyles = templatestyles .. templateStyles(v)
		end
	end
	
	for k, v in pairs(args) do
		if mw.ustring.gsub(k, '%d*$', '') == 'list' and v ~= '' then
			table.insert(lists, k)
		end
	end
	
	table.sort(labels)
	table.sort(lists)
	
	if args['state'] == 'open' then state = '' elseif args['state'] == 'closed' then state = 'mw-collapsed' end
	if #labels > 0 then cols = '2' end
	
	local navbox = mw.html.create('table')
		:addClass('navbox hlist mw-collapsible')
		:addClass(state)
		:addClass(args['class'] or '')
		:attr('role', 'navigation')
		:attr('aria-labelledby', mw.uri.anchorEncode(args['title']))
		:attr('data-expandtext', 'show')
		:attr('data-collapsetext', 'hide')
		:tag('tr')
		:tag('th')
		:addClass('navbox-header')
		:attr('scope', 'col')
		:attr('colspan', cols)
	
	if args['name'] and args['name'] ~= '' then
		navbox = navbox
			:tag('div')
			:addClass('navbar hlist')
			:tag('ul')
			:tag('li')
			:wikitext(navbarLink('v', 'View this template'))
			:done()
			:tag('li')
			:wikitext(navbarLink('t', 'Discuss this template', 'TalkPage'))
			:done()
			:tag('li')
			:wikitext(navbarLink('e', 'Edit this template', 'EditPage'))
			:done()
			:done()
			:done()
	else
		-- categories = categories .. '[[Category:Memory Alpha articles with incorrect tag]]'
	end
	
	navbox = navbox
		:tag('div')
		:attr('id', mw.uri.anchorEncode(args['title']))
		:wikitext(args['title'])
		:allDone()
	
	for k, v in ipairs(lists) do
		local group = mw.html.create('tr'):addClass('navbox-group')
		local cell = mw.html.create('td')
		local label = args['label' .. mw.ustring.gsub(v, '^list', '')]
		local list = args[v]
		
		if label and label ~= '' then group
			:tag('th')
			:attr('scope', 'row')
			:wikitext(label)
			:allDone()
		elseif cols == '2' then
			cell:attr('colspan', cols)
		end
		
		cell
			:newline()
			:wikitext(list)
			:newline()
			:allDone()
		
		group:node(cell)
		navbox:node(group)
	end
	
	templatestyles = mw.html.create('div')
		:addClass('navbox-styles')
		:wikitext(templatestyles)
	
	return tostring(templatestyles) .. tostring(navbox) .. categories
end

return p