Module:Instrument: Difference between revisions
From PickiPedia: A knowledge base of bluegrass, old time psychedelic jams, and other public domain music
Jump to navigationJump to search
Fix: use frame:callParserFunction for #set instead of mw.smw (Semantic Scribunto not installed) |
Fix alignment: trim whitespace, remove trailing newlines from row output |
||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local function trim(s) | |||
return s:match('^%s*(.-)%s*$') | |||
end | |||
local function row(label, value) | local function row(label, value) | ||
if not value or value == '' then return | if not value or trim(value) == '' then return nil end | ||
return | return '|-\n! style="text-align: right; padding: 3px;" | ' .. label .. '\n| style="padding: 3px;" | ' .. trim(value) | ||
end | end | ||
local function sectionHeader(label) | local function sectionHeader(label) | ||
return | return '|-\n! colspan="2" style="background: #ddd; padding: 3px;" | ' .. label | ||
end | end | ||
local function any(...) | local function any(...) | ||
for _, v in ipairs({...}) do | for _, v in ipairs({...}) do | ||
if v and v ~= '' then return true end | if v and trim(v) ~= '' then return true end | ||
end | end | ||
return false | return false | ||
end | |||
local function add(out, val) | |||
if val then table.insert(out, val) end | |||
end | end | ||
| Line 98: | Line 100: | ||
table.insert(out, '! colspan="2" style="background: #ccc; font-size: 1.1em; padding: 5px;" | ' .. header) | table.insert(out, '! colspan="2" style="background: #ccc; font-size: 1.1em; padding: 5px;" | ' .. header) | ||
if image ~= '' then | if trim(image) ~= '' then | ||
table.insert(out, '|-') | table.insert(out, '|-') | ||
table.insert(out, '| colspan="2" style="text-align: center; padding: 10px;" | [[File:' .. image .. '|280px]]') | table.insert(out, '| colspan="2" style="text-align: center; padding: 10px;" | [[File:' .. trim(image) .. '|280px]]') | ||
end | end | ||
add(out, row('Type', type_)) | |||
add(out, row('Serial', serial)) | |||
add(out, row('Year', year)) | |||
if current_owner ~= '' then | if trim(current_owner) ~= '' then | ||
local ownerDisplay = frame:expandTemplate{ title = 'm', args = { current_owner } } | local ownerDisplay = trim(frame:expandTemplate{ title = 'm', args = { current_owner } }) | ||
add(out, row('Owner', ownerDisplay)) | |||
end | end | ||
add(out, row('Previous', previous_owners)) | |||
if any(body_shape, top_wood, back_sides_wood, neck_wood, fingerboard_wood, bridge_wood) then | if any(body_shape, top_wood, back_sides_wood, neck_wood, fingerboard_wood, bridge_wood) then | ||
table.insert(out, sectionHeader('Construction')) | table.insert(out, sectionHeader('Construction')) | ||
add(out, row('Body', body_shape)) | |||
add(out, row('Top', top_wood)) | |||
add(out, row('Back/Sides', back_sides_wood)) | |||
add(out, row('Neck', neck_wood)) | |||
add(out, row('Fingerboard', fingerboard_wood)) | |||
add(out, row('Bridge', bridge_wood)) | |||
end | end | ||
if any(scale_length, nut_width, strings) then | if any(scale_length, nut_width, strings) then | ||
table.insert(out, sectionHeader('Specifications')) | table.insert(out, sectionHeader('Specifications')) | ||
add(out, row('Scale', scale_length)) | |||
add(out, row('Nut Width', nut_width)) | |||
add(out, row('Strings', strings)) | |||
end | end | ||
if electronics ~= '' then | if trim(electronics) ~= '' then | ||
table.insert(out, sectionHeader('Electronics')) | table.insert(out, sectionHeader('Electronics')) | ||
table.insert(out, '|-') | table.insert(out, '|-') | ||
table.insert(out, '| colspan="2" style="padding: 3px;" | ' .. electronics) | table.insert(out, '| colspan="2" style="padding: 3px;" | ' .. trim(electronics)) | ||
end | end | ||
Revision as of 04:24, 31 March 2026
Documentation for this module may be created at Module:Instrument/doc
local p = {}
local function trim(s)
return s:match('^%s*(.-)%s*$')
end
local function row(label, value)
if not value or trim(value) == '' then return nil end
return '|-\n! style="text-align: right; padding: 3px;" | ' .. label .. '\n| style="padding: 3px;" | ' .. trim(value)
end
local function sectionHeader(label)
return '|-\n! colspan="2" style="background: #ddd; padding: 3px;" | ' .. label
end
local function any(...)
for _, v in ipairs({...}) do
if v and trim(v) ~= '' then return true end
end
return false
end
local function add(out, val)
if val then table.insert(out, val) end
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 via #set parser function
local setArgs = {}
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
table.insert(setArgs, k .. '=' .. v)
end
end
if #setArgs > 0 then
frame:callParserFunction('#set', setArgs)
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 trim(image) ~= '' then
table.insert(out, '|-')
table.insert(out, '| colspan="2" style="text-align: center; padding: 10px;" | [[File:' .. trim(image) .. '|280px]]')
end
add(out, row('Type', type_))
add(out, row('Serial', serial))
add(out, row('Year', year))
if trim(current_owner) ~= '' then
local ownerDisplay = trim(frame:expandTemplate{ title = 'm', args = { current_owner } })
add(out, row('Owner', ownerDisplay))
end
add(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'))
add(out, row('Body', body_shape))
add(out, row('Top', top_wood))
add(out, row('Back/Sides', back_sides_wood))
add(out, row('Neck', neck_wood))
add(out, row('Fingerboard', fingerboard_wood))
add(out, row('Bridge', bridge_wood))
end
if any(scale_length, nut_width, strings) then
table.insert(out, sectionHeader('Specifications'))
add(out, row('Scale', scale_length))
add(out, row('Nut Width', nut_width))
add(out, row('Strings', strings))
end
if trim(electronics) ~= '' then
table.insert(out, sectionHeader('Electronics'))
table.insert(out, '|-')
table.insert(out, '| colspan="2" style="padding: 3px;" | ' .. trim(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