Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Please sign up or log in to edit the wiki.

Module:Remove Duplicate Links: Difference between revisions

From the Star Citizen Wiki, the fidelity™ encyclopedia
Content deleted Content added
Astrid (talk | contribs)
Created page with "local p = {} --- Remove duplicate links and return the singular ones function p.main(frame) local input = frame.args[1] or "" local links = {} input:gsub("%[%[([^%]]*)%]%]", function(link) link = "" .. link .. "" -- :gsub removes the double brackets table.insert(links, link) end) local duplicateTracker = {} for _, link in pairs(links) do if not duplicateTracker[link] then duplicateTracker[link] = 0..."
 
Astrid (talk | contribs)
No edit summary
Line 1: Line 1:
local p = {}
local p = {}

function startsWith(str, start)
return str:sub(1, #start) == start
end


--- Remove duplicate links and return the singular ones
--- Remove duplicate links and return the singular ones
Line 8: Line 12:
input:gsub("%[%[([^%]]*)%]%]", function(link)
input:gsub("%[%[([^%]]*)%]%]", function(link)
link = "[[" .. link .. "]]" -- :gsub removes the double brackets
link = "[[" .. link .. "]]" -- :gsub removes the double brackets
if frame.args["removeFiles"] == "yes" and not startsWith(string.lower(link), "file:") then
table.insert(links, link)
table.insert(links, link)
end
end)
end)



Revision as of 03:01, 10 November 2023

Module documentation[view][edit][history][purge]
This documentation is transcluded from Module:Remove Duplicate Links/doc. Changes can be proposed in the talk page.
Function list
L 3 — startsWith
L 8 — p.main

local p = {}

function startsWith(str, start)
   return str:sub(1, #start) == start
end

--- Remove duplicate links and return the singular ones
function p.main(frame)
    local input = frame.args[1] or ""

    local links = {}
    input:gsub("%[%[([^%]]*)%]%]", function(link)
        link = "[[" .. link .. "]]" -- :gsub removes the double brackets
        if frame.args["removeFiles"] == "yes" and not startsWith(string.lower(link), "file:") then 
        	table.insert(links, link)
        end
    end)

    local duplicateTracker = {}
    for _, link in pairs(links) do
        if not duplicateTracker[link] then
            duplicateTracker[link] = 0
        end
        duplicateTracker[link] = duplicateTracker[link] + 1
    end

    local output = ""
    for link, duplicates in pairs(duplicateTracker) do
        if duplicates == 1 then
            output = output .. link
        end
    end

    return output
end

return p