Módulo:StringTables

De RuneScape Wiki
Ir para: navegação, pesquisa

A documentação para este módulo pode ser criada em Módulo:StringTables/doc

-- <pre>
local p = {}

function p._stringTable(inArr)
    local resArr = {}
    local index = 0
    local row = ""
    local cell = ""
    
    -- Create the table tag with its attributes
    resArr[index + 1] = "<table class='"
    resArr[index + 2] = inArr.addClass or ""
    resArr[index + 3] = "' style='"
    index = index + 3
    
    for k, v in pairs(inArr.css or {}) do
        resArr[index + 1] = k or ""
        resArr[index + 2] = ":"
        resArr[index + 3] = v or ""
        resArr[index + 4] = "; "
        index = index + 4
    end
    
    index = index + 1
    resArr[index] = "' "
    
    for k, v in pairs(inArr.attr or {}) do
        resArr[index + 1] = k or ""
        resArr[index + 2] = "='"
        resArr[index + 3] = v or ""
        resArr[index + 4] = "' "
        index = index + 4
    end
    
    index = index + 1
    resArr[index] = ">"
    
    -- Create the rows of the table
    for row = 1, #inArr do
        row = inArr[row]

        resArr[index + 1] = "<tr class='"
        resArr[index + 2] = row.addClass or ""
        resArr[index + 3] = "' style='"
        index = index + 3
        
        for k, v in pairs(row.css or {}) do
            resArr[index + 1] = k or ""
            resArr[index + 2] = ":"
            resArr[index + 3] = v or ""
            resArr[index + 4] = "; "
            index = index + 4
        end
        
        index = index + 1
        resArr[index] = "' "
    
        for k, v in pairs(row.attr or {}) do
            resArr[index + 1] = k or ""
            resArr[index + 2] = "='"
            resArr[index + 3] = v or ""
            resArr[index + 4] = "' "
            index = index + 4
        end
        
        index = index + 1
        resArr[index] = ">"
        
        -- Create the cells of the row
        for cell = 1, #row do
            cell = row[cell]

            resArr[index + 1] = "<"
            resArr[index + 2] = cell.tag or "td"
            resArr[index + 3] = " class='"
            resArr[index + 4] = cell.addClass or ""
            resArr[index + 5] = "' style='"
            index = index + 5
            
            for k, v in pairs(cell.css or {}) do
                resArr[index + 1] = k or ""
                resArr[index + 2] = ":"
                resArr[index + 3] = v or ""
                resArr[index + 4] = "; "
                index = index + 4
            end
            
            index = index + 1
            resArr[index] = "' "
        
            for k, v in pairs(cell.attr or {}) do
                resArr[index + 1] = k or ""
                resArr[index + 2] = "='"
                resArr[index + 3] = v or ""
                resArr[index + 4] = "' "
                index = index + 4
            end
            
            resArr[index + 1] = ">"
            
            resArr[index + 2] = cell.text or ""
            
            resArr[index + 3] = "</"
            resArr[index + 4] = cell.tag or "td"
            resArr[index + 5] = ">"
            index = index + 5
        end
        
        index = index + 1
        resArr[index] = "</tr>"
    end
    
    -- Close the table tag
    index = index + 1
    resArr[index] = "</table>"
    
    return table.concat(resArr)
end

function p._tableToString(tbl)
    local resArr = {}
    local index = 0
    
    if tbl.tagName then
        resArr[index + 1] = "<"
        resArr[index + 2] = tbl.tagName
        resArr[index + 3] = " "
        index = index + 3
    end
    
    if tbl.attributes then
        for k, v in pairs(tbl.attributes or {}) do
            resArr[index + 1] = v.name
            resArr[index + 2] = "='"
            resArr[index + 3] = v.val
            resArr[index + 4] = "' "
            index = index + 4
        end
    end
    
    if #tbl.styles > 0 then
        index = index + 1
        resArr[index] = "style='"
        
        for k, v in pairs(tbl.styles or {}) do
            if type(v) == "table" then 
                resArr[index + 1] = v.name or ""
                resArr[index + 2] = ":"
                resArr[index + 3] = v.val or ""
                resArr[index + 4] = "; "
                index = index + 4
            else 
                resArr[index + 1] = v or ""
                resArr[index + 2] = "; "
                index = index + 2
            end
        end

        index = index + 1
        resArr[index] = "'"
    end
    
    if tbl.tagName then
        index = index + 1
        resArr[index] = ">"
    end
    
    if tbl.nodes then
        for k, v in pairs(tbl.nodes or {}) do
            index = index + 1
            if type(v) == "table" then
                resArr[index] = p._tableToString(v)
            else
                resArr[index] = v
            end
        end
    end
    
    if tbl.tagName then
        resArr[index + 1] = "</"
        resArr[index + 2] = tbl.tagName
        resArr[index + 3] = ">"
    end
    
    return table.concat(resArr)
end

return p
-- </pre>