Module:Instrument

From PickiPedia: A knowledge base of bluegrass, old time psychedelic jams, and other public domain music
Revision as of 03:58, 31 March 2026 by Magent (talk | contribs) (Create Lua instrument infobox module)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

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

local p = {}

local function row(label, value)
    if not value or value == '' then return '' end
    return string.format(
        '|-\n! style="text-align: right; padding: 3px;" | %s\n| style="padding: 3px;" | %s\n',
        label, value
    )
end

local function sectionHeader(label)
    return string.format(
        '|-\n! colspan="2" style="background: #ddd; padding: 3px;" | %s\n',
        label
    )
end

local function any(...)
    for _, v in ipairs({...}) do
        if v and v ~= '' then return true end
    end
    return false
end

function p.infobox(frame)
    local args = frame:getParent().args
    local type_ = args['type'] or 'Instrument'
    local nickname = args['nickname'] or ''
    local make = args['make'] or ''
    local model = args['model'] or ''
    local serial = args['serial'] or ''
    local year = args['year'] or ''
    local image = args['image'] or ''
    local current_owner = args['current_owner'] or ''
    local previous_owners = args['previous_owners'] or ''
    local body_shape = args['body_shape'] or ''
    local top_wood = args['top_wood'] or ''
    local back_sides_wood = args['back_sides_wood'] or ''
    local neck_wood = args['neck_wood'] or ''
    local fingerboard_wood = args['fingerboard_wood'] or ''
    local bridge_wood = args['bridge_wood'] or ''
    local scale_length = args['scale_length'] or ''
    local nut_width = args['nut_width'] or ''
    local strings = args['strings'] or ''
    local electronics = args['electronics'] or ''

    -- SMW properties
    local smw = mw.smw
    if smw then
        local setProps = {}
        local propMap = {
            ['Instrument type'] = type_,
            ['Nickname'] = nickname,
            ['Make'] = make,
            ['Model'] = model,
            ['Serial'] = serial,
            ['Year built'] = year,
            ['Current owner'] = current_owner,
            ['Body shape'] = body_shape,
            ['Top wood'] = top_wood,
            ['Back and sides wood'] = back_sides_wood,
            ['Neck wood'] = neck_wood,
            ['Fingerboard wood'] = fingerboard_wood,
            ['Bridge wood'] = bridge_wood,
            ['Scale length'] = scale_length,
            ['Nut width'] = nut_width,
            ['Number of strings'] = strings,
            ['Electronics'] = electronics,
        }
        for k, v in pairs(propMap) do
            if v ~= '' then setProps[k] = v end
        end
        smw.set(setProps)
    end

    -- Header
    local header
    if make ~= '' then
        header = make .. ' ' .. model
        if serial ~= '' then
            if tonumber(serial) then
                header = header .. ' #' .. serial
            else
                header = header .. ' ' .. serial
            end
        end
    elseif nickname ~= '' then
        header = "''" .. nickname .. "''"
    else
        header = type_
    end

    -- Build table
    local out = {}
    table.insert(out, '{| class="infobox" style="width: 300px; border: 1px solid #aaa; background: #f9f9f9; padding: 10px; margin: 0 0 1em 1em; float: right;"')
    table.insert(out, '|-')
    table.insert(out, '! colspan="2" style="background: #ccc; font-size: 1.1em; padding: 5px;" | ' .. header)

    if image ~= '' then
        table.insert(out, '|-')
        table.insert(out, '| colspan="2" style="text-align: center; padding: 10px;" | [[File:' .. image .. '|280px]]')
    end

    table.insert(out, row('Type', type_))
    table.insert(out, row('Serial', serial))
    table.insert(out, row('Year', year))

    if current_owner ~= '' then
        local ownerDisplay = frame:expandTemplate{ title = 'm', args = { current_owner } }
        table.insert(out, row('Owner', ownerDisplay))
    end
    table.insert(out, row('Previous', previous_owners))

    if any(body_shape, top_wood, back_sides_wood, neck_wood, fingerboard_wood, bridge_wood) then
        table.insert(out, sectionHeader('Construction'))
        table.insert(out, row('Body', body_shape))
        table.insert(out, row('Top', top_wood))
        table.insert(out, row('Back/Sides', back_sides_wood))
        table.insert(out, row('Neck', neck_wood))
        table.insert(out, row('Fingerboard', fingerboard_wood))
        table.insert(out, row('Bridge', bridge_wood))
    end

    if any(scale_length, nut_width, strings) then
        table.insert(out, sectionHeader('Specifications'))
        table.insert(out, row('Scale', scale_length))
        table.insert(out, row('Nut Width', nut_width))
        table.insert(out, row('Strings', strings))
    end

    if electronics ~= '' then
        table.insert(out, sectionHeader('Electronics'))
        table.insert(out, '|-')
        table.insert(out, '| colspan="2" style="padding: 3px;" | ' .. electronics)
    end

    local detailsForm = ({
        Guitar = 'GuitarDetails',
        Banjo = 'BanjoDetails',
        Mandolin = 'MandolinDetails',
    })[type_] or 'InstrumentDetails'

    local pageName = mw.title.getCurrentTitle().fullText
    table.insert(out, '|-')
    table.insert(out, '| colspan="2" style="text-align: center; padding: 8px 3px 3px;" | [[Special:FormEdit/' .. detailsForm .. '/' .. pageName .. '|Add details & photos]]')
    table.insert(out, '|}')
    table.insert(out, '[[Category:Instruments]][[Category:' .. type_ .. 's]]')

    return table.concat(out, '\n')
end

return p