Module:CRTModel
Documentation for this module may be created at Module:CRTModel/doc
-- Module:CRTModel
-- Builds the CRT infobox as strict HTML to avoid whitespace -> <p><br></p>
-- Uses frame:preprocess() so parser functions like #ask and #formredlink evaluate.
local p = {}
local function trim(s)
if s == nil then return nil end
s = tostring(s)
s = s:gsub('^%s+', ''):gsub('%s+$', '')
if s == '' then return nil end
return s
end
local function css(node, cssText)
if cssText and cssText ~= '' then
node:cssText(cssText)
end
return node
end
local function addSectionRow(tbl, title)
local tr = tbl:tag('tr')
css(tr:tag('td'), 'text-align:center; padding:0.5em; background:#e0e0e0; font-weight:bold;')
:attr('colspan', '2')
:wikitext(title)
end
local function addRow(tbl, label, valueHtmlOrWikitext, isRawHtml)
local tr = tbl:tag('tr')
css(tr:tag('th'), 'text-align:left; padding:0.2em 0.4em;')
:wikitext(label)
local td = tr:tag('td')
css(td, 'padding:0.2em 0.4em;')
-- After preprocess, what we have is HTML. Use :node() to insert it as HTML.
if isRawHtml then
td:node(mw.html.create('span'):wikitext(valueHtmlOrWikitext))
else
td:wikitext(valueHtmlOrWikitext)
end
end
local function addFullRow(tbl, valueHtmlOrWikitext, cssText, isRawHtml)
local tr = tbl:tag('tr')
local td = tr:tag('td'):attr('colspan', '2')
css(td, cssText or '')
if isRawHtml then
-- For raw HTML, insert directly without the span wrapper
td:wikitext(valueHtmlOrWikitext)
else
td:wikitext(valueHtmlOrWikitext)
end
end
function p.infobox(frame)
local parent = frame:getParent()
local args = parent and parent.args or frame.args
local title = mw.title.getCurrentTitle()
local pageName = title.text
local fullPageName = title.fullText
local pageId = title.id or 0
local brand = trim(args.brand)
local model_code = trim(args.model_code)
local image_main = trim(args.image_main)
local model_3d = trim(args.model_3d)
local crt_type = trim(args.crt_type)
local screen_size_in = trim(args.screen_size_in)
local tvl = trim(args.tvl)
local chassis = trim(args.chassis)
local height = trim(args.height)
local width = trim(args.width)
local depth = trim(args.depth)
local weight = trim(args.weight)
local standards_semantic = trim(args.standards_semantic)
local crtdatabase = trim(args.crtdatabase)
local consolemods = trim(args.consolemods)
local tbl = mw.html.create('table')
tbl:addClass('infobox')
css(tbl, 'float:right; clear:right; margin:0 0 1em 1em; width:22em; border:1px solid #a2a9b1; border-spacing:3px; background:#f8f9fa; font-size:88%; line-height:1.5em;')
-- Title row (no background color, centered, bold)
addFullRow(
tbl,
(brand or '') .. ' ' .. (model_code or ''),
'text-align:center; font-size:125%; font-weight:bold; padding:0.5em;',
false
)
if image_main then
local file = image_main:gsub('^File:', '')
addFullRow(
tbl,
string.format('[[File:%s|frameless|300px]]', file),
'text-align:center; padding:0;',
false
)
end
-- 3D model display (right below main photo)
if model_3d then
local file = model_3d:gsub('^File:', '')
addFullRow(
tbl,
string.format('[[File:%s]]', file),
'text-align:center; padding:0;',
false
)
end
addSectionRow(tbl, 'Overview')
-- Brand row using #formredlink (must preprocess)
if brand then
local brandWikitext = string.format(
'{{#formredlink: target=%s |form=Manufacturer |existing page link text=%s }}',
brand, brand
)
local brandHtml = frame:preprocess(brandWikitext)
addRow(tbl, 'Brand', brandHtml, true)
end
if model_code then
addRow(tbl, 'Model code', model_code, false)
else
addRow(tbl, 'Model code', '', false)
end
if crt_type then
addRow(tbl, 'Type', crt_type, false)
end
if screen_size_in then
addRow(tbl, 'Screen size', screen_size_in .. '″', false)
end
if tvl then
addRow(tbl, 'TVL', tvl, false)
end
if chassis then
local chassisWikitext = string.format(
'{{#formredlink: target=%s |form=Chassis |existing page link text=%s }}',
chassis, chassis
)
local chassisHtml = frame:preprocess(chassisWikitext)
addRow(tbl, 'Chassis', chassisHtml, true)
end
-- ECID row: build as a normal external link wikitext; preprocess not required, but fine.
local ecidWikitext = string.format('[{{fullurl:%s|curid=%d}} EC%d]', pageName, pageId, pageId)
addRow(tbl, 'ECID', frame:preprocess(ecidWikitext), true)
if height or width or depth then
addSectionRow(tbl, 'Dimensions')
local h = height or '–'
local w = width or '–'
local d = depth or '–'
addRow(tbl, 'H × W × D', string.format('%s × %s × %s', h, w, d), false)
end
-- Weight row: uses #show, so preprocess it
if weight then
local weightWikitext = '{{#show: ' .. fullPageName .. ' |?Weight # lb }} / {{#show: ' .. fullPageName .. ' |?Weight # kg }}'
addRow(tbl, 'Weight', frame:preprocess(weightWikitext), true)
end
-- Inputs section: preprocess #ask so it renders, but only show section if there are results
local askWikitext =
'{{#ask:\n' ..
' [[Has CRT model::' .. fullPageName .. ']]\n' ..
' |?Has video signal format=signal\n' ..
' |?Has connector type=connector\n' ..
' |?Has input count=count\n' ..
' |?Has input location=location\n' ..
' |format=ul\n' ..
' |template=CRT input inline\n' ..
' |named args=yes\n' ..
' |searchlabel=\n' ..
'}}'
local askHtml = frame:preprocess(askWikitext)
-- Only display Inputs section if the query returned content (not just whitespace)
if askHtml and trim(askHtml) then
addSectionRow(tbl, 'Inputs')
addFullRow(tbl, askHtml, 'padding:0.2em 0.4em;', true)
end
-- Only display Standards section if standards_semantic has content
if standards_semantic then
addSectionRow(tbl, 'Standards')
addFullRow(tbl, standards_semantic, 'padding:0.2em 0.4em;', false)
end
-- Documents section
addSectionRow(tbl, 'Documents')
-- Build the upload link
local target = pageName .. " - document"
local uploadUrl = mw.uri.fullUrl(
"Special:FormEdit",
{
form = "Document",
target = target,
["Document[crt_model]"] = pageName
}
)
-- Query for document count
local documentsCountWikitext =
'{{#ask:\n' ..
' [[Category:Documents]]\n' ..
' [[Has related CRT model::' .. fullPageName .. ']]\n' ..
' |format=count\n' ..
'}}'
local documentCount = frame:preprocess(documentsCountWikitext)
local hasDocuments = documentCount and tonumber(trim(documentCount)) and tonumber(trim(documentCount)) > 0
-- Create the content row
local tr = tbl:tag('tr')
local td = tr:tag('td'):attr('colspan', '2')
css(td, 'padding:0.2em 0.4em;')
if hasDocuments then
-- Use #ask - it returns an HTML table
local docsQueryWikitext =
'{{#ask:\n' ..
' [[Category:Documents]]\n' ..
' [[Has related CRT model::' .. fullPageName .. ']]\n' ..
' |?Has document type\n' ..
' |?Has document URL\n' ..
' |format=broadtable\n' ..
' |headers=hide\n' ..
' |link=none\n' ..
'}}'
local docsResult = frame:preprocess(docsQueryWikitext)
-- Create UL element
local ul = mw.html.create('ul')
ul:css('margin', '0'):css('padding-left', '1.5em')
-- Parse the HTML table - extract data from <td> tags
-- Pattern: <td ...>page</td><td ...>type</td><td ...>URL</td>
if docsResult then
-- Match each table row
for rowContent in docsResult:gmatch('<tr[^>]*>(.-)</tr>') do
local cells = {}
-- Extract content from each <td>
for cellContent in rowContent:gmatch('<td[^>]*>(.-)</td>') do
-- Strip any remaining HTML tags and get just the text
local cleanContent = cellContent:gsub('<[^>]+>', '')
table.insert(cells, trim(cleanContent))
end
-- cells[1] = page name, cells[2] = type, cells[3] = URL
if #cells >= 3 then
local docType = cells[2]
local docUrl = cells[3]
if docUrl and docUrl ~= '' then
local displayText = (docType and docType ~= '') and docType or 'Document'
-- Create list item with link
local li = ul:tag('li')
li:wikitext('[' .. docUrl .. ' ' .. displayText .. ']')
end
end
end
end
td:node(ul)
-- Add upload link as a button - outline style with blue border
local uploadDiv = mw.html.create('div')
uploadDiv:css('margin-top', '0.5em'):css('text-align', 'center')
uploadDiv:wikitext('[' .. tostring(uploadUrl) .. ' <span style="display:inline-block;padding:0.5em 1em;background-color:#ffffff;color:#3366cc;text-decoration:none;border:2px solid #3366cc;border-radius:2px;font-size:90%;font-weight:bold;">Upload a document</span>]')
td:node(uploadDiv)
else
-- Just show the upload link as a button - outline style with blue border
local uploadDiv = mw.html.create('div')
uploadDiv:css('text-align', 'center')
uploadDiv:wikitext('[' .. tostring(uploadUrl) .. ' <span style="display:inline-block;padding:0.5em 1em;background-color:#ffffff;color:#3366cc;text-decoration:none;border:2px solid #3366cc;border-radius:2px;font-size:90%;font-weight:bold;">Upload a document</span>]')
td:node(uploadDiv)
end
-- Other Databases section: only display if there are links
if crtdatabase or consolemods then
addSectionRow(tbl, 'Other Databases')
local links = {}
if crtdatabase then
table.insert(links, '<li>[' .. crtdatabase .. ' CRT Database]</li>')
end
if consolemods then
table.insert(links, '<li>[' .. consolemods .. ' Console Mods Wiki]</li>')
end
local linksHtml = '<ul style="margin:0; padding-left:1.5em;">' .. table.concat(links, '') .. '</ul>'
addFullRow(tbl, linksHtml, 'padding:0.2em 0.4em;', true)
end
return tostring(tbl)
end
return p