Module:BrandDirectory: Difference between revisions
Create Lua module for brand directory (via create-page on MediaWiki MCP Server) |
Fix bracket escaping for SMW query (via update-page on MediaWiki MCP Server) |
||
| Line 2: | Line 2: | ||
function p.brandDirectory(frame) | function p.brandDirectory(frame) | ||
-- Use callParserFunction for SMW query | |||
local queryResult = frame:callParserFunction{ | |||
name = '#ask', | |||
args = { | |||
'[[Category:CRT models]]', | |||
-- | '?Has brand', | ||
local | format = 'array', | ||
sep = ';;;', | |||
propsep = ':::', | |||
limit = '1000' | |||
} | } | ||
} | |||
if not | if not queryResult or queryResult == "" then | ||
return "No CRT models found" | return "No CRT models found" | ||
end | end | ||
-- | -- Parse results and count brands | ||
local brandCounts = {} | local brandCounts = {} | ||
for | for entry in string.gmatch(queryResult, "[^;;;]+") do | ||
local | -- Each entry is "PageName:::BrandName" | ||
if | local parts = mw.text.split(entry, ":::") | ||
if parts[2] then | |||
local brand = mw.text.trim(parts[2]) | |||
if brand ~= "" then | |||
if | |||
brandCounts[brand] = (brandCounts[brand] or 0) + 1 | brandCounts[brand] = (brandCounts[brand] or 0) + 1 | ||
end | end | ||
| Line 42: | Line 41: | ||
end) | end) | ||
-- Generate HTML output | -- Generate HTML output with responsive column styles | ||
local html = mw.html.create('div') | local html = mw.html.create('div') | ||
:addClass('brand-directory') | :addClass('brand-directory') | ||
: | :cssText('column-count: 4; column-gap: 2em; margin: 1.5em 0;') | ||
for _, brand in ipairs(sortedBrands) do | for _, brand in ipairs(sortedBrands) do | ||
local count = brandCounts[brand] | local count = brandCounts[brand] | ||
local item = html:tag('div') | local item = html:tag('div') | ||
: | :cssText('break-inside: avoid; margin-bottom: 0.5em; line-height: 1.6;') | ||
-- Create link to brand page | -- Create link to brand page using wikitext | ||
local brandPage = "CRTs by " .. brand | local brandPage = "CRTs by " .. brand | ||
local linkMarkup = "[[" .. brandPage .. "|" .. brand .. "]]" | |||
item:wikitext(linkMarkup) | |||
item:tag('span') | item:tag('span') | ||
: | :cssText('color: #72777d; font-size: 0.9em; margin-left: 0.3em;') | ||
:wikitext('(' .. count .. ')') | :wikitext('(' .. count .. ')') | ||
end | end | ||
return tostring(html) | -- Add responsive CSS | ||
local style = mw.html.create('style') | |||
:wikitext('@media (max-width: 1200px) { .brand-directory { column-count: 3 !important; } }') | |||
:wikitext('@media (max-width: 800px) { .brand-directory { column-count: 2 !important; } }') | |||
:wikitext('@media (max-width: 500px) { .brand-directory { column-count: 1 !important; } }') | |||
return tostring(style) .. tostring(html) | |||
end | end | ||
return p | return p | ||
Latest revision as of 16:41, 10 January 2026
Documentation for this module may be created at Module:BrandDirectory/doc
local p = {}
function p.brandDirectory(frame)
-- Use callParserFunction for SMW query
local queryResult = frame:callParserFunction{
name = '#ask',
args = {
'[[Category:CRT models]]',
'?Has brand',
format = 'array',
sep = ';;;',
propsep = ':::',
limit = '1000'
}
}
if not queryResult or queryResult == "" then
return "No CRT models found"
end
-- Parse results and count brands
local brandCounts = {}
for entry in string.gmatch(queryResult, "[^;;;]+") do
-- Each entry is "PageName:::BrandName"
local parts = mw.text.split(entry, ":::")
if parts[2] then
local brand = mw.text.trim(parts[2])
if brand ~= "" then
brandCounts[brand] = (brandCounts[brand] or 0) + 1
end
end
end
-- Sort brands alphabetically
local sortedBrands = {}
for brand, _ in pairs(brandCounts) do
table.insert(sortedBrands, brand)
end
table.sort(sortedBrands, function(a, b)
return string.lower(a) < string.lower(b)
end)
-- Generate HTML output with responsive column styles
local html = mw.html.create('div')
:addClass('brand-directory')
:cssText('column-count: 4; column-gap: 2em; margin: 1.5em 0;')
for _, brand in ipairs(sortedBrands) do
local count = brandCounts[brand]
local item = html:tag('div')
:cssText('break-inside: avoid; margin-bottom: 0.5em; line-height: 1.6;')
-- Create link to brand page using wikitext
local brandPage = "CRTs by " .. brand
local linkMarkup = "[[" .. brandPage .. "|" .. brand .. "]]"
item:wikitext(linkMarkup)
item:tag('span')
:cssText('color: #72777d; font-size: 0.9em; margin-left: 0.3em;')
:wikitext('(' .. count .. ')')
end
-- Add responsive CSS
local style = mw.html.create('style')
:wikitext('@media (max-width: 1200px) { .brand-directory { column-count: 3 !important; } }')
:wikitext('@media (max-width: 800px) { .brand-directory { column-count: 2 !important; } }')
:wikitext('@media (max-width: 500px) { .brand-directory { column-count: 1 !important; } }')
return tostring(style) .. tostring(html)
end
return p