Module:VariantGrid: Difference between revisions
From Piñata Journal
More actions
Admin Jeremy (talk | contribs) m Admin Jeremy moved page Module:PinataVariants to Module:VariantGrid |
Admin Jeremy (talk | contribs) No edit summary |
||
| Line 3: | Line 3: | ||
local function notEmpty(val) | local function notEmpty(val) | ||
return val and val ~= "" | return val and val ~= "" | ||
end | |||
-- Render single item token into icon format | |||
local function renderItem(token) | |||
token = mw.text.trim(token) | |||
-- Detect quantity (Apple x2) | |||
local name, qty = token:match("^(.-)%s*x(%d+)$") | |||
if not name then | |||
name = token | |||
end | |||
local icon = string.format( | |||
'[[File:%s.png|28px|link=%s]]', | |||
name, | |||
name | |||
) | |||
if qty then | |||
return icon .. '<span class="vpvg-qty">×' .. qty .. '</span>' | |||
else | |||
return icon | |||
end | |||
end | |||
-- Parse multi-item strings separated by + | |||
local function renderRequirement(content) | |||
if not notEmpty(content) then | |||
return "" | |||
end | |||
local parts = mw.text.split(content, "%+") | |||
local rendered = {} | |||
for _, part in ipairs(parts) do | |||
table.insert(rendered, renderItem(part)) | |||
end | |||
return table.concat(rendered, '<span class="vpvg-plus">+</span>') | |||
end | end | ||
| Line 9: | Line 49: | ||
return "" | return "" | ||
end | end | ||
return string.format( | return string.format( | ||
'<div class="vpvg-req %s"><span class="vpvg-game">%s:</span> %s</div>', | '<div class="vpvg-req %s"><span class="vpvg-game">%s:</span> %s</div>', | ||
class, | class, | ||
label, | label, | ||
content | renderRequirement(content) | ||
) | ) | ||
end | end | ||
| Line 23: | Line 63: | ||
return "" | return "" | ||
end | end | ||
local image = args["variant" .. index .. "_image"] or "" | local image = args["variant" .. index .. "_image"] or "" | ||
local vp1 = args["variant" .. index .. "_vp1"] | local vp1 = args["variant" .. index .. "_vp1"] | ||
local tip = args["variant" .. index .. "_tip"] | local tip = args["variant" .. index .. "_tip"] | ||
local pp = args["variant" .. index .. "_pp"] | local pp = args["variant" .. index .. "_pp"] | ||
local html = {} | local html = {} | ||
table.insert(html, '<div class="vpvg-col">') | table.insert(html, '<div class="vpvg-col">') | ||
table.insert(html, '<div class="vpvg-title">' .. title .. '</div>') | table.insert(html, '<div class="vpvg-title">' .. title .. '</div>') | ||
if notEmpty(image) then | if notEmpty(image) then | ||
table.insert(html, | table.insert(html, | ||
| Line 39: | Line 79: | ||
) | ) | ||
end | end | ||
table.insert(html, gameRow("VP1", "vp1", vp1)) | table.insert(html, gameRow("VP1", "vp1", vp1)) | ||
table.insert(html, gameRow("TIP", "tip", tip)) | table.insert(html, gameRow("TIP", "tip", tip)) | ||
table.insert(html, gameRow("PP", "pp", pp)) | table.insert(html, gameRow("PP", "pp", pp)) | ||
table.insert(html, '</div>') | table.insert(html, '</div>') | ||
return table.concat(html) | return table.concat(html) | ||
end | end | ||
| Line 51: | Line 91: | ||
function p.main(frame) | function p.main(frame) | ||
local args = frame:getParent().args | local args = frame:getParent().args | ||
local output = {} | local output = {} | ||
table.insert(output, '<div class="vpvg-grid">') | table.insert(output, '<div class="vpvg-grid">') | ||
for i = 1, 4 do | for i = 1, 4 do | ||
table.insert(output, renderVariant(args, i)) | table.insert(output, renderVariant(args, i)) | ||
end | end | ||
table.insert(output, '</div>') | table.insert(output, '</div>') | ||
return table.concat(output) | return table.concat(output) | ||
end | end | ||
return p | return p | ||
Revision as of 14:29, 4 March 2026
Documentation for this module may be created at Module:VariantGrid/doc
local p = {}
local function notEmpty(val)
return val and val ~= ""
end
-- Render single item token into icon format
local function renderItem(token)
token = mw.text.trim(token)
-- Detect quantity (Apple x2)
local name, qty = token:match("^(.-)%s*x(%d+)$")
if not name then
name = token
end
local icon = string.format(
'[[File:%s.png|28px|link=%s]]',
name,
name
)
if qty then
return icon .. '<span class="vpvg-qty">×' .. qty .. '</span>'
else
return icon
end
end
-- Parse multi-item strings separated by +
local function renderRequirement(content)
if not notEmpty(content) then
return ""
end
local parts = mw.text.split(content, "%+")
local rendered = {}
for _, part in ipairs(parts) do
table.insert(rendered, renderItem(part))
end
return table.concat(rendered, '<span class="vpvg-plus">+</span>')
end
local function gameRow(label, class, content)
if not notEmpty(content) then
return ""
end
return string.format(
'<div class="vpvg-req %s"><span class="vpvg-game">%s:</span> %s</div>',
class,
label,
renderRequirement(content)
)
end
local function renderVariant(args, index)
local title = args["variant" .. index .. "_title"]
if not notEmpty(title) then
return ""
end
local image = args["variant" .. index .. "_image"] or ""
local vp1 = args["variant" .. index .. "_vp1"]
local tip = args["variant" .. index .. "_tip"]
local pp = args["variant" .. index .. "_pp"]
local html = {}
table.insert(html, '<div class="vpvg-col">')
table.insert(html, '<div class="vpvg-title">' .. title .. '</div>')
if notEmpty(image) then
table.insert(html,
'<div class="vpvg-image">[[File:' .. image .. '|160px]]</div>'
)
end
table.insert(html, gameRow("VP1", "vp1", vp1))
table.insert(html, gameRow("TIP", "tip", tip))
table.insert(html, gameRow("PP", "pp", pp))
table.insert(html, '</div>')
return table.concat(html)
end
function p.main(frame)
local args = frame:getParent().args
local output = {}
table.insert(output, '<div class="vpvg-grid">')
for i = 1, 4 do
table.insert(output, renderVariant(args, i))
end
table.insert(output, '</div>')
return table.concat(output)
end
return p