Module:Command: Difference between revisions

From Modded Wiki
Jump to navigation Jump to search
Smartly handle spaces and dataTags
Auto escape sub-commands
Line 10: Line 10:
if not fullCommand and v == '...' then
if not fullCommand and v == '...' then
fullCommand = true
fullCommand = true
elseif i > 1 or v ~= commandName then
elseif i > 1 or v:match( '^%s*/?(.+)' ):lower() ~= commandName then
-- Don't encode sub-commands
-- Don't encode if told not to or if there is a sub-command
if not v:find( ' ' ) then
if args.encode ~= '0' and not v:find( '<!%-%- Command %-%->' ) then
v = mw.text.encode( v )
v = v:gsub( '<', '&lt;' )
end
end
table.insert( command, mw.text.trim( v ) )
table.insert( command, mw.text.trim( v ) )
end
end
end
end
if #command == 1 and ( not args[2] or args[2] == '...' ) and command[1]:find( '%s' ) then
if #command == 1 and ( not args[2] or args[2] == '...' ) and command[1]:find( '%s' ) then
local startBrace, endBrace = command[1]:find( '{.+}' )
local startPos, endPos = command[1]:find( '{.+}' )
if startBrace then
if not startPos then
command[1] = command[1]:sub( 1, startBrace ) ..
startPos, endPos = command[1]:find( '<!%-%- Command %-%->.+<!%-%- /Command %-%->' )
command[1]:sub( startBrace + 1, endBrace - 1 ):gsub( '%s', '&#32;' ) ..
end
command[1]:sub( endBrace )
if startPos then
command[1] = command[1]:sub( 1, startPos ) ..
command[1]:sub( startPos + 1, endPos - 1 ):gsub( '%s', '&#32;' ) ..
command[1]:sub( endPos )
end
end
Line 88: Line 93:
end
end
if args.sub then
local attr
return slash .. table.concat( command, ' ' ):gsub( '%s', '&#32;' )
if args.long == '1' then
attr = 'style="display:block;padding:0.8em 1em;margin-bottom:0.4em;word-wrap:break-word"'
else
else
local attr
attr = 'class="nowrap"'
if args.long == '1' then
attr = 'style="display:block;padding:0.8em 1em;margin-bottom:0.4em;word-wrap:break-word"'
else
attr = 'class="nowrap"'
end
return '<code ' .. attr .. '>' .. slash .. table.concat( command, ' ' ):gsub( '&#32;', ' ' ) .. '</code>'
end
end
return '<!-- Command --><code ' .. attr .. '>' .. slash .. table.concat( command, ' ' ):gsub( '&#32;', ' ' ) .. '</code><!-- /Command -->'
end
end
return p
return p

Revision as of 10:38, 22 February 2014

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

local p = {}
function p.cmd( f )
	local args = f:getParent().args
	local syntax = mw.loadData( 'Module:Command/Syntax' )
	local fullCommand
	local commandName = args[1]:match( '^%s*/?([^%s]+)' ):lower()
	local command = {}
	
	for i, v in ipairs( args ) do
		if not fullCommand and v == '...' then
			fullCommand = true
		elseif i > 1 or v:match( '^%s*/?(.+)' ):lower() ~= commandName then
			-- Don't encode if told not to or if there is a sub-command
			if args.encode ~= '0' and not v:find( '<!%-%- Command %-%->' ) then
				v = v:gsub( '<', '&lt;' )
			end
			
			table.insert( command, mw.text.trim( v ) )
		end
	end
	if #command == 1 and ( not args[2] or args[2] == '...' ) and command[1]:find( '%s' ) then
		local startPos, endPos = command[1]:find( '{.+}' )
		if not startPos then
			startPos, endPos = command[1]:find( '<!%-%- Command %-%->.+<!%-%- /Command %-%->' )
		end
		
		if startPos then
			command[1] = command[1]:sub( 1, startPos ) ..
				command[1]:sub( startPos + 1, endPos - 1 ):gsub( '%s', '&#32;' ) ..
				command[1]:sub( endPos )
		end
		
		command = mw.text.split( command[1]:match( '^[^%s]+%s(.+)' ), '%s+' )
	end
	
	if ( fullCommand or command[1] ) and syntax[commandName] then
		local param = 0
		function parseParams( defaultParams, sub )
			local section = {}
			local hasValue
			for i, v in ipairs( defaultParams ) do
				if type( v ) == 'table' then
					local subSection, subHasValue = parseParams( v, true )
					if subHasValue then
						hasValue = true
					end
					table.insert( section, subSection )
				else
					param = param + 1
					if command[param] then
						hasValue = true
						if command[param] ~= '' and command[param] ~= '?' then
							table.insert( section, command[param] )
						end
					end
					if not section[i] then
						table.insert( section, v )
					end
				end
			end
			section = table.concat( section, ' ' )
			
			if sub then
				if not hasValue then
					if fullCommand then
						section = '(' .. section .. ')'
					else
						section = nil
					end
				end
				
				return section, hasValue
			else
				return section
			end
		end
		
		command = { parseParams( syntax[syntax[commandName]] or syntax[commandName] ) }
	end
	
	if args.link then
		if args.link:lower() ~= 'none' then
			commandName = '[[' .. args.link .. '|' .. commandName .. ']]'
		end
	else
		commandName = '[[Commands#' .. commandName .. '|' .. commandName .. ']]'
	end
	table.insert( command, 1, commandName )
	
	local slash = '/'
	if args['/'] == '0' or args.slash == '0' then
		slash = ''
	end
	
	local attr
	if args.long == '1' then
		attr = 'style="display:block;padding:0.8em 1em;margin-bottom:0.4em;word-wrap:break-word"'
	else
		attr = 'class="nowrap"'
	end
	
	return '<!-- Command --><code ' .. attr .. '>' .. slash .. table.concat( command, ' ' ):gsub( '&#32;', ' ' ) .. '</code><!-- /Command -->'
end
return p