Module:Library work multi-author: Difference between revisions
From ProleWiki, the proletarian encyclopedia
More languages
More actions
Created page with "local p = {} function p.main(frame) local args = frame:getParent().args local items = {} for _, value in pairs(args) do if value ~= '' then -- ignores empty arguments table.insert(items, value) end end local count = #items if count == 0 then return "" -- returns nothing if empty elseif count == 1 then return items[1] -- just return the string input if alone elseif count ==..." |
m Using the same method as the other one |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
function | local function formatList(items) | ||
local count = #items | local count = #items | ||
if count == 0 then | if count == 0 then | ||
return "" -- returns nothing if empty | return "" -- returns nothing if empty | ||
| Line 24: | Line 14: | ||
end | end | ||
end | end | ||
-- Helper function to split comma-separated string | |||
local function splitString(input) | |||
local items = {} | |||
if not input or input == "" then | |||
return items | |||
end | |||
-- Split by comma and trim whitespace from each item | |||
for item in string.gmatch(input, "([^,]+)") do | |||
item = string.gsub(item, "^%s*(.-)%s*$", "%1") -- Trim whitespace | |||
if item ~= "" then | |||
table.insert(items, item) | |||
end | |||
end | |||
return items | |||
end | |||
-- Just prints "a,b,c, ... and z" | |||
function p.main(frame) | |||
local args = frame:getParent().args | |||
local inputString = args[1] or "" | |||
local items = splitString(inputString) | |||
return formatList(items) | |||
end | |||
-- Prints with links if exists "a,[[b]],[[c]], ... and z" | |||
function p.link(frame) | |||
local args = frame:getParent().args | |||
local inputString = args[1] or "" | |||
local items = splitString(inputString) | |||
local linkedItems = {} | |||
for _, value in ipairs(items) do | |||
local title = mw.title.new(value) -- creates object for page | |||
if title and title.exists then -- checks if page exists | |||
table.insert(linkedItems, "[[" .. value .. "]]") -- adds with wikilink format | |||
else | |||
table.insert(linkedItems, value) -- adds without wikilink format | |||
end | |||
end | |||
return formatList(linkedItems) | |||
end | |||
-- Returns categories if exists "[[Category:Library works by a]]" | |||
-- "[[Category:Library works by b]]" | |||
function p.cat(frame) | |||
local args = frame:getParent().args | |||
local inputString = args[1] or "" | |||
if inputString == "" then | |||
return "" | |||
end | |||
local items = splitString(inputString) | |||
local result = {} | |||
for _, item in ipairs(items) do | |||
local categoryName = "Category:Library works by " .. item | |||
local categoryTitle = mw.title.new(categoryName) | |||
-- Check if the category page exists using mw.title (same approach as link function) | |||
if categoryTitle and categoryTitle.exists then | |||
table.insert(result, "[[" .. categoryName .. "]]") | |||
end | |||
-- If category doesn't exist, we don't add anything (no else clause) | |||
end | |||
return table.concat(result, "\n") | |||
end | |||
return p | return p | ||
Latest revision as of 16:16, 16 October 2025
local p = {}
local function formatList(items)
local count = #items
if count == 0 then
return "" -- returns nothing if empty
elseif count == 1 then
return items[1] -- just return the string input if alone
elseif count == 2 then
return items[1] .. " and " .. items[2] -- returns "string1, string2, ... and last_string"
else
local last = table.remove(items)
return table.concat(items, ", ") .. " and " .. last
end
end
-- Helper function to split comma-separated string
local function splitString(input)
local items = {}
if not input or input == "" then
return items
end
-- Split by comma and trim whitespace from each item
for item in string.gmatch(input, "([^,]+)") do
item = string.gsub(item, "^%s*(.-)%s*$", "%1") -- Trim whitespace
if item ~= "" then
table.insert(items, item)
end
end
return items
end
-- Just prints "a,b,c, ... and z"
function p.main(frame)
local args = frame:getParent().args
local inputString = args[1] or ""
local items = splitString(inputString)
return formatList(items)
end
-- Prints with links if exists "a,[[b]],[[c]], ... and z"
function p.link(frame)
local args = frame:getParent().args
local inputString = args[1] or ""
local items = splitString(inputString)
local linkedItems = {}
for _, value in ipairs(items) do
local title = mw.title.new(value) -- creates object for page
if title and title.exists then -- checks if page exists
table.insert(linkedItems, "[[" .. value .. "]]") -- adds with wikilink format
else
table.insert(linkedItems, value) -- adds without wikilink format
end
end
return formatList(linkedItems)
end
-- Returns categories if exists "[[Category:Library works by a]]"
-- "[[Category:Library works by b]]"
function p.cat(frame)
local args = frame:getParent().args
local inputString = args[1] or ""
if inputString == "" then
return ""
end
local items = splitString(inputString)
local result = {}
for _, item in ipairs(items) do
local categoryName = "Category:Library works by " .. item
local categoryTitle = mw.title.new(categoryName)
-- Check if the category page exists using mw.title (same approach as link function)
if categoryTitle and categoryTitle.exists then
table.insert(result, "[[" .. categoryName .. "]]")
end
-- If category doesn't exist, we don't add anything (no else clause)
end
return table.concat(result, "\n")
end
return p