Module:GetEffect: Difference between revisions
Appearance
imported>Ziebster No edit summary |
imported>Ziebster No edit summary |
||
| Line 26: | Line 26: | ||
for _, part in ipairs(parts) do | for _, part in ipairs(parts) do | ||
part = mw.text.trim(part) | part = mw.text.trim(part) | ||
local lower_part = part:lower() | |||
local | local start_pos, end_pos = lower_part:find(normalizedStat, 1, true) | ||
if start_pos then | |||
local | -- Extract value before the stat name | ||
if | local value = mw.text.trim(part:sub(1, start_pos - 1)) | ||
-- Extract rest after the stat name | |||
local rest = mw.text.trim(part:sub(end_pos + 1)) | |||
-- If rest starts with '(', ignore it | |||
if rest:match('^%(') then | |||
rest = '' | |||
end | |||
-- Only proceed if there's no remaining text after stat (or ignored paren) | |||
if rest == '' then | |||
-- Strip leading '+' if present | -- Strip leading '+' if present | ||
if | if value:sub(1,1) == '+' then | ||
value = value:sub(2) | |||
end | end | ||
return | -- Strip trailing '%' if present | ||
value = value:gsub('%%$', '') | |||
-- Trim again | |||
value = mw.text.trim(value) | |||
return value | |||
end | end | ||
end | end | ||
Revision as of 19:37, 9 February 2026
local p = {}
function p.getEffect(frame)
local pageName = frame.args[1] or mw.title.getCurrentTitle().text
local statName = frame.args[2]
local page = mw.title.new(pageName)
if not page then
return 'Page not found'
end
local content = page:getContent()
local effect = content:match('|%s*effects%s*=%s*(.-)\n%s*|') or content:match('|%s*effects%s*=%s*(.-)%s*}}') or ''
effect = mw.text.trim(effect)
if not statName or statName == '' then
-- Return full effect if no stat specified
return effect
end
-- Normalize statName for case-insensitive matching
local normalizedStat = statName:lower()
-- Split the effect into comma-separated parts
local parts = mw.text.split(effect, ',')
for _, part in ipairs(parts) do
part = mw.text.trim(part)
local lower_part = part:lower()
local start_pos, end_pos = lower_part:find(normalizedStat, 1, true)
if start_pos then
-- Extract value before the stat name
local value = mw.text.trim(part:sub(1, start_pos - 1))
-- Extract rest after the stat name
local rest = mw.text.trim(part:sub(end_pos + 1))
-- If rest starts with '(', ignore it
if rest:match('^%(') then
rest = ''
end
-- Only proceed if there's no remaining text after stat (or ignored paren)
if rest == '' then
-- Strip leading '+' if present
if value:sub(1,1) == '+' then
value = value:sub(2)
end
-- Strip trailing '%' if present
value = value:gsub('%%$', '')
-- Trim again
value = mw.text.trim(value)
return value
end
end
end
-- If not found, return empty string
return ''
end
return p