Module:Shindo
Appearance
local p = {}
local getArgs = require("Module:Arguments").getArgs
local data = mw.loadData("Module:Shindo/data")
local messages = mw.loadData("Module:Shindo/messages")
local makeInvokeFunc = require("Module:MakeInvokeFunc")(p)
local message = require("Module:Message")(messages)
local yn = require("Module:Yesno")
local function getValueOfColor(tbl)
return (tbl[1] + tbl[2] + tbl[3]) / 3 / 255
end
local function rgbColor(tbl)
return tbl[1] .. (tbl[2] and ", " .. tbl[2] .. (tbl[3] and ", " .. tbl[3] or "") or "")
end
local function listIntensitiesFromScale(scale, intensities)
local out = ''
out = out .. data[scale].short .. ' '
for k,v in pairs(intensities) do
if data[scale].ranks[v] == nil then error(message("invalidIntensity", {v, scale})) end
out = out .. data[scale].ranks[v].label
if k ~= #intensities then
if #intensities == 2 then
out = out .. '–'
else
out = out .. '/'
end
end
end
return out
end
p._color = function(args)
local scale = string.lower(args.scale or args[1] or error(message("noScaleShortCode")))
local intensity = string.upper(args.intensity or args[2] or error(message("noIntensity")))
if data[scale] == nil then error(message("invalidScale", {scale})) end
if data[scale].ranks[intensity] == nil then error(message("invalidIntensity", {intensity, scale})) end
local order = data[scale].ranks[intensity].order
local color = data[scale].colors[order]
local colorIntensity = getValueOfColor(color)
return 'background-color:rgb(' .. rgbColor(data[scale].colors[order]) .. '); color:' .. (colorIntensity < 0.5 and "white" or "black") .. ";"
end
p._formatInWikitable = function(args)
local scale = string.lower(args.scale or args[1] or error(message("noScaleShortCode")))
local link = args.link ~= nil and args.link or true
local doColor = args.color ~= nil and args.color or true
local intensity = string.upper(args.intensity or args[2] or error(message("noIntensity")))
if data[scale] == nil then error(message("invalidScale", {scale})) end
if data[scale].ranks[intensity] == nil then error(message("invalidIntensity", {intensity, scale})) end
local order = data[scale].ranks[intensity].order
local color = data[scale].colors[order]
local colorIntensity = getValueOfColor(color)
local out = ""
if yn(args.header or false) then
out = out .. "! "
else
out = out .. "| "
end
out = out .. 'style="background-color:rgba(' .. rgbColor(color) .. '); color:' .. (colorIntensity < 0.5 and "white" or "black") .. ';' .. (args.style or "") .. ';" | '
if yn(link) then
out = out .. '[[' .. data[scale].name .. "#" .. data[scale].id_prefix .. data[scale].ranks[intensity].id .. "|"
end
out = out .. listIntensitiesFromScale(scale, { intensity })
if yn(link) then
out = out .. "]]"
end
return out
end
p._formatTag = function(args)
local scale = string.lower(args.scale or args[1] or error(message("noScaleShortCode")))
local scale2 = args.scale2 or args[3] or nil
if scale2 ~= nil then scale2 = string.lower(scale2) end
local link = args.link ~= nil and args.link or true
local doColor = args.color ~= nil and args.color or true
local intensity = string.upper(args.intensity or args[2] or error(message("noIntensity")))
local intensities = mw.text.split(intensity, "/") or { intensity }
local intensity2 = args.intensity2 or args[4] or (scale2 and error(message("noIntensity"))) or nil
if intensity2 ~= nil then intensity2 = string.upper(intensity2) end
local intensities2 = mw.text.split(intensity, "/") or { intensity2 }
if data[scale] == nil then error(message("invalidScale", {scale})) end
if scale2 and data[scale2] == nil then error(message("invalidScale", {scale2})) end
if intensity2 and data[scale2].ranks[intensity2] == nil then error(message("invalidIntensity", {intensity2, scale2})) end
local order = data[scale].ranks[intensities[1]].order
local order2 = (scale2 ~= nil and intensity2 ~= nil) and data[scale2].ranks[intensities2[1]].order or nil
local color = data[scale].colors[order]
local colorIntensity = getValueOfColor(color)
local out = ''
if yn(doColor) then
out = out .. '<' .. (args.tag or "div") .. ' style="background-color:rgba(' .. rgbColor(color) .. '); padding:4px; color:' .. (colorIntensity < 0.5 and "white" or "black") .. '; '
elseif args.style then
out = out .. '<' .. (args.tag or "div") .. ' style="'
end
if args.style then
out = out .. args.style
end
out = out .. '">'
if yn(link) then
out = out .. '[[' .. data[scale].name .. "#" .. data[scale].id_prefix .. data[scale].ranks[intensities[1]].id .. "|"
end
if yn(doColor) then
out = out .. '<span style=\"color:' .. (colorIntensity < 0.5 and "white" or "black") .. ';">'
end
out = out .. listIntensitiesFromScale(scale, intensities) .. ((data[scale].ranks[intensities[1]].category and not scale2) and " (''" .. data[scale].ranks[intensities[1]].category .. "'')" or "")
if yn(doColor) then
out = out .. '</span>'
end
if yn(link) then
out = out .. "]]"
end
if (scale2) then
out = out .. " ("
if yn(link) then
out = out .. '[[' .. data[scale2].name .. "#" .. data[scale2].id_prefix .. data[scale2].ranks[intensity2].id .. "|"
end
if yn(doColor) then
out = out .. '<span style=\"color:' .. (colorIntensity < 0.5 and "white" or "black") .. ';">'
end
out = out .. listIntensitiesFromScale(scale, intensities)
if yn(doColor) then
out = out .. '</span>'
end
if yn(link) then
out = out .. "]]"
end
out = out .. ")"
end
if yn(doColor) then
out = out .. '</' .. (args.tag or "div") .. '>'
end
return mw.getCurrentFrame():preprocess(out)
end
p._format = function(args)
if args["format"] == "wikitable" then
return p.formatInWikitable(args)
else
return p.formatTag(args)
end
end
-- make each scale invokable
for k,v in pairs(data) do
if p["_" .. k] ~= nil then error(message("scaleNameInvalid", k)) end
p["_" .. k] = function(args)
args["scale"] = k
args["intensity"] = args["intensity"] or args[1] or nil
args["scale2"] = args["scale2"] or args[2] or nil
args["intensity2"] = args["intensity2"] or args[3] or nil
if args["format"] == "wikitable" then
return p.formatInWikitable(args)
else
return p.formatTag(args)
end
end
end
-- make all functions that begin with _ invokable
local q = mw.clone(p)
for k,v in pairs(q) do
if mw.ustring.sub(k, 1, 1) == "_" then
mw.log(k)
p[mw.ustring.sub(k, 2, #k)] = makeInvokeFunc(k)
end
end
return p