Module:MarkupObjectClass

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, opts)) 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, opts)) :wikitext(self:key(length)) return self:link(tostring(span), opts) end

function MarkupObject:image(length, opts) if not opts then opts = {} end local size = opts.size or length or self.defaultlength return Sprite{ self.vars.link, type = self.objectType, size = self.imagesizes[size] or size } end

function MarkupObject:imagelinked(length, opts) return self:link(self:image(length, opts), 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