Module:Appearances: Difference between revisions
From Gensopedia
More actions
mNo edit summary |
mNo edit summary |
||
| Line 1: | Line 1: | ||
-- Module:Appearances | -- Module:Appearances | ||
-- | -- Renders a character's appearances using short codes or full titles. | ||
-- | -- Supports: | ||
-- | -- - |appears=S1, Suikoden II, G1| | ||
-- - |S1=yes|G1=yes| (legacy flags) | |||
-- - Mixed input (codes and names) | |||
-- - Auto category linking and display names | |||
local p = {} | local p = {} | ||
-- Master lookup: | -- Master lookup: code → { page name, category, optional display name } | ||
local games = { | local games = { | ||
S1 = { "Suikoden", "Characters in Suikoden" }, | S1 = { "Suikoden", "Characters in Suikoden" }, | ||
| Line 35: | Line 38: | ||
} | } | ||
-- Reverse lookup: full title → code | -- Reverse lookup: full title → short code | ||
local titleToCode = {} | local titleToCode = {} | ||
for code, data in pairs(games) do | for code, data in pairs(games) do | ||
| Line 54: | Line 57: | ||
local inputCodes = {} | local inputCodes = {} | ||
-- 1️⃣ | -- 1️⃣ Read comma-separated list from |appears= | ||
if args. | if args.appears and args.appears ~= "" then | ||
for _, token in ipairs(mw.text.split(args. | for _, token in ipairs(mw.text.split(args.appears, ",")) do | ||
token = trim(token) | token = trim(token) | ||
if token ~= "" then | if token ~= "" then | ||
-- Normalize to short code if | -- Normalize to short code if full name is given | ||
local code = titleToCode[token] or token | local code = titleToCode[token] or token | ||
if games[code] then | if games[code] then | ||
| Line 68: | Line 71: | ||
end | end | ||
-- 2️⃣ | -- 2️⃣ Legacy syntax: |S1=yes|G1=yes| | ||
for code, _ in pairs(games) do | for code, _ in pairs(games) do | ||
if (args[code] or ""):lower() == "yes" then | if (args[code] or ""):lower() == "yes" then | ||
| Line 84: | Line 87: | ||
end | end | ||
-- 4️⃣ Sort codes for | -- 4️⃣ Sort codes alphabetically for consistency | ||
table.sort(unique, function(a, b) return a < b end) | table.sort(unique, function(a, b) return a < b end) | ||
Revision as of 13:20, 10 October 2025
Documentation for this module may be created at Module:Appearances/doc
-- Module:Appearances
-- Renders a character's appearances using short codes or full titles.
-- Supports:
-- - |appears=S1, Suikoden II, G1|
-- - |S1=yes|G1=yes| (legacy flags)
-- - Mixed input (codes and names)
-- - Auto category linking and display names
local p = {}
-- Master lookup: code → { page name, category, optional display name }
local games = {
S1 = { "Suikoden", "Characters in Suikoden" },
S2 = { "Suikoden II", "Characters in Suikoden II" },
G1 = { "Genso Suikogaiden Vol.1", "Characters in Genso Suikogaiden Vol.1" },
G2 = { "Genso Suikogaiden Vol.2", "Characters in Genso Suikogaiden Vol.2" },
CS = { "Genso Suikoden Card Stories", "Characters in Genso Suikoden Card Stories" },
S3 = { "Suikoden III", "Characters in Suikoden III" },
S4 = { "Suikoden IV", "Characters in Suikoden IV" },
ST = { "Suikoden Tactics", "Characters in Suikoden Tactics" },
S5 = { "Suikoden V", "Characters in Suikoden V" },
HS = { "Genso Suikoden Tierkreis Hoshikuzu no Shiro", "Characters in Genso Suikoden Tierkreis Hoshikuzu no Shiro" },
TK = { "Suikoden Tierkreis", "Characters in Suikoden Tierkreis" },
PS = { "Pachislot Genso Suikoden", "Characters in Pachislot Genso Suikoden" },
HT = { "Genso Suikoden Tsumugareshi Hyakunen no Toki", "Characters in Genso Suikoden Tsumugareshi Hyakunen no Toki" },
SP = { "Suikoden STAR LEAP", "Characters in Suikoden STAR LEAP" },
SL = { "Suikoden STAR LEAP", "Characters in Suikoden STAR LEAP" },
kpc = { "Konami Parody Comic Series Genso Suikoden", "Characters from Konami Parody Comic Series Genso Suikoden" },
SE = { "Genso Suikoden: Soul Eater 1", "Characters from Genso Suikoden Soul Eater", "Soul Eater 1" },
SE3 = { "Genso Suikoden: Soul Eater 3", "Characters from Genso Suikoden Soul Eater", "Soul Eater 3" },
S2n = { "Genso Suikoden II: 1 (novel)", "Characters from the Genso Suikoden II novel" },
S2n4= { "Genso Suikoden II: 4 (novel)", "Characters from the Genso Suikoden II novel" },
SS1 = { "Genso Suikoden Short Story Collection 1", "Characters from Genso Suikoden Short Story Collection 1" },
SS2 = { "Genso Suikoden Short Story Collection 2", "Characters from Genso Suikoden Short Story Collection 2" },
SS3 = { "Genso Suikoden Short Story Collection 3", "Characters from Genso Suikoden Short Story Collection 3" },
SS4 = { "Genso Suikoden Short Story Collection 4", "Characters from Genso Suikoden Short Story Collection 4" },
S4n = { "Genso Suikoden IV: 1 (novel)", "Characters from the Genso Suikoden IV novel" },
}
-- Reverse lookup: full title → short code
local titleToCode = {}
for code, data in pairs(games) do
titleToCode[data[1]] = code
end
local function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
local function makeLine(entry)
local title, category, display = entry[1], entry[2], entry[3]
return string.format("* [[%s|%s]] [[Category:%s]]", title, display or title, category)
end
function p.render(frame)
local args = frame.args
local inputCodes = {}
-- 1️⃣ Read comma-separated list from |appears=
if args.appears and args.appears ~= "" then
for _, token in ipairs(mw.text.split(args.appears, ",")) do
token = trim(token)
if token ~= "" then
-- Normalize to short code if full name is given
local code = titleToCode[token] or token
if games[code] then
table.insert(inputCodes, code)
end
end
end
end
-- 2️⃣ Legacy syntax: |S1=yes|G1=yes|
for code, _ in pairs(games) do
if (args[code] or ""):lower() == "yes" then
table.insert(inputCodes, code)
end
end
-- 3️⃣ Remove duplicates
local seen, unique = {}, {}
for _, code in ipairs(inputCodes) do
if not seen[code] then
seen[code] = true
table.insert(unique, code)
end
end
-- 4️⃣ Sort codes alphabetically for consistency
table.sort(unique, function(a, b) return a < b end)
-- 5️⃣ Build output
local output = {}
for _, code in ipairs(unique) do
table.insert(output, makeLine(games[code]))
end
return table.concat(output, "<br />\n")
end
return p