Module:Editions
Jump to navigation
Jump to search
local p = {}
local data = mw.loadData("Module:Editions/data")
function p.main(frame)
local args = frame:getParent().args
return p._main(args)
end
function p._main(args)
local short = (args.short or "") ~= ""
local no_link = (args.nl or "") ~= ""
local no_italics = (args.noitalics or "") ~= ""
local linked_text = no_link and function(_, text)
return text
end or function(link_name, text)
if link_name == text then
return ("[[%s]]"):format(text)
end
return ("[[%s|%s]]"):format(link_name, text)
end
local remove_italics = no_italics and function(text)
return text:gsub("'", "")
end or function(text) return text end
local editions = {}
for i, arg in ipairs(args) do
arg = mw.text.trim(arg or "")
if arg ~= "" then
local edition_name = data.alias_map[arg:lower()]
if edition_name then
editions[i] = data.editions[edition_name]
else
-- Unknown edition rendered as "[[<args[i]> Edition]]"
editions[i] = {
short = arg,
no_suffix = arg,
full = arg .. " Edition",
link_name = arg .. " Edition",
has_edition_suffix = true,
}
end
end
end
local result = {}
for i, edition in ipairs(editions) do
if short then
if i ~= 1 then
table.insert(result, (i == #editions) and " & " or ", ")
end
table.insert(result, linked_text(edition.link_name, remove_italics(edition.short)))
else
local prev_was_edition = i > 1 and editions[i-1].has_edition_suffix
local prev2_was_edition = i > 2 and editions[i-2].has_edition_suffix
local next_is_edition = i ~= #editions and editions[i+1].has_edition_suffix
if not next_is_edition and prev_was_edition and edition.has_edition_suffix then
-- Insert an oxford comma if this is a list of more-than-2 editions
table.insert(result, prev2_was_edition and ", and " or " and ")
elseif i > 1 then
if i == #editions then
table.insert(result, i > 2 and ", and " or " and ")
else
table.insert(result, ", ")
end
end
if edition.has_edition_suffix and (prev_was_edition or next_is_edition) then
table.insert(result, linked_text(edition.link_name, remove_italics(edition.no_suffix)))
if not next_is_edition then
table.insert(result, " editions")
end
else
table.insert(result, linked_text(edition.link_name, remove_italics(edition.full)))
end
end
end
return table.concat(result, "")
end
return p