Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Library work multi-author: Difference between revisions

From ProleWiki, the proletarian encyclopedia
Forte (talk | contribs)
now with automatica wikilinking
Forte (talk | contribs)
Now string1,string2,string3, ... format
Line 15: Line 15:
end
end


function p.main(frame)
-- Helper function to split comma-separated string
    local args = frame:getParent().args
local function splitString(input)
     local items = {}
     local items = {}
    if not input or input == "" then
        return items
    end
      
      
     for _, value in pairs(args) do
    -- Split by comma and trim whitespace from each item
         if value ~= '' then
     for item in string.gmatch(input, "([^,]+)") do
             table.insert(items, value)
        item = string.gsub(item, "^%s*(.-)%s*$", "%1")  -- Trim whitespace
         if item ~= "" then
             table.insert(items, item)
         end
         end
     end
     end
      
      
    return items
end
function p.main(frame)
    local args = frame:getParent().args
    local inputString = args[1] or ""
   
    local items = splitString(inputString)
     return formatList(items)
     return formatList(items)
end
end
Line 30: Line 43:
function p.link(frame)
function p.link(frame)
     local args = frame:getParent().args
     local args = frame:getParent().args
     local items = {}
    local inputString = args[1] or ""
   
     local items = splitString(inputString)
    local linkedItems = {}
      
      
     for _, value in pairs(args) do
     for _, value in ipairs(items) do
         if value ~= '' then
         local title = mw.title.new(value) -- creates object for page
            local title = mw.title.new(value) -- creates object for page
        if title and title.exists then -- checks if page exists
            if title and title.exists then -- checks if page exists
            table.insert(linkedItems, "[[" .. value .. "]]") -- adds with wikilink format
                table.insert(items, "[[" .. value .. "]]") -- adds to the item table with wikilink format
        else
            else
            table.insert(linkedItems, value) -- adds without wikilink format
                table.insert(items, value) -- page doesn't exist, adds to the item table without the wikilink format
            end
         end
         end
     end
     end
      
      
     return formatList(items)
     return formatList(linkedItems)
end
end


return p
return p

Revision as of 02:59, 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

function p.main(frame)
    local args = frame:getParent().args
    local inputString = args[1] or ""
    
    local items = splitString(inputString)
    return formatList(items)
end

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

return p