Module:Sandbox/Daniel Carrero/gamelink
Documentation for this module may be created at Module:Sandbox/Daniel Carrero/gamelink/doc
local maingames = require("Module:Sandbox/Daniel Carrero/gamelink/games")
local japanesegames = require("Module:Sandbox/Daniel Carrero/gamelink/Japanese games")
local export = {}
function export.gamelinks(frame)
local args = frame:getParent().args
return parsegames(args)
end
function parsegames(args)
local result = ""
local games = {}
local gamecount = 0
local conjunction
if args["conj"] then
conjunction = args["conj"]
end
-- The language can be assigned with the "lang" parameter. Currently only "lang=ja" is accepted.
-- If no language was assigned but Pokémon Green is found, assume that the language is Japanese.
-- In particular, this affects the links to Pokémon Red and Blue, which have separate pages for the Japanese versions.
local language
if args["lang"] then
language = args["lang"]
else
for k, v in pairs(args) do
if tonumber(k) and v == "Green" then
language = "ja"
break
end
end
end
-- The template arguments may contain single games, shortcuts to multiple games, or arbitrary text
for k, v in ipairs(args) do
if tonumber(k) then
local gamevalues
local currentgame
local currenttext
local currentlink
local alt
-- If the Japanese language is enabled, use the Japanese version for this particular game if it exists.
-- Use the main game list if no Japanese version exists for this game or the Japanese version is not enabled.
if language == "ja" and japanesegames[v] then
gamevalues = japanesegames[v]
else
gamevalues = maingames[v]
end
if gamevalues then
if type(gamevalues) == "table" then
for k2, v2 in ipairs(gamevalues) do
if tonumber(k2) then
gamecount = gamecount + 1
if k2 == 1 then
alt = "alt"
else
alt = "alt" .. k2
end
if gamevalues[alt] then
currenttext = gamevalues[alt]
else
if language == "ja" then
currenttext = v2
else
currenttext = v2
end
end
if language == "ja" then
currentlink = japanesegames[v2]
else
currentlink = maingames[v2]
end
currentgame = {text=currenttext, link=currentlink}
table.insert(games, currentgame)
end
end
else
gamecount = gamecount + 1
currentgame = {text=v, link=gamevalues}
table.insert(games, currentgame)
end
else
error("Unexpected game title", 0)
end
end
end
if gamecount > 0 then
local currentlink
local nextlink
local currenttext
local firstlink = true
for k, v in ipairs(games) do
if v["link"] and not currentlink then
currentlink = v["link"]
end
-- The link of the next game in the list, if any
if games[k + 1] and games[k + 1]["link"] then
nextlink = games[k + 1]["link"]
else
nextlink = nil
end
if currenttext then
currenttext = currenttext .. v["text"]
else
currenttext = v["text"]
end
if k == 1 then
if currentlink and string.find(currentlink, "^Pokémon:") then
prefix = "Pokémon:"
else
prefix = "Pokémon"
end
currenttext = prefix .. " " .. currenttext
end
if currentlink == nextlink and currentlink ~= nil then
currenttext = currenttext .. separator(k, gamecount, conjunction)
else
if currentlink then
if firstlink == true and args["alt"] then
currenttext = args["alt"]
end
firstlink = false
result = result .. "[[" .. currentlink .. "|" .. currenttext .. "]]"
else
result = result .. currenttext
end
if k < gamecount then
result = result .. separator(k, gamecount, conjunction)
end
currentlink = nil
currenttext = nil
end
end
end
return result
end
function separator(currentnumber,lastnumber,conjunction)
local result = ""
if not conjunction then
conjunction = "and"
end
if currentnumber == 1 and lastnumber == 2 then
result = " " .. conjunction .. " "
elseif currentnumber < lastnumber then
if currentnumber == lastnumber - 1 then
result = ", " .. conjunction .. " "
else
result = ", "
end
end
return result
end
return export
Categories
- No categories associated with this article