Module:Command: Difference between revisions

From Modded Wiki
Jump to navigation Jump to search
m cleanup the html\css output
mNo edit summary
 
(20 intermediate revisions by 7 users not shown)
Line 5: Line 5:
args = f:getParent().args
args = f:getParent().args
end
end
local syntax = mw.loadData( 'Module:Command/Syntax' )
--if args.be then
--local syntax = mw.loadData( 'Module:Command/SyntaxBE' )
--else
local syntax = mw.loadData( 'Module:Command/Syntax')
--end
local fullCommand
local fullCommand
local commandName = args[1]:match( '^%s*/?([^%s]+)' ):lower()
local commandName = args[1]:match( '^%s*/?([^%s]+)' ):lower()
Line 11: Line 15:
local command = {}
local command = {}
--getparams
for i, v in ipairs( args ) do
for i, v in ipairs( args ) do
if not fullCommand and v == '...' then
if not args[i+1] and v == '...' then
fullCommand = true
fullCommand = true
elseif i > 1 or v:match( '^%s*/?(.+)' ):lower() ~= commandName then
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( params, mw.text.trim( v ) )
table.insert( params, mw.text.trim( v ) )
end
end
end
end
local needFormat = false
if ( fullCommand or args[2] or args[1]:match('?') ) and syntax['command'..commandName] then
needFormat = true
end
--escape space and split args
if #params == 1 and ( not args[2] or args[2] == '...' ) and params[1]:find( '%s' ) then
if #params == 1 and ( not args[2] or args[2] == '...' ) and params[1]:find( '%s' ) then
local startPos, endPos = params[1]:find( '{.+}' )
params[1] = params[1]:match( '^[^%s]+%s(.+)' )
if not startPos then
startPos, endPos = params[1]:find( '<!%-%- Command %-%->.+<!%-%- /Command %-%->' )
if needFormat then
end
params[1] = params[1]:gsub( '\\\\', '&bsol;&bsol;' ):gsub( '\\"', '&bsol;&quot;' ):gsub( "\\'", '&bsol;&apos;' ):gsub( '(@[aesprcv])%s+(%[)', '%1%2' )
while(true) do
local startPos, endPos = params[1]:find( "'[^']-'" )
local startPosn, endPosn = params[1]:find('"[^"]-"')
if startPosn and startPos and startPosn < startPos then
startPos, endPos = startPosn, endPosn
end
if not startPos then
startPos, endPos = params[1]:find( '"[^"]+"' )
if not startPos then
startPos, endPos = params[1]:find( '%b[]' )
if not startPos then
startPos, endPos = params[1]:find( '%b{}' )
if not startPos then
startPos, endPos = params[1]:find( '&lt;!%-%- Command %-%->.+&lt;!%-%- /Command %-%->' )
end
end
end
end
if startPos then
params[1] = params[1]:sub( 1, startPos - 1 ) ..
params[1]:sub( startPos, endPos ):gsub('%[','〈lsqb〉'):gsub('%]','〈rsqb〉'):gsub('{','&#123;'):gsub('}','&#125;'):gsub("'",'〈apos〉'):gsub('"','〈quot〉'):gsub( '%s', '〈space〉' ) ..
params[1]:sub( endPos + 1 )
else
break
end
end
if startPos then
params = mw.text.split( params[1], '%s+' )
params[1] = params[1]:sub( 1, startPos ) ..
params[1]:sub( startPos + 1, endPos - 1 ):gsub( '%s', '&#32;' ) ..
params[1]:sub( endPos )
end
end
params = mw.text.split( params[1]:match( '^[^%s]+%s(.+)' ), '%s+' )
end
end
if ( fullCommand or params[1] ) and syntax[commandName] then
if needFormat then
local param = 0
local param = 0
function parseParams( defaultParams, sub )
function parseParams( defaultParams )
local section = {}
local section = {}
local hasValue
local hasValue
for i, v in ipairs( defaultParams ) do
local prefix
for i, v in pairs( defaultParams or {} ) do
if type( v ) == 'table' then
if type( v ) == 'table' then
local subSection, subHasValue = parseParams( v, true )
param = param + 1
if subHasValue then
for k,j in pairs(v) do
hasValue = true
v = j
prefix = k
break
end
if params[param] and params[param] ~= '' and params[param]~='?' then
hasValue = false
for k,j in pairs(v) do
if j == params[param] or j:match('<.+>') then
table.insert(section, params[param])
hasValue=true
if k=='redirect' then
param = param - 1
table.insert(section, parseParams(syntax[j:match('<(.+)>')]))
else
table.insert(section, parseParams(syntax[prefix..j]))
end
break
end
end
if not hasValue then
params = {}
break
end
else
params = {}
local option = {}
local swap
for _,j in pairs(v) do
if fullCommand then
swap = {j,parseParams(syntax[prefix..j])}
table.insert(option, table.concat(swap,' '))
else
table.insert(option, j)
end
end
table.insert(section, '('..table.concat(option, '|')..')')
end
elseif i == 'redirect' then
if params[param+1] then
table.insert(section, parseParams(syntax[v]))
elseif v:match("^command$") then
table.insert(section, "-> ''"..v.."''")
else
table.insert(section, "-> ''"..v:match('command(.+)').."''")
end
end
table.insert( section, subSection )
else
else
param = param + 1
param = param + 1
if params[param] then
if params[param] and params[param] ~= '' and params[param] ~= '?' then
hasValue = true
table.insert(section,params[param])
if params[param] ~= '' and params[param] ~= '?' then
else
table.insert( section, params[param] )
table.insert(section, v)
end
end
if not section[i] and ( fullCommand or params[param] == '?' ) then
table.insert( section, v )
end
end
end
if not fullCommand and not params[param+1] then
break
end
end
end
end
section = table.concat( section, ' ' )
section = table.concat( section, ' ' )
if section == '' then
section = nil
end
if sub then
return section
if not hasValue then
if fullCommand then
section = '(' .. section .. ')'
else
section = nil
end
end
return section, hasValue
else
return section
end
end
end
command = { parseParams( syntax[syntax[commandName]] or syntax[commandName] ) }
command = { parseParams( syntax['command'..commandName] ) }
-- Add any extra parameters not defined in the syntax
-- Add any extra parameters not defined in the syntax
Line 108: Line 173:
end
end
local attr
local attr = ''
if args.long then
if args.long then
attr = 'style="display: block; padding: 0.8em 1em; margin-bottom: 0.4em; word-wrap: break-word;"'
attr = 'style="display: flex; padding: 0.8em 1em; margin-bottom: 0.4em; word-warp: break-word;"'
else
attr = 'style="word-break: break-all;"'
end
end
return '<!-- Command start --><code ' .. attr .. '>' .. slash .. table.concat( command, ' ' ):gsub( '&#32;', ' ' ) .. '</code><!-- Command end -->'
local result = table.concat( command, ' ' ):gsub( '〈space〉', ' ' ):gsub('〈lsqb〉', '%['):gsub('〈rsqb〉', '%]'):gsub('〈apos〉',"'"):gsub('〈quot〉','"')
-- Don't encode if told not to or if there is a sub-command
if args.escape ~= '0' then
result = result:gsub( '<', '&lt;' ):gsub( '&lt;(!%-%- Command %-%->)&lt;','<%1<'):gsub( '&lt;/code>&lt;(!%-%- /Command %-%->)','</code><%1')
end
result = '<!-- Command --><code ' .. attr .. '><span>' .. slash .. result .. '</span></code><!-- /Command -->'
    if needFormat then
        result = result .. '[[Category:Pages using deprecated command module]]'
    end
    return result
