Module:Trailblazer Region

From Old School RuneScape Wiki
Jump to navigation Jump to search
local p = {}
local lang = mw.getContentLanguage()

local regions = {
	misthalin = true,
	karamja = true,
	asgarnia = true,
	desert = true,
	fremennik = true,
	kandarin = true,
	morytania = true,
	tirannwn = true,
	wilderness = true,
}

function p.main(frame)
	local args = frame:getParent().args
	local region = args[1]
	local note = args.note
	return p._main(region, note)
end

function p._main(region, note)
	if region == nil or string.lower(region) == "no" then
		return "None"
	end
	local ret = {}
	for value in string.gmatch(region, "[^,]+") do
		local lower = string.lower(value)
		local trimmed = lower:gsub("^%s*(.-)%s*$", "%1")
		if regions[trimmed] then
			local uc_region = lang:ucfirst(trimmed)
			local icon = p._icon(trimmed, note)
			local cur_ret = highlight(string.format('%s [[Trailblazer League/Areas/%s|%s]]', icon, uc_region, uc_region), uc_region)
			table.insert(ret, cur_ret)
		else
			table.insert(ret, value)
		end
	end
	return table.concat(ret, '')
end

function highlight(s, r)
	r = string.lower(r)
	return string.format('<span class="tbz-region" data-tbz-area="%s">%s<span class="tbz-check">✓</span></span>', r, s)
end

function p._icon(region, note)
	if string.lower(region or '') == "no" then
		return ""
	else
		region = lang:ucfirst(region)
	end
	if note == nil then
		note = region
	end
	return string.format('[[File:%s Area Badge.png|%s|link=Trailblazer League/Areas/%s]]', region, note, region)
end

function p._badge(region, note)
	if region == nil or string.lower(region) == "no" then
		return ""
	end
	local ret = {}
	for value in string.gmatch(region, "[^,]+") do
		local lower = string.lower(value)
		local trimmed = lower:gsub("^%s*(.-)%s*$", "%1")
		if regions[trimmed] then
			local icon = p._icon(trimmed, note)
			local cur_ret = string.format('<span class="tbz-badge" data-tbz-area="%s">%s</span>', trimmed, icon)
			table.insert(ret, cur_ret)
		else
			table.insert(ret, value)
		end
	end
	return table.concat(ret, '')
end

function p.highlightedIcon(frame)
	local args = frame:getParent().args
	local region = args[1]
	local note = args.note
	return highlight(p._icon(region, note), region)
end

function p.icon(frame)
	local args = frame:getParent().args
	local region = args[1]
	local note = args.note
	return p._icon(region, note)
end

function p.badge(frame)
	local args = frame:getParent().args
	local region = args[1]
	local note = args.note
	return p._badge(region, note)
end

return p