Module:VariantGrid: Difference between revisions
From Piñata Journal
More actions
Admin Jeremy (talk | contribs) No edit summary |
Admin Jeremy (talk | contribs) No edit summary |
||
| Line 9: | Line 9: | ||
token = mw.text.trim(token) | token = mw.text.trim(token) | ||
local name, qty = token:match("^(.-)%s*x(%d+)$") | local name, qty = token:match("^(.-)%s*x(%d+)$") | ||
if not name then | if not name then | ||
name = token | name = token | ||
| Line 17: | Line 15: | ||
local icon = string.format( | local icon = string.format( | ||
'[[File:%s icon.png| | '[[File:%s icon.png|20px|link=%s]]', | ||
name, | name, | ||
name | name | ||
| Line 37: | Line 35: | ||
end | end | ||
local function renderRequirement(content) | local function renderRequirement(content) | ||
if not notEmpty(content) then | if not notEmpty(content) then | ||
| Line 53: | Line 50: | ||
end | end | ||
local function | local function requirementRow(label, class, content) | ||
if not notEmpty(content) then | if not notEmpty(content) then | ||
return "" | return "" | ||
| Line 59: | Line 56: | ||
return string.format( | return string.format( | ||
'< | '<tr class="vpvg-row %s">' .. | ||
'<td class="vpvg-gamecell">%s</td>' .. | |||
'<td class="vpvg-reqcell">%s</td>' .. | |||
'</tr>', | |||
class, | class, | ||
label, | label, | ||
| Line 88: | Line 88: | ||
end | end | ||
table.insert(html, | table.insert(html, '<table class="vpvg-reqtable">') | ||
table.insert(html, | |||
table.insert(html, | table.insert(html, requirementRow("VP1", "vp1", vp1)) | ||
table.insert(html, requirementRow("TIP", "tip", tip)) | |||
table.insert(html, requirementRow("PP", "pp", pp)) | |||
table.insert(html, '</table>') | |||
table.insert(html, '</div>') | table.insert(html, '</div>') | ||
Revision as of 12:51, 5 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 + text format
local function renderItem(token)
token = mw.text.trim(token)
local name, qty = token:match("^(.-)%s*x(%d+)$")
if not name then
name = token
end
local icon = string.format(
'[[File:%s icon.png|20px|link=%s]]',
name,
name
)
local text = string.format('[[%s]]', name)
local result = string.format(
'<span class="vpvg-item">%s<span class="vpvg-name">%s</span></span>',
icon,
text
)
if qty then
result = result .. '<span class="vpvg-qty">×' .. qty .. '</span>'
end
return result
end
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 requirementRow(label, class, content)
if not notEmpty(content) then
return ""
end
return string.format(
'<tr class="vpvg-row %s">' ..
'<td class="vpvg-gamecell">%s</td>' ..
'<td class="vpvg-reqcell">%s</td>' ..
'</tr>',
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, '<table class="vpvg-reqtable">')
table.insert(html, requirementRow("VP1", "vp1", vp1))
table.insert(html, requirementRow("TIP", "tip", tip))
table.insert(html, requirementRow("PP", "pp", pp))
table.insert(html, '</table>')
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