Module:Commentary

Documentation for this module may be created at Module:Commentary/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

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

    local title       = template['Commentary title'] or ''
    local subtitle    = template['Commentary subtitle'] or ''
    local source      = template['Commentary source'] or ''
    local year        = template['Commentary year'] or ''
    local month       = template['Commentary month'] or ''
    local coauthors   = template['Commentary coauthors'] or ''
    local description = template['Commentary description'] or ''
    local link        = template['Commentary link'] or ''
    local file        = template['Commentary file'] or ''

    local filepath = ''
    if file ~= nil and file ~= '' then
        filepath = frame:preprocess('{{fullurl:{{PAGENAME:' .. file .. '}}}}')
    end

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

    local coauthors_list = ''
    if coauthors ~= '' then
        coauthors_list = 'Co-authors: ' .. formatted_list(coauthors)
    end

    local tagline = ''
    local parts = {}

    if coauthors ~= '' then
        table.insert(parts, coauthors_list or '')
    end
    if source ~= '' then
        table.insert(parts, source 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

    local source_secondary = frame:callParserFunction(
        '#set',
        'Secondary source=' .. source,
        '+sep=;'
    )

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

    local commentary = html:tag('div')
        :addClass('commentary mb-5')
        :wikitext(source_secondary)

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

    if subtitle ~= '' then
        commentary:tag('div')
            :wikitext(subtitle)
    end

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

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

    return tostring(commentary)
end

return p