Jump to content

Module:SimpleDebug: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m Fix counter bug
m Added "local" to local functions
Line 19: Line 19:
local LinCount = 0
local LinCount = 0
local vep = '  •  '
local vep = '  •  '
function MessRaised (n)
local function MessRaised (n)
return '\n\nIt has been reached to '..n..', you can change this limit with "maxlines.num".'
return '\n\nIt has been reached to '..n..', you can change this limit with "maxlines.num".'
end
end
Line 38: Line 38:
end --breakline
end --breakline


function CheckWhereName (wn, what)
local function CheckWhereName (wn, what)
if wn == nil then
if wn == nil then
return '"'..what..'" == nil'
return '"'..what..'" == nil'
Line 48: Line 48:
end --CheckWhereName
end --CheckWhereName


function plain (text) --Modified from "Module:Plain text"
local function plain (text) --Modified from "Module:Plain text"
if not text then return end
if not text then return end
text = mw.text.killMarkers(text)
text = mw.text.killMarkers(text)
Line 74: Line 74:
end --plain
end --plain


function totext (text)
local function totext (text)
if p.plaintext then
if p.plaintext then
return plain (text)
return plain (text)
Line 82: Line 82:
end --totext
end --totext


function NumToStr (N)
local function NumToStr (N)
if (p.dec == -1) or (N == math.floor(N)) then
if (p.dec == -1) or (N == math.floor(N)) then
return tostring(N)
return tostring(N)
Line 102: Line 102:
end --containsTab
end --containsTab


function DumTab (tbl, indent)
local function DumTab (tbl, indent)
if not indent then indent = 1 end
if not indent then indent = 1 end
local toprint = " {\r\n"
local toprint = " {\r\n"
Line 139: Line 139:
end --DumTab
end --DumTab


function var (avar)
local function var (avar)
local EndStr = ''
local EndStr = ''
if type(avar) == 'table' then
if type(avar) == 'table' then
Line 185: Line 185:
end --w
end --w


function varx (avar)
local function varx (avar)
iniTab1Line = p.tab.oneline
iniTab1Line = p.tab.oneline
if p.tab.oneline and (type(avar) == 'table') then
if p.tab.oneline and (type(avar) == 'table') then

Revision as of 08:00, 7 May 2020

local p = {}

p.s = ''
p.tab = {
    oneline = true,
    allidx = false,
    }
p.dec = -1
p.maxlines = {
    num = 100,
    doerror = true,
    }
p.enabled = true
p.nowiki = false
p.nohtml = false
p.plaintext = false
p.counter = false

local LinCount = 0
local vep = '  •  '
local function MessRaised (n)
    return '\n\nIt has been reached to '..n..', you can change this limit with "maxlines.num".'
end    
local function arrow()
    return ' => '
end

function p.breakline ()
    LinCount = LinCount + 1
    p.s = p.s..'\n\n'
    if p.counter then
        p.s = p.s..LinCount..vep
    end    
    if (LinCount > p.maxlines.num) and p.maxlines.doerror then
        p.pa = p.s..MessRaised(p.maxlines.num)
        error (p.s,0)
    end    
end    --breakline

local function CheckWhereName (wn, what)
    if wn == nil then
        return '"'..what..'" == nil'
    elseif (type(wn) == "table") then
        return 'Table as "'..what..'"!'
    else
        return wn    
    end    
end --CheckWhereName

local function plain (text) --Modified from "Module:Plain text"
    if not text then return end
    text = mw.text.killMarkers(text)
        :gsub(' ', ' ') --replace nbsp spaces with regular spaces
        :gsub('<br ?/?>', ', ') --replace br with commas
        :gsub('<span.->(.-)</span>', '%1') --remove spans while keeping text inside
        :gsub('<b>(.-)</b>', '%1') --remove bold while keeping text inside
        :gsub('<i>(.-)</i>', '%1') --remove italic while keeping text inside
        :gsub('<sub>(.-)</sub>', '%1') --remove bold while keeping text inside
        :gsub('<sup>(.-)</sup>', '%1') --remove bold while keeping text inside
        :gsub('<.->.-<.->', '') --strip out remaining tags and the text inside
        :gsub('<.->', '') --remove any other tag markup
        :gsub('%[%[%s*[Ff]ile%s*:.-%]%]', '') --strip out files
        :gsub('%[%[%s*[Ii]mage%s*:.-%]%]', '') --strip out use of image:
        :gsub('%[%[%s*[Cc]ategory%s*:.-%]%]', '') --strip out categories
        :gsub('%[%[[^%]]-|', '') --strip out piped link text
        :gsub('[%[%]]', '') --then strip out remaining [ and ]
        :gsub("'''''", "") --strip out bold italic markup
        :gsub("'''?", "") --not stripping out '''' gives correct output for bolded text in quotes
        :gsub('----', '') --remove ---- lines
        :gsub("^%s+", "") --strip leading
        :gsub("%s+$", "") --and trailing spaces
        :gsub("%s+", " ") --strip redundant spaces
    return text
end --plain

local function totext (text)
    if p.plaintext then
        return plain (text)
    else
        return text
    end    
end --totext

local function NumToStr (N)        
    if (p.dec == -1) or (N == math.floor(N)) then
        return tostring(N)
    else
        return tostring (math.floor ((N*10^p.dec)+0.5) / (10^p.dec))
    end    
end    --NumToStr

local iniTab1Line = true
function p.containsTab (avar)
    result = false
    for k,v in pairs(avar) do
        if type(v) == 'table' then
            result = true
            break
        end    
    end
    return result
end --containsTab

local function DumTab (tbl, indent)
    if not indent then indent = 1 end
    local toprint = " {\r\n"
    indent = indent + 2
    for k, v in pairs(tbl) do
        toprint = toprint..string.rep(" ", indent)
        local id = k
        if (type(k) == "string") then
            k = '"'..k..'"'
        end
        toprint = toprint.."["..k.."] = "
        if (type(v) == "number") then
            toprint = toprint..NumToStr(v)..",\r\n"
        elseif (type(v) == "string") then
            toprint = toprint.."\""..totext(v).."\",\r\n"
        elseif (type(v) == "table") then
            if iniTab1Line and (not p.containsTab (v)) then
                local wds = '{'
                for kk,vv in pairs(v) do
                    if (p.tab.allidx == true) or (type(kk) ~= 'number')  then
                        wds = wds..'['..kk..']='..var(vv)..', '
                    else    
                        wds = wds..var(vv)..', '
                    end
                end
                toprint = toprint..wds.."},\r\n"
            else    
                toprint = toprint..DumTab(v, indent + 2)..",\r\n"
            end
        else
            toprint = toprint.."\""..tostring(v).."\",\r\n"
        end
    end
    toprint = toprint..string.rep(" ", indent-2).."}"
    return toprint
end --DumTab

local function var (avar)
    local EndStr = ''
    if type(avar) == 'table' then
        if #avar > 0 then
            p.s = p.s..'\r\n'
        end    
        if p.tab.oneline then
            local wds = '{ '
            for k,v in pairs(avar) do
                if (p.tab.allidx == true) or (type(k) ~= 'number')  then
                    wds = wds..'['..k..']='..var(v)..', '
                else    
                    wds = wds..var(v)..', '
                end
            end
            EndStr = wds .. '} '
        else
            EndStr = DumTab (avar)
        end    
    elseif type(avar) == 'number' then
        EndStr = NumToStr (avar)
    elseif type(avar) == 'boolean' then
        if avar == true then
            EndStr = 'true'
        else
            EndStr = 'false'
        end    
    elseif avar == nil then    
        EndStr = 'nil'
    else
        avar = totext (tostring(avar))
        if p.nohtml then
            avar = string.gsub (avar, "<", "⪡")
            avar = string.gsub (avar, ">", "⪢")
        end    
        EndStr = '"'..avar..'"'
    end
    return EndStr
end --var

function p.w (where)
    if p.enabled then
        return CheckWhereName (where, 'w')
    end    
end --w

local function varx (avar)
    iniTab1Line = p.tab.oneline
    if p.tab.oneline and (type(avar) == 'table') then
        p.tab.oneline = not p.containsTab(avar)
    end
    local ss = var(avar)
    p.tab.oneline = iniTab1Line
    return ss
end --varx

function p.v (...)
    if p.enabled then
        local str = ''
        for i = 1, #arg do
            if str ~= '' then
                str = str..vep
            end    
            str = str..varx(arg[i])
        end    
        return str
    end    
end    --v

function p.wv (where, ...)
    if p.enabled then
        return CheckWhereName(where,'w')..arrow()..p.v(unpack(arg))
    end    
end    --wv

function p.nv (...)
    if p.enabled then
        if math.mod(#arg,2) ~= 0 then    
            EndStr = 'Any parameter has not a name or variable'
        else
            local s = ''
            local IsName = true
            function Concat(wds)
                if s ~= '' then
                    if IsName then
                        s = s..vep
                    else    
                        s = s..': '
                    end    
                end
                s = s..wds
            end
            for i = 1, #arg do
                if IsName then
                    Concat (CheckWhereName(arg[i],'n'))
                    IsName = false
                else    
                    Concat (varx(arg[i]))
                    IsName = true
                end    
            end
            EndStr = s
        end
        return EndStr
    end    
end --nv

function p.wnv (where, ...)
    if p.enabled then
        return CheckWhereName(where,'w')..arrow()..p.nv (unpack(arg))
    end    
end

----------

local function EnabAndBl ()
    if p.enabled then
        if LinCount < p.maxlines.num then
            p.breakline ()
            return true
        else
            p.s = p.s..MessRaised(p.maxlines.num)
            error (p.s)
            return false
        end    
    else
        return false
    end
end --EnabAndBl

function p.wtos (where)
    if EnabAndBl () then
        p.s = p.s..p.w (where)
    end    
end --wtos

function p.vtos (...)
    if EnabAndBl () then
        p.s = p.s..p.v (unpack(arg))
    end    
end --vtos

function p.wvtos (where, ...)
    if EnabAndBl () then
        p.s = p.s..p.wv (where,unpack(arg))
    end    
end --wvtos

function p.nvtos (...)
    if EnabAndBl () then
        p.s = p.s..p.nv (unpack(arg))
    end    
end --nvtos

function p.wnvtos (where, ...)
    if EnabAndBl () then
        p.s = p.s..p.wnv (where, unpack(arg))
    end    
end --wnvtos

return p