Module:Table

From the Super Mario Wiki, the Mario encyclopedia
Jump to navigationJump to search

Documentation for this module may be created at Module:Table/doc

local p = {}

function p.trim(str)
    return string.match(str, "^%s*(.-)%s*$")
end

function p.noimage(str)
    return "{{no image|name=" .. string.gsub(string.gsub(str, "[-_ ]+", " "), "[^A-Za-z0-9 ]+", "") .. ".png}}"
end

p.LinkTracker = {}
p.LinkTracker.__index = p.LinkTracker

function p.LinkTracker.new(current_title)
    local self = setmetatable({}, p.LinkTracker)

    self.used_links = {}
    self.used_links[current_title] = true

    return self
end

function p.LinkTracker:parse(str)
	return string.gsub(str or "", "%[%[(.-)%]%]", function(inner)
		local pipe = string.find(inner, "|", 1, true)
		local target = pipe and string.sub(inner, 1, pipe - 1) or inner
		local display_text = pipe and string.sub(inner, pipe + 1) or target
		local norm = string.lower(target)

		if self.used_links[norm] then
			return display_text
		else
			self.used_links[norm] = true
			return "[[" .. inner .. "]]"
		end
	end)
end

return p