Module:BooksAndResearch

Documentation for this module may be created at Module:BooksAndResearch/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 commentary_title       = template['BooksAndResearch title'] or ''
    local commentary_subtitle    = template['BooksAndResearch subtitle'] or ''
    local commentary_source      = template['BooksAndResearch publisher'] or ''
    local commentary_year        = template['BooksAndResearch year'] or ''
    local commentary_month       = template['BooksAndResearch month'] or ''
    local commentary_coauthors   = template['BooksAndResearch coauthors'] or ''
    local commentary_description = template['BooksAndResearch description'] or ''
    local commentary_link        = template['BooksAndResearch link'] or ''
    local commentary_file        = template['BooksAndResearch file'] or ''

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

    local commentary_header = ''
    if commentary_link ~= '' or commentary_file ~= '' then
        if commentary_filepath ~= '' then
            commentary_header = frame:preprocess('[' .. commentary_filepath .. ' ' .. commentary_title .. ']')
        else
            commentary_header = frame:preprocess('[' .. commentary_link .. ' ' .. commentary_title .. ']')
        end
    else
        commentary_header = commentary_title or ''
    end

    local commentary_coauthors_list = ''
    if commentary_coauthors ~= '' then
        commentary_coauthors_list = 'Co-authors: ' .. formatted_list(commentary_coauthors)
    end

    local commentary_tagline = ''
    local parts = {}

    if commentary_coauthors ~= '' then
        table.insert(parts, commentary_coauthors_list or '')
    end
    if commentary_source ~= '' then
        table.insert(parts, commentary_source or '')
    end
    if commentary_month ~= '' then
        table.insert(parts, commentary_month or '')
    end
    if commentary_year ~= '' then
        table.insert(parts, commentary_year or '')
    end

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

    local authors_list = ''
    local authors = {}
    
    if commentary_coauthors ~= '' then
        table.insert(authors, commentary_coauthors or '')
    end
    table.insert(authors, page or '')

    if #authors > 0 then 
        authors_list = table.concat(authors, ';')
    end

    local set_properties = frame:callParserFunction(
        '#set',
        'Publisher=' .. commentary_source,
        '+sep=;',
        'Co-author=' .. authors_list,
        '+sep=;'
    )

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

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

    local title = commentary:tag('div')
        :addClass('h5 interface')
        :wikitext(commentary_header)

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

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

    if commentary_description ~= '' then
        local description = commentary:tag('div')
            :addClass('mb-3')
            :wikitext('\n' .. commentary_description)
    end

    return tostring(html)
end

return p