Module:C: Difference between revisions
From Teriock
More actions
// via Wikitext Extension for VSCode |
// via Wikitext Extension for VSCode |
||
| Line 1: | Line 1: | ||
local p = {} | |||
local p = {} | local p = {} | ||
| Line 5: | Line 7: | ||
local rawAbilities = args['a'] or '' | local rawAbilities = args['a'] or '' | ||
-- Parse abilities into a structured table | |||
local abilities = {} | local abilities = {} | ||
for ability in mw.text.gsplit(rawAbilities, ";;", true) do | for ability in mw.text.gsplit(rawAbilities, ";;", true) do | ||
| Line 10: | Line 13: | ||
if ability ~= "" then | if ability ~= "" then | ||
local name = ability | local name = ability | ||
local limited = "" | local limited, improved = "", "" | ||
local gifted, adept = false, false | |||
local gifted = false | |||
-- | -- flags | ||
if mw.ustring.find(ability, "%-g%-") then | if mw.ustring.find(ability, "%-g%-") then gifted = true end | ||
if mw.ustring.find(ability, "%-a%-") then adept = true end | |||
end | |||
-- | -- notes (order-insensitive, tolerate any following marker) | ||
local limitedMatch = mw.ustring.match(ability, "%-l%-(.-)(%-[liag]%-|$)") | local limitedMatch = mw.ustring.match(ability, "%-l%-(.-)(%-[liag]%-|$)") | ||
or mw.ustring.match(ability, "%-l%-(.+)$") | or mw.ustring.match(ability, "%-l%-(.+)$") | ||
if limitedMatch then | if limitedMatch then limited = mw.text.trim(limitedMatch) end | ||
local improvedMatch = mw.ustring.match(ability, "%-i%-(.-)(%-[liag]%-|$)") | local improvedMatch = mw.ustring.match(ability, "%-i%-(.-)(%-[liag]%-|$)") | ||
or mw.ustring.match(ability, "%-i%-(.+)$") | or mw.ustring.match(ability, "%-i%-(.+)$") | ||
if improvedMatch then | if improvedMatch then improved = mw.text.trim(improvedMatch) end | ||
-- | -- clean name (strip markers + their payloads/flags) | ||
name = mw.ustring.gsub(name, "%-l%-.+", "") | name = mw.ustring.gsub(name, "%-l%-.+", "") | ||
name = mw.ustring.gsub(name, "%-i%-.+", "") | name = mw.ustring.gsub(name, "%-i%-.+", "") | ||
| Line 56: | Line 46: | ||
end | end | ||
-- Sort alphabetically by name | -- Sort alphabetically by name (stable) | ||
table.sort(abilities, function(a, b) return a.name < b.name end) | table.sort(abilities, function(a, b) return a.name < b.name end) | ||
-- | -- Build the out string: | ||
-- {{AE|name|limited=...|improved=...|gifted=1|adept=1}} joined by ";; " | |||
local transclusions = {} | |||
for _, a in ipairs(abilities) do | |||
local t = {"{{AE|", a.name} | |||
if a.limited ~= "" then table.insert(t, "|limited=" .. a.limited) end | |||
if a.improved ~= "" then table.insert(t, "|improved=" .. a.improved) end | |||
if a.gifted then table.insert(t, "|gifted=1") end | |||
if a.adept then table.insert(t, "|adept=1") end | |||
table.insert(t, "}}") | |||
table.insert(transclusions, table.concat(t)) | |||
end | |||
local inner = table.concat(transclusions, ";; ") | |||
local out = '<div class="expandable-table"><div>' .. inner .. '</div></div>' | |||
-- Return the assembled string (you said this is an intermediate output) | |||
return out | |||
end | end | ||
return p | return p | ||
Revision as of 01:53, 12 August 2025
Documentation for this module may be created at Module:C/doc
local p = {}
local p = {}
function p.c(frame)
local args = frame.args
local rawAbilities = args['a'] or ''
-- Parse abilities into a structured table
local abilities = {}
for ability in mw.text.gsplit(rawAbilities, ";;", true) do
ability = mw.text.trim(ability)
if ability ~= "" then
local name = ability
local limited, improved = "", ""
local gifted, adept = false, false
-- flags
if mw.ustring.find(ability, "%-g%-") then gifted = true end
if mw.ustring.find(ability, "%-a%-") then adept = true end
-- notes (order-insensitive, tolerate any following marker)
local limitedMatch = mw.ustring.match(ability, "%-l%-(.-)(%-[liag]%-|$)")
or mw.ustring.match(ability, "%-l%-(.+)$")
if limitedMatch then limited = mw.text.trim(limitedMatch) end
local improvedMatch = mw.ustring.match(ability, "%-i%-(.-)(%-[liag]%-|$)")
or mw.ustring.match(ability, "%-i%-(.+)$")
if improvedMatch then improved = mw.text.trim(improvedMatch) end
-- clean name (strip markers + their payloads/flags)
name = mw.ustring.gsub(name, "%-l%-.+", "")
name = mw.ustring.gsub(name, "%-i%-.+", "")
name = mw.ustring.gsub(name, "%-g%-", "")
name = mw.ustring.gsub(name, "%-a%-", "")
name = mw.text.trim(name)
table.insert(abilities, {
name = name,
limited = limited,
improved = improved,
gifted = gifted,
adept = adept
})
end
end
-- Sort alphabetically by name (stable)
table.sort(abilities, function(a, b) return a.name < b.name end)
-- Build the out string:
-- {{AE|name|limited=...|improved=...|gifted=1|adept=1}} joined by ";; "
local transclusions = {}
for _, a in ipairs(abilities) do
local t = {"{{AE|", a.name}
if a.limited ~= "" then table.insert(t, "|limited=" .. a.limited) end
if a.improved ~= "" then table.insert(t, "|improved=" .. a.improved) end
if a.gifted then table.insert(t, "|gifted=1") end
if a.adept then table.insert(t, "|adept=1") end
table.insert(t, "}}")
table.insert(transclusions, table.concat(t))
end
local inner = table.concat(transclusions, ";; ")
local out = '<div class="expandable-table"><div>' .. inner .. '</div></div>'
-- Return the assembled string (you said this is an intermediate output)
return out
end
return p