Module:PortableNavbox
Documentation for this module may be created at Module:PortableNavbox/doc
-- <nowiki>
local p = {}
local yesno = require('Module:Yesno')
local title = mw.title.getCurrentTitle()
function tohex(color)
color = string.lower(color)
color = string.gsub(color, '^%#', '')
local hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
local red, green, blue
if string.find(color, 'rgb') then
red, green, blue = string.match(color, '%(%s*(%d+)%s*%,%s*(%d+)%s*%,%s*(%d+)')
elseif string.find(color, 'hsl') then -- https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB
local hue, sat, light = string.match(color, '%(%s*(%d+)[%s%,]+(%d+)%%[%s%,]+(%d+)%%')
hue = hue/60
sat = sat/100
light = light/100
local chroma = (1 - math.abs(2*light - 1))*sat
local x = chroma*(1 - math.abs(math.fmod(hue,2) - 1))
local r1, g1, b1
if hue <1 then
r1 = chroma
g1 = x
b1 = 0
elseif hue <2 then
r1 = x
g1 = chroma
b1 = 0
elseif hue <3 then
r1 = 0
g1 = chroma
b1 = x
elseif hue <4 then
r1 = 0
g1 = x
b1 = chroma
elseif hue <5 then
r1 = x
g1 = 0
b1 = chroma
else
r1 = chroma
g1 = 0
b1 = x
end
local m = light - chroma/2
red = math.floor((r1 + m)*255 +0.5)
green = math.floor((g1 + m)*255 +0.5)
blue = math.floor((b1 + m)*255 +0.5)
end
if red then
red = red/16
green = green/16
blue = blue/16
return hex[math.floor(red) +1] .. hex[math.floor(math.fmod(red,1)*16) +1] .. hex[math.floor(green) +1] .. hex[math.floor(math.fmod(green,1)*16) +1] .. hex[math.floor(blue) +1] .. hex[math.floor(math.fmod(blue,1)*16) +1]
end
if string.len(color) == 4 then
color = string.sub(color,1,3)
elseif string.len(color) == 8 then
color = string.sub(color,1,6)
end
return color
end
function p.main(frame)
local args = frame:getParent().args
local contents = {
'<infobox theme="',
args.theme or 'navbox',
'"'
}
if args.type then
table.insert(contents, ' type="' .. args.type .. '"')
end
if args['header-background'] then
table.insert(contents, ' accent-color-default="' .. tohex(args['header-background']) .. '"')
end
if args['header-text'] then
table.insert(contents, ' accent-color-text-default="' .. tohex(args['header-text']) .. '"')
end
if yesno(args.stacked, false) then
table.insert(contents, ' layout="stacked"')
end
table.insert(contents, '><group')
if args.collapse ~= nil then
if yesno(args.collapse, true) then --Collapse true: close
table.insert(contents, ' collapse="closed"')
else --Collapse false: open
table.insert(contents, ' collapse="open"')
end
end
table.insert(contents, '>')
if args[1] then
table.insert(contents, '<header>')
table.insert(contents, mw.text.trim(args[1]))
table.insert(contents, '</header>')
end
for k, v in ipairs(args) do
local i = tonumber(k)
if i and v and i > 1 then
i = i - 2
if i % 2 == 0 then
table.insert(contents, '<data><label>')
table.insert(contents, mw.text.trim(v))
table.insert(contents, '</label>')
else
local r = mw.ustring.gsub(mw.text.trim(v), table.concat({' ', args.separator or '!', ' '}), ' • ')
table.insert(contents, '<default>')
table.insert(contents, mw.text.trim(r))
table.insert(contents, '</default></data>')
end
end
end
table.insert(contents, '</group></infobox>')
contents = table.concat(contents)
if
title.namespace ~= 829 or
title.subpageText ~= 'testcases'
then
contents = frame:preprocess(contents)
end
return contents
end
return p