Module:Designation
Appearance
| This module is rated as beta. It is considered ready for widespread use, but as it is still relatively new, it should be applied with some caution to ensure results are as expected. |
| This module is currently protected from editing. See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected. |
This module has been developed to implement Template:Designation and all its sub-templates. Data is held in two subpages in JSON format:
- Module:Designation/list - list of designations with text/link, abbreviation and colours
- Module:Designation/lookup - list of recognised aliases for each designation
Usage
{{#invoke:Designation|text}}implements Template:Designation/text{{#invoke:Designation|color}}implements Template:Designation/colour{{#invoke:Designation|color2}}implements Template:Designation/colour2{{#invoke:Designation|abbr}}implements Template:Designation/abbreviation{{#invoke:Designation|divbox}}implements Template:Designation/divbox
require('strict')
local p = {}
local get_data = function(frame)
local args = frame:getParent().args
local former = false
if args.delisted=='yes' then
former = true
end
local par
if args and args[1] and args[1]~='' then
par = args[1]:lower()
if par:sub(1, 6)=='former' then
former = true
par = par:sub(7) -- strip off "former" from start
end
local list = mw.loadJsonData('Module:Designation/list')
if list[par] then -- lookup not needed
return list[par], former
else -- attempt lookup
local lookup = mw.loadJsonData('Module:Designation/lookup')
if lookup[par] then -- successful lookup
par = lookup[par]
return list[par], former
else
local category = mw.title.getCurrentTitle().namespace==0 and '[[Category:Articles using Template:Designation with invalid designation]]' or ''
return 'Invalid designation' .. category
end
end
else
return 'No parameter'
end
end
local text = function(frame, data, former)
local text = data.text
if data.lang then
text = frame:expandTemplate{title = 'lang', args = {
code = data.lang,
text = text
}}
end
return text and ((former and 'Former ' or '') .. text)
end
p.text = function(frame)
local data, former = get_data(frame)
if type(data)=='string' then
return data
end
return text(frame, data, former)
end
local colour = function(data, former, index)
local color
if former then
color = 'DDDDDD'
elseif data[index] then
color = data[index]
else
color = 'A8EDEF' -- default colour
end
return color and ('#' .. color)
end
p.color = function(frame)
local data, former = get_data(frame)
if type(data)=='string' then
return data
end
return mw.text.nowiki(colour(data, former, 'col'))
end
p.color2 = function(frame)
local data, former = get_data(frame)
if type(data)=='string' then
return data
end
return mw.text.nowiki(colour(data, former, 'col2'))
end
p.abbr = function(frame)
local data, former = get_data(frame)
if type(data)=='string' then
return data
end
return former and '???' or data.abbr or '???'
end
p.divbox = function(frame)
local data, former = get_data(frame)
local color = colour(data, former, 'col2')
local text = text(frame, data, former)
local div = mw.html.create('div')
:css('line-height', '1.5')
:css('text-align', 'center')
if type(data)=='string' then
local invalid = mw.html.create('span')
:css('color', 'red')
:wikitext('Error: ' .. data)
div:node(invalid)
else
div:css('border-style', 'solid')
:css('border-width', '4px')
:css('border-color', color)
:wikitext(text)
end
return tostring(div)
end
return p