Module:CRTModel

Revision as of 23:11, 22 December 2025 by Arclight (talk | contribs) (Created page with "-- Module:CRTModel -- Builds the CRT infobox as strict HTML to avoid MediaWiki's whitespace -> <p><br></p> behavior. 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')...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

-- Module:CRTModel
-- Builds the CRT infobox as strict HTML to avoid MediaWiki's whitespace -> <p><br></p> behavior.

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, valueWikitext)
	local tr = tbl:tag('tr')
	css(tr:tag('th'), 'text-align:left; padding:0.2em 0.4em;')
		:wikitext(label)
	css(tr:tag('td'), 'padding:0.2em 0.4em;')
		:wikitext(valueWikitext)
end

local function addFullRow(tbl, valueWikitext, cssText)
	local tr = tbl:tag('tr')
	local td = tr:tag('td'):attr('colspan', '2')
	css(td, cssText or '')
	td:wikitext(valueWikitext)
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

	-- Read args (template params)
	local brand = trim(args.brand)
	local model_code = trim(args.model_code)
	local image_main = trim(args.image_main)
	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)

	-- Build table
	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;')

	-- Caption
	local caption = tbl:tag('caption')
	css(caption, 'font-size:125%; font-weight:bold; padding:0.5em;')
	caption:wikitext((brand or '') .. ' ' .. (model_code or ''))

	-- Image row
	if image_main then
		local file = image_main:gsub('^File:', '')
		addFullRow(
			tbl,
			string.format('[[File:%s|frameless|300px]]', file),
			'text-align:center; padding:0;'
		)
	end

	-- Overview section
	addSectionRow(tbl, 'Overview')

	-- Brand row (use formredlink like your template does)
	if brand then
		local brandLink = string.format(
			'{{#formredlink: target=%s |form=Manufacturer |existing page link text=%s }}',
			brand, brand
		)
		addRow(tbl, 'Brand', brandLink)
	end

	-- ECID row
	-- Keep your exact ECID behavior (curid link + EC<PageID>)
	local ecid = string.format('[{{fullurl:%s|curid=%d}} EC%d]', pageName, pageId, pageId)
	addRow(tbl, 'ECID', ecid)

	-- Model code row
	if model_code then
		addRow(tbl, 'Model code', model_code)
	else
		-- show empty but don't accidentally create paragraphs; an empty string here is fine
		addRow(tbl, 'Model code', '')
	end

	-- Optional rows
	if crt_type then
		addRow(tbl, 'Type', crt_type)
	end
	if screen_size_in then
		addRow(tbl, 'Screen size', screen_size_in .. '″')
	end
	if tvl then
		addRow(tbl, 'TVL', tvl)
	end
	if chassis then
		local chassisLink = string.format(
			'{{#formredlink: target=%s |form=Chassis |existing page link text=%s }}',
			chassis, chassis
		)
		addRow(tbl, 'Chassis', chassisLink)
	end

	-- Dimensions section (only if any are present)
	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))
	end

	-- Weight row (keep your #show behavior)
	if weight then
		addRow(tbl, 'Weight', '{{#show: ' .. fullPageName .. ' |?Weight # lb }} / {{#show: ' .. fullPageName .. ' |?Weight # kg }}')
	end

	-- Inputs section (keep your existing #ask)
	addSectionRow(tbl, 'Inputs')
	addFullRow(tbl,
		'{{#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' ..
		'}}',
		'padding:0.2em 0.4em;'
	)

	-- Standards section
	addSectionRow(tbl, 'Standards')
	addFullRow(tbl, standards_semantic or '', 'padding:0.2em 0.4em;')

	return tostring(tbl)
end

return p
MediaWiki Appliance - Powered by TurnKey Linux