Skip to content

Module:Controls

From Whitecoast City Railway Wiki
Revision as of 14:20, 30 October 2026 by Whitecoast City Railway (talk | contribs) (Set up the wiki)

(diff) ← Older revision | Published version (diff) | Latest revision (diff) | Newer revision → (diff)

Revision as of 14:20, 30 October 2026 by Whitecoast City Railway (talk | contribs) (Set up the wiki)

(diff) ← Older revision | Published version (diff) | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Controls/doc

-- A table of keys and what they do: {{Controls}}.
local p = {}

-- One key, or keys pressed together ("Shift+W"), as keycaps.
local function keys( text )
	local caps = {}
	for key in mw.text.gsplit( text, '+', true ) do
		key = mw.text.trim( key )
		if key ~= '' then
			table.insert( caps, tostring( mw.html.create( 'kbd' ):addClass( 'wcr-key' ):wikitext( key ) ) )
		end
	end
	return tostring( mw.html.create( 'span' ):addClass( 'wcr-keys' )
		:wikitext( table.concat( caps, ' + ' ) ) )
end

-- Keys that do the same thing ("W/Up"), each as keycaps, joined with "or".
local function choices( text )
	local each = {}
	for choice in mw.text.gsplit( text, '/', true ) do
		choice = mw.text.trim( choice )
		if choice ~= '' then
			table.insert( each, keys( choice ) )
		end
	end
	return table.concat( each, ' or ' )
end

function p.main( frame )
	local args = frame:getParent().args
	local tbl = mw.html.create( 'table' ):addClass( 'wikitable wcr-controls' )
	if args.title and mw.text.trim( args.title ) ~= '' then
		tbl:tag( 'caption' ):wikitext( mw.text.trim( args.title ) )
	end
	local head = tbl:tag( 'tr' )
	head:tag( 'th' ):attr( 'scope', 'col' ):wikitext( 'Key' )
	head:tag( 'th' ):attr( 'scope', 'col' ):wikitext( 'What it does' )
	local rows = 0
	local i = 1
	while args[ i ] do
		local key = mw.text.trim( args[ i ] )
		local action = mw.text.trim( args[ i + 1 ] or '' )
		if key ~= '' then
			local row = tbl:tag( 'tr' )
			row:tag( 'td' ):wikitext( choices( key ) )
			row:tag( 'td' ):wikitext( action )
			rows = rows + 1
		end
		i = i + 2
	end
	if rows == 0 then
		return '<strong class="error">Controls: give at least one key and what it does.</strong>'
	end
	return tostring( tbl )
end

return p