end
end
return p
return p

Latest revision as of 14:34, 1 May 2024

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

local p = {}
function p.cmd( f )
	local args = f
	if f == mw.getCurrentFrame() then
		args = f:getParent().args
	end
	--if args.be then
		--local syntax = mw.loadData( 'Module:Command/SyntaxBE' )
	--else
		local syntax = mw.loadData( 'Module:Command/Syntax')
	--end
	local fullCommand
	local commandName = args[1]:match( '^%s*/?([^%s]+)' ):lower()
	local params = {}
	local command = {}
	
	--getparams
	for i, v in ipairs( args ) do
		if not args[i+1] and v == '...' then
			fullCommand = true
		elseif i > 1 or v:match( '^%s*/?(.+)' ):lower() ~= commandName then
			table.insert( params, mw.text.trim( v ) )
		end
	end

	local needFormat = false
	if ( fullCommand or args[2] or args[1]:match('?') ) and syntax['command'..commandName] then
		needFormat = true
	end

	--escape space and split args
	if #params == 1 and ( not args[2] or args[2] == '...' ) and params[1]:find( '%s' ) then
		params[1] = params[1]:match( '^[^%s]+%s(.+)' )
		
		if needFormat then
		
			params[1] = params[1]:gsub( '\\\\', '&bsol;&bsol;' ):gsub( '\\"', '&bsol;&quot;' ):gsub( "\\'", '&bsol;&apos;' ):gsub( '(@[aesprcv])%s+(%[)', '%1%2' )
		
			while(true) do
				local startPos, endPos = params[1]:find( "'[^']-'" )
				local startPosn, endPosn = params[1]:find('"[^"]-"')
				if startPosn and startPos and startPosn < startPos then
					startPos, endPos = startPosn, endPosn
				end
				if not startPos then
					startPos, endPos = params[1]:find( '"[^"]+"' )
					if not startPos then
						startPos, endPos = params[1]:find( '%b[]' )
						if not startPos then
							startPos, endPos = params[1]:find( '%b{}' )
							if not startPos then
								startPos, endPos = params[1]:find( '&lt;!%-%- Command %-%->.+&lt;!%-%- /Command %-%->' )
							end
						end
					end
				end
				
				if startPos then
					params[1] = params[1]:sub( 1, startPos - 1 ) ..
						params[1]:sub( startPos, endPos ):gsub('%[','〈lsqb〉'):gsub('%]','〈rsqb〉'):gsub('{','&#123;'):gsub('}','&#125;'):gsub("'",'〈apos〉'):gsub('"','〈quot〉'):gsub( '%s', '〈space〉' ) ..
						params[1]:sub( endPos + 1 )
				else
					break
				end
			end
		
			params = mw.text.split( params[1], '%s+' )
		end
	end
	
	if needFormat then
		local param = 0
		function parseParams( defaultParams )
			local section = {}
			local hasValue
			local prefix
			for i, v in pairs( defaultParams or {} ) do
				if type( v ) == 'table' then
					param = param + 1
					for k,j in pairs(v) do
						v = j
						prefix = k
						break
					end
					if params[param] and params[param] ~= '' and params[param]~='?' then
						hasValue = false
						for k,j in pairs(v) do
							if j == params[param] or j:match('<.+>') then
								table.insert(section, params[param])
								hasValue=true
								if k=='redirect' then
									param = param - 1
									table.insert(section, parseParams(syntax[j:match('<(.+)>')]))
								else
									table.insert(section, parseParams(syntax[prefix..j]))
								end
								break
							end
						end
						if not hasValue then
							params = {}
							break
						end
					else
						params = {}
						local option = {}
						local swap
						for _,j in pairs(v) do
							if fullCommand then
								swap = {j,parseParams(syntax[prefix..j])}
								table.insert(option, table.concat(swap,' '))
							else
								table.insert(option, j)
							end
						end
						table.insert(section, '('..table.concat(option, '|')..')')
					end
				elseif i == 'redirect' then
					if params[param+1] then
						table.insert(section, parseParams(syntax[v]))
					elseif v:match("^command$") then
						table.insert(section, "-> ''"..v.."''")
					else
						table.insert(section, "-> ''"..v:match('command(.+)').."''")
					end
				else
					param = param + 1
					if params[param] and params[param] ~= '' and params[param] ~= '?' then
						table.insert(section,params[param])
					else
						table.insert(section, v)
					end
				end
				if not fullCommand and not params[param+1] then
					break
				end
			end
			
			section = table.concat( section, ' ' )
			if section == '' then
				section = nil
			end
			
			return section
		end
		
		command = { parseParams( syntax['command'..commandName] ) }
		
		-- Add any extra parameters not defined in the syntax
		if #params > param then
			for i, v in ipairs( params ) do
				if i > param then
					table.insert( command, v )
				end
			end
		end
	else
		command = params
	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 then
		attr = 'style="display: flex; padding: 0.8em 1em; margin-bottom: 0.4em; word-warp: break-word;"'
	end
	
	local result = table.concat( command, ' ' ):gsub( '〈space〉', ' ' ):gsub('〈lsqb〉', '%['):gsub('〈rsqb〉', '%]'):gsub('〈apos〉',"'"):gsub('〈quot〉','"')
	-- Don't encode if told not to or if there is a sub-command
	if args.escape ~= '0' then
		result = result:gsub( '<', '&lt;' ):gsub( '&lt;(!%-%- Command %-%->)&lt;','<%1<'):gsub( '&lt;/code>&lt;(!%-%- /Command %-%->)','</code><%1')
	end
	result = '<!-- Command --><code ' .. attr .. '><span>' .. slash .. result .. '</span></code><!-- /Command -->'
    if needFormat then
        result = result .. '[[Category:Pages using deprecated command module]]'
    end
    return result
end
return p