Module:MarkupObjectClass

From Halo Esports Wiki
Jump to: navigation, search

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

local util_args = require('Module:ArgsUtil')
local util_text = require('Module:TextUtil')
local util_title = require('Module:TitleUtil')
local Class = require('Module:Class').Class
local Sprite = require('Module:Sprite').spriteImage

local MarkupObject = Class()

function MarkupObject:new(str, objectType)
	local lookup = mw.loadData('Module:' .. objectType .. 'names')
	local vars = lookup[str and str:lower() or 'DEFAULT']
	if type(vars) == 'string' then
		vars = lookup[vars]
	end
	if not vars then
		vars = lookup.DEFAULT or {}
		self.unknown = true
	end
	self.vars = {}
	for k, v in pairs(vars) do
		self.vars[k] = v
	end
	self.objectType = objectType
	self.defaultlength = 'long'
	self.imagesizes = {}
end

function MarkupObject:key(length, opts)
	return self.vars[length or 'link']
end

function MarkupObject:name(length, opts)
	return self.vars[length or self.defaultlength]
end

function MarkupObject:linked(length, opts)
	return self:link(self.vars[length or self.defaultlength], opts)
end

function MarkupObject:flair(length, opts)
	local span = mw.html.create('span')
		:addClass('markup-object')
	span:wikitext(self:image(length))
	span:tag('span')
		:wikitext(self:name(length))
	return tostring(span)
end

function MarkupObject:flairlinked(length, opts)
	local span = mw.html.create('span')
		:addClass('markup-object')
	span:wikitext(self:image(length or self.defaultlength))
		:wikitext(self:key(length))
	return self:link(tostring(span), opts)
end

function MarkupObject:image(length)
	return Sprite{
		self.vars.link,
		type = self.objectType,
		size = length and self.imagesizes[length] or length
	}
end

function MarkupObject:imagelinked(length, opts)
	return self:link(self:image(length), opts)
end

function MarkupObject:destination(opts)
	if opts.link then
		return opts.link
	elseif opts.suffix then
		return util_title.concatSubpage(self.vars.link, opts.suffix)
	else
		return self.vars.link
	end
end

function MarkupObject:link(str, opts)
	local link = not self.vars.exception and self:destination(opts or {})
	return util_text.intLinkOrText(link, str)
end

return MarkupObject