Modul:Vorlage:Standardfarbe
Erscheinungsbild
Vorlagenprogrammierung | Diskussionen | Lua | Unterseiten | |||
Modul | Deutsch | English
|
Modul: | Dokumentation |
Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus
-- module table
local Standardfarbe = {
config = {
colorJSON = "Vorlage:Standardfarbe/colors.json",
errorOutput = "<span class=\"error\">Fehler in [[Vorlage:Standardfarbe]]: %s</span>"
},
colors = {}
}
-- export table
local p = {}
Standardfarbe.formatError = function ( message )
-- format error message
-- parameters:
-- message: (string) raw error message
-- returns:
-- (string) formatted error message
return string.format( Standardfarbe.config.errorOutput, message )
end
Standardfarbe.iterateColorGroup = function ( group, key )
-- return color value
-- parameters:
-- group: (table) color group
-- key: (string) key to find
-- returns:
-- (bool, table) color block found, color block table
for _, block in ipairs( group ) do
for _, k in ipairs( block.keys ) do
if k == key then
return true, block.colors
end
end
end
return false, {}
end
Standardfarbe.loadColorsJson = function ( page )
-- load colors from json page
-- parameters:
-- page: (string) path to color json page
-- returns:
-- (bool) true or false by success/fault
local success, c = pcall( mw.loadJsonData, page )
if type( c ) == "table" then
Standardfarbe.colors = c
end
return success
end
Standardfarbe.getColor = function ( group, key, mode, ucase )
-- return color value
-- parameters:
-- group: (string) color group
-- key: (string) color key
-- mode: (string) display mode
-- ucase: (bool) uppercase color codes
-- returns:
-- (string) hex color code
local blockColors -- colors in block
local groupColors -- colors in group
local found -- color block found
local color -- color value
-- load defined colors
if Standardfarbe.loadColorsJson( Standardfarbe.config.colorJSON ) == false then
return ""
end
-- find color group
groupColors = Standardfarbe.colors[group]
if type( groupColors ) ~= "table" then
return Standardfarbe.formatError( "color group not found" )
end
-- find color block with key
found, blockColors = Standardfarbe.iterateColorGroup( groupColors, key )
if found == false then
return Standardfarbe.formatError( "color key not found" )
end
-- find color in block
if mode == "" then
mode = "light"
end
color = blockColors[mode]
if color == nil then
color = blockColors["light"]
if color == nil then
return Standardfarbe.formatError( "no matching color mode" )
end
end
-- uppercase, if wanted
if ucase then
color = string.upper( color )
end
-- return color value
return color
end
p.f = function ( frame )
-- return color value, access from templates
-- parameters:
-- frame: (table) wiki environment frame
-- returns:
-- (string) hex color code
local args -- arguments of template call
local group -- color group
local key -- color key
local mode -- display mode
local ucase -- uppercase color values
local nowiki -- enclose in nowiki tags
-- get template parameters
args = frame:getParent().args
group = args[1] or ""
key = args[2] or ""
mode = args["mode"] or "light"
ucase = ( args["ucase"] == "1" )
nowiki = ( args["nowiki"] == "1" )
-- call main and return
local success, r = pcall( Standardfarbe.getColor, group, key, mode, ucase )
if not success then
return Standardfarbe.formatError( "color call failed" )
end
-- apply nowiki
if nowiki then
r = frame:extensionTag( "nowiki", r )
end
-- return
return r
end
p.Standardfarbe = function ()
-- return module, access from other modules
return Standardfarbe
end
return p