Module:Succession box

From Wikipedia

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

function findminor(t, pagename)
    for major,row in ipairs(t) do
        for minor,name in ipairs(row) do
            if ( pagename == name ) then
                return { major=major, minor=minor, row=row }
            end
        end
    end
    return nil
end

function makestyle(styles)
    local text = {}
    for n,v in pairs(styles) do
        table.insert(text, n .. ":" .. v .. ";")
    end
    local s = table.concat(text, " ")
    if ( s ~= "" and s ~= nil ) then return "style = \"" .. s .. "\"" else return "" end
end

function linkedtitle(title, link)    
    if ( link ~= nil ) then
        if ( title ~= nil ) then title = "|" .. title else title = "" end
        title = "[[" .. link .. title .. "]]"
    else
        title = title or ""
    end
    return title
end

function displayrow(names)
    local links = {}
    for i,name in ipairs(names) do
        table.insert(links, linkedtitle(nil, name))
    end
    return "*" .. table.concat(links, "\n*") .. "\n"
end

local z = {
    wikitext = require "Module:Wikitext"
}

function z.autosuccessionbox (data, config, styles)
    local otherlinks = {}
    local boxes = {}
    local pagename = config.pagename
    for n,box in ipairs(data) do
        local position = findminor(box, pagename)
        if ( nil ~= position ) then
            table.insert(boxes, {box=box, major=position.major, row=position.row, minor=position.minor})
        else
            local link = linkedtitle(box.shorttitle or box.title, box.link)
            table.insert(otherlinks, link)
        end
    end
    local text = {}
    for i,pos in ipairs(boxes) do
        local box, major, row, minor = pos.box, pos.major, pos.row, pos.minor
        local n, p
        if (pos.major > 1) then
            p = "\n" .. displayrow(box[major - 1])
        else
            p = ""
        end
        if (pos.major < #pos.box) then
            n = "\n" .. displayrow(box[major + 1])
        else
            n = ""
        end
        local t = linkedtitle(box.longtitle or box.title, box.link)
        local c = "\n" .. displayrow(row)
        local row = "|-\n! colspan=\"3\"|" .. t .. "\n|-\n!predecessor\n| rowspan=\"2\" |" .. c .. "\n!successor\n|-\n|" .. p .. "\n|" .. n .. "\n"
        table.insert(text, row)
    end
    local editurl = config.editurl
    local edit
    if ( nil ~= editurl ) then
        local editlink = "([" .. editurl .. " " .. "edit data" .. "])"
        local small = mw.text.tag({name="small", contents=editlink, params={}})
        edit = " " .. mw.text.tag({name="span", contents=small, params={class="plainlinks"}})
    else
        edit = ""
    end
    local title = linkedtitle(data.title, data.link) .. edit
    local style = makestyle(styles)
    return  "{| class=\"wikitable hlist\" " .. style .. "\n|+" .. title .. "\n" .. table.concat(text) .. "|-\n! colspan=\"3\"|" .. data.other .. "\n|-\n| colspan=\"3\"|\n* " .. table.concat(otherlinks, "\n* ") .. "\n|}"
end

return z