Module:Sandbox/MarioGom/Multiple issues

Revision as of 23:39, 26 July 2019 by imported>MarioGom (Created page with 'local p = {} local state_initial = 1 local state_first_open_brace = 2 local state_first_closing_brace = 3 local c_open_brace = string.byte("{") local c_close_b...')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Sandbox/MarioGom/Multiple issues/doc

local p = {}

local state_initial = 1
local state_first_open_brace = 2
local state_first_closing_brace = 3

local c_open_brace = string.byte("{")
local c_close_brace = string.byte("}")

function p.number_of_templates(content)
	if content == nil then
		mw.log("content is nil")
		return 0
	end
    local state = state_initial
    local count = 0
    local level = 0
    for i = 1, #content do
        local c = content:byte(i)
        if state == state_initial then
            if c == c_open_brace then
                state = state_first_open_brace
            elseif c == c_close_brace then
                state = state_first_closing_brace
            end
        elseif state == state_first_open_brace then
            if c == c_open_brace then
                state = state_initial
                level = level + 1
            else
                state = state_initial
            end
        elseif state == state_first_closing_brace then
            if c == c_close_brace then
                level = level - 1
                if level == 0 then
                    count = count + 1
                end
            end
            state = state_initial
        end
    end
    return count
end

function p.multiple_issues(frame)
	local frame = mw.getCurrentFrame()
	local first = frame.args[1]
	local second = frame.args[2]
	if p.number_of_templates(first) > 1 then
		return second
	end
	return first
end

return p