Edit the documentation or categories for this module.
-- <nowiki>
local util_args = require('Module:ArgsUtil')
local util_html = require("Module:HtmlUtil")
local util_map = require('Module:MapUtil')
local util_table = require("Module:TableUtil")
local util_text = require("Module:TextUtil")
local util_title = require("Module:TitleUtil")
local util_vars = require("Module:VarsUtil")
local i18n = require("Module:I18nUtil")
local lang = mw.getLanguage('en')
local Sprite = require('Module:Sprite').spriteImage
local LCS = require('Module:LuaClassSystem')
local Entity = LCS.class.abstract()
Entity.imagesizes = {
default = 25
}
-- the following are mandatory params when subclassed
Entity.objectType = nil
Entity.defaultlength = 'long'
Entity.imagelength = 'link'
Entity.imagesizes = { default = 25 } -- should never be modified only overwritten so it's ok to be outside of init
-- the following are all optional params to be defined when subclassed
Entity.cssClass = nil
Entity.defaultLink = nil
Entity.filelength = nil
Entity.filePattern = nil
Entity.imageDisplayLength = nil
Entity.spriteType = nil -- language needs to be a country
function Entity:init(str)
if not str then
self.is_nil = true
return
end
local lookup = mw.loadData('Module:' .. self.objectType .. 'names')
local vars = lookup[str and tostring(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 = util_table.shallowClone(vars)
end
function Entity:get(len)
if self.is_nil then return nil end
return self.vars[len or self.defaultlength]
end
function Entity:getRaw(len)
if self.is_nil then return nil end
return self.vars[len]
end
function Entity:exists()
return not self.is_nil
end
function Entity:isknown()
if self.is_nil then return nil end
return not self.unknown
end
function Entity:__tostring()
if self.is_nil then return nil end
if self.vars then
return self:get(self.defaultlength)
end
return 'Attempting to tostring something we cannot'
end
function Entity:name(opts)
if not opts then opts = {} end
return self:get(opts.len or self.defaultlength)
end
function Entity:flair(opts)
if self.is_nil then return nil end
local span = mw.html.create('span')
:addClass('markup-object')
:addClass(self.cssClass)
span:wikitext(self:image(opts))
span:tag('span')
:wikitext(self:name(opts))
:addClass("markup-object-name")
return tostring(span)
end
function Entity:flairlink(opts)
if self.is_nil then return nil end
if not opts then opts = {} end
local span = mw.html.create('span')
:addClass('markup-object')
:addClass(self.cssClass)
span:wikitext(self:image(opts))
:wikitext(self:name(opts))
return self:_link(tostring(span), opts)
end
function Entity:image(opts)
if self.is_nil then return nil end
if not opts then opts = {} end
return Sprite{
self:getSpriteKey(opts),
type = self.spriteType or self.objectType,
-- size is needed to calculate position even if nosize is specified
size = opts.size or self.imagesizes[opts.len or 'default'] or self.imagesizes.default,
nosize = opts.nosize or self.nosize,
class = self:getImageClass(opts),
skiplookup = true,
display = self.vars[self.imageDisplayLength or self.imagelength],
fallback = self:getSpriteFallback(opts),
}
end
function Entity:file(opts)
if self.is_nil then return end
if not opts then opts = {} end
return self:_file(opts, '')
end
function Entity:filelink(opts)
if self.is_nil then return end
if not opts then opts = {} end
return self:_file(opts, opts.link or self:get(self.defaultLink or self.imagelength))
end
function Entity:_file(opts, link, file)
return ('[[File:%s|link=%s|%spx]]'):format(
file or self.filePattern:format(self:get(self.filelength or self.imagelength)),
link,
opts.size or self.imagesizes[opts.len or 'default'] or self.imagesizes.default
)
end
function Entity:getSpriteFallback(opts)
return nil
end
function Entity:getImageClass(opts)
return nil
end
function Entity:imagelink(opts)
if not opts then opts = {} end
return self:_link(self:image(opts), opts)
end
function Entity:link(opts)
if not opts then opts = {} end
return self:_link(nil, opts)
end
function Entity:_link(display, opts)
local link = self:_getLinkTarget(link, opts)
display = display or opts.display or self:name(opts)
return util_text.intLink(link, display)
end
function Entity:_getLinkTarget(link, opts)
if link then return link end
if opts.link then return opts.link end
return util_title.concatSubpage(
self:get(self.defaultLink or self.defaultlength),
opts.suffix
)
end
function Entity:_or(entity)
if self.is_nil then
return entity
end
return self
end
function Entity:getSpriteKey(opts)
-- this function is needed so that we can overwrite it on other wikis
-- for example the COD wiki uses it for its substitute handling
return self.vars[self.imagelength]
end
return Entity
-- </nowiki>