Module:Multimedia

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

local p = {}

-- Helper function to split a string by a delimiter
local function splitString(input, delimiter)
    if input ~= nil and input ~= '' then
        local result = {}
        for value in string.gmatch(input, "([^" .. delimiter .. "]+)") do
            table.insert(result, value)
        end
        return result
    else
        return {}
    end
end

-- Helper function to format a list of items as formatted links
local function formatted_list(items, prefix)
    local formatted_items = {}

    for item in items:gmatch("[^;]+") do
        item = mw.text.trim(item) -- Trim spaces around the name
        table.insert(formatted_items, item)
    end

    local formatted_list
    if #formatted_items > 1 then
        local last_item = table.remove(formatted_items) -- Remove the last item
        formatted_list = table.concat(formatted_items, ", ") .. " and " .. last_item
    else
        formatted_list = formatted_items[1] or "" -- Return the single item or an empty string
    end

    -- Add prefix if provided
    if prefix and prefix ~= "" then
        formatted_list = prefix .. formatted_list
    end

    return formatted_list
end

local function multimediaIcon(mtype)
    local multimedia_type = mtype
    local icon
    if multimedia_type == 'Video' then
        icon = '<i class="fas fa-video mr-3"></i>'
    elseif multimedia_type == 'Audio' then
        icon = '<i class="fas fa-microphone mr-3"></i>'
    elseif multimedia_type == 'File' then
        icon = '<i class="fas fa-file mr-3"></i>'
    else
        icon = '<i class="fas fa-atlas mr-3"></i>'
    end

    return icon
end

function p.main(frame)
    -- Arguments
    local template = frame:getParent().args

    local title       = template['Multimedia title'] or ''
    local mtype        = template['Multimedia type'] or ''
    local year        = template['Multimedia year'] or ''
    local month       = template['Multimedia month'] or ''
    local tags        = template['Multimedia tag'] or ''
    local description = template['Multimedia description'] or ''
    local link        = template['Multimedia link'] or ''

    local header = ''
    if link ~= '' then
        header = frame:preprocess('[' .. link .. ' ' .. title .. ']')
    else
        header = title or ''
    end

    local icon = multimediaIcon(mtype) or ''
    local tagline = ''
    local parts = {}

    if tags ~= '' then
        table.insert(parts, tags or '')
    end
    if month ~= '' then
        table.insert(parts, month or '')
    end
    if year ~= '' then
        table.insert(parts, year or '')
    end

    if #parts > 0 then
        tagline = table.concat(parts, ' | ')
    end

    -- Assemble
    local output = {}
    local html = mw.html.create()

    local multimedia = html:tag('div')
        :addClass('multimedia mb-5')

    if header ~= '' then
        multimedia:tag('div')
            :addClass('h5 interface')
            :wikitext(header)
    end

    if tagline ~= '' then
        multimedia:tag('div')
            :addClass('small text-muted interface mb-2')
            :wikitext(icon .. tagline)
    end

    if description ~= '' then
        multimedia:tag('div')
            :addClass('mb-3')
            :wikitext(description)
    end

    return tostring(multimedia)
end

return p