Module:SidebarPeople

From Bodhicitta
Revision as of 12:02, 17 October 2025 by Jeremi (talk | contribs) ((by SublimeText.Mediawiker))

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

local p = {}

function p.main(frame)

    local pargs = frame:getParent().args
    local typesIndexed = {
        [1] = 'sourceauthor',
        [2] = 'authors',
        [3] = 'translators',
        [4] = 'contributors',
        [5] = 'editors',
        [6] = 'publisher',
        [7] = 'creators',
        [8] = 'performers',
        [9] = 'producers'
    }
    local typeLabels = {
        sourceauthor = "source author",
        authors = "author",
        translators = "translator",
        contributors = "contributor",
        editors = "editor",
        publisher = "publisher",
        creators = "creator",
        performers = "performer",
        producers = "producer"
    }
    local pplTbl = {}

    local output = '<div class="d-flex justify-content-center flex-row flex-wrap">'
    for role, people in pairs(pargs) do
        for person in string.gmatch(people, "([^;]+)") do
            if pplTbl[person] then else
                pplTbl[person] = {}
            end
            table.insert(pplTbl[person], role)
        end
    end
    for _, ptype in ipairs(typesIndexed) do
        for person,roles in pairs(pplTbl) do
            if type(roles) == 'string' then
                if ptype == roles then
                    output = output .. frame:expandTemplate{ title = 'SidebarPersonBadge', args = { 
                        person , 
                        typeLabels[roles]
                    } }
                    pplTbl[person] = ''
                end
            elseif type(roles) == 'table' then
                for _,role in ipairs(roles) do
                    if ptype == role then
                        output = output .. frame:expandTemplate{ title = 'SidebarPersonBadge', args = { 
                            person , 
                            listRoles(roles, typeLabels, typesIndexed)
                        } }
                        pplTbl[person] = ''
                    end
                end
            end
        end
    end
    output = output .. '</div>'

    return output

end

function sortRoles(roles, typesIndexed)
    local typeIndices = {}
    for i, role in ipairs(typesIndexed) do
        typeIndices[role] = i
    end
    
    local sortedRoles = {}
    for role, _ in pairs(roles) do
        table.insert(sortedRoles, role)
    end
    
    table.sort(sortedRoles, function(a, b)
        return typeIndices[a] < typeIndices[b]
    end)
    
    return sortedRoles
end

function listRoles(roles, typeLabels, typesIndexed)
    local uniqueRoles = {}
    for _, role in pairs(roles) do
        uniqueRoles[role] = true
    end
    
    local sortedRoles = sortRoles(uniqueRoles, typesIndexed)
    local list = {}
    
    for _, role in ipairs(sortedRoles) do
        table.insert(list, typeLabels[role])
    end
    
    return table.concat(list, ';')
end

function makeTblUnique(indexedTable)
    local uniqueTbl = {}
    for _, value in ipairs(indexedTable) do
        uniqueTbl[value] = true
    end
    indexedTable = {}
    for value,_ in pairs(uniqueTbl) do
        table.insert(indexedTable, value)
    end
    return indexedTable
end

return p