Module:ScriptItem2: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 407: Line 407:
else
else
for i,arg in ipairs(entries) do
for i,arg in ipairs(entries) do
local tokens = p.split(arg, ' ')
                table.insert(argsText, p.getArgSyntaxText(p.getArgParts(arg)))
local isOptional = tokens[1]:sub(1, 1) == '['
if isOptional then
tokens[1] = tokens[1]:sub(2, tokens[1]:len())
tokens[4] = tokens[4]:sub(1, tokens[4]:len() - 1)
end
local defaultValue = isOptional and tokens[4] or "n/a"
if isOptional then
table.insert(argsText, '[ '..tokens[1]..' '..tokens[2]..' = '..defaultValue..' ]')
else
table.insert(argsText, tokens[1]..' '..tokens[2])
end
end
end
end
end
Line 441: Line 428:
})
})
end
end
end
function p.getArgParts(text)
--[[
        INPUT EXAMPLES (str)
        Vec3 pos The 3D position
        [Vec3 pos] The 3D position.
        [ Vec3 pos ] The 3D position
        [ Vec3 pos = new Vec3(0.0, 0.0, 0.0) ] The 3D position.
        OUTPUT (table)
        bool parts.optional
        bool parts.defaultValueIsSpecified
str parts.type
str parts.name
str parts.defaultValue
str parts.description
]]
local parts = {}
text = p.trim(text)
    parts.optional = false
local isProbablyOptional = text:sub(1, 1) == '['
if isProbablyOptional then
local optionalEndIndex = p.rfind(text, ']')
if optionalEndIndex then
            parts.optional = true
            local syntaxPart = p.trim(text:sub(2, optionalEndIndex - 1))
            parts.description = p.trim(text:sub(optionalEndIndex + 1, #text))
            local tokens = p.split(syntaxPart, ' ')
            parts.type = tokens[1]
            parts.name = tokens[2]
            parts.defaultValueIsSpecified = tokens[3] == '=' and #tokens >= 4
            parts.defaultValue = table.concat(tokens, ' ', 4, #tokens)
            return parts
        end
    end
    local tokens = p.split(text, ' ')
    parts.type = tokens[1]
    parts.name = tokens[2]
    parts.description = p.trim(table.concat(tokens, ' ', 3, #tokens))
    return parts
end
function p.getArgSyntaxText(parts)
    if parts.optional then
        if parts.defaultValueIsSpecified then
            return '[ '..parts.type..' '..parts.name..' = '..parts.defaultValue..' ]'
        else
            return '[ '..parts.type..' '..parts.name..' ]'
        end
    else
        return parts.type..' '..parts.name
    end
end
end


Line 447: Line 491:
local entries = p.getIndexedArgs(frame, 'desc')
local entries = p.getIndexedArgs(frame, 'desc')
local startTextFirstLine = "The <span style=\"font-family: 'Source Code Pro', monospace;\">"..p.getDisplayedNameColoured(frame).."</span> [[HowTo/Functions|"..args.type.."]] is "..(args.type == "event" and "invoked when" or "used to").." "
local startTextFirstLine = "The <span style=\"font-family: 'Source Code Pro', monospace;\">"..p.getDisplayedNameColoured(frame).."</span> "..args.type:lower().." is "..(args.type == "event" and "invoked when" or "used to").." "
for i,entry in ipairs(entries) do
for i,entry in ipairs(entries) do
Line 473: Line 517:
else
else
for i,arg in ipairs(entries) do
for i,arg in ipairs(entries) do
local tokens = p.split(arg, ' ')
local parts = p.getArgParts(arg)
 
local isOptional = tokens[1]:sub(1, 1) == '['
if isOptional then
tokens[1] = tokens[1]:sub(2, tokens[1]:len())
tokens[4] = tokens[4]:sub(1, tokens[4]:len() - 1)
end
local defaultValue = isOptional and "<span style='font-family: \"Source Code Pro\", monospace;'>"..tokens[4].."</span>" or "n/a"
local description = p.formatDescription(table.concat(tokens, ' ', isOptional and 5 or 3))
if isEvent then
if isEvent then
rows[i] = { (i + 1)..") ", p.formatType(tokens[1], frame), p.formatName(tokens[2], frame), description }
rows[i] = { (i + 1)..") ", p.formatType(tokens[1], frame), p.formatName(tokens[2], frame), p.formatDescription(parts.description) }
else
else
rows[i] = { i..") ", p.formatType(tokens[1], frame), p.formatName(tokens[2], frame), (isOptional and ("Optional, defaults to "..defaultValue..". ") or "")..description }
if parts.optional then
if parts.defaultValueIsSpecified then
rows[i] = { i..") ", p.formatType(parts.type, frame), p.formatName(parts.name, frame), "Optional, defaults to "..p.formatDefaultValue(parts.defaultValue)..". "..p.formatDescription(parts.description) }
else
rows[i] = { i..") ", p.formatType(parts.type, frame), p.formatName(parts.name, frame), "Optional, the default value has not been documented here yet. "..p.formatDescription(parts.description) }
end
else
rows[i] = { i..") ", p.formatType(parts.type, frame), p.formatName(parts.name, frame), p.formatDescription(parts.description) }
end
end
end
end
end
Line 1,107: Line 1,151:
function p.formatName(name, frame)
function p.formatName(name, frame)
return "<span style='font-family: \"Source Code Pro\", monospace; color: "..p.getNameRgb()..";\"'>"..name.."</span>"
return "<span style='font-family: \"Source Code Pro\", monospace; color: "..p.getNameRgb()..";\"'>"..name.."</span>"
end
function p.formatDefaultValue(value, frame)
return "<span style='font-family: \"Source Code Pro\", monospace; color: "..p.getNameRgb()..";\"'>"..value.."</span>"
end
end


Line 1,124: Line 1,172:


function p.standardizeNullType(type)
function p.standardizeNullType(type)
type = type:lower()
local type2 = type:lower()
if type == 'void' or type == 'null' or type == 'undefined' or type == 'n/a' then
if type2 == 'void' or type2 == 'null' or type2 == 'undefined' or type2 == 'n/a' then
return 'void'
return 'void'
else
else
Line 1,174: Line 1,222:


   return r, n
   return r, n
end
function p.rfind(s, find)
local index = s:reverse():find(find)
if not index then return end
return #s - index + 1
end
function p.trim(s)
return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end
end


6,833

edits