Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules


Logic for {{Bicolor}}.

Usage

{{#invoke:Bicolor|bicolor}}
local bicolor = require('Module:Bicolor')._bicolor


Code

--[[
Bicolor module to implement Template:Bicolor.

Colour definitions and existing combinations are in Module:Bicolor/data.

We can load the data efficiently with:

	local c = mw.loadData("Module:Bicolor/data")

Creating the definitions like this offers us tables indexed by number or by colour name,
and combinations as a sequence or an indexed set for efficient testing
--]]

require('strict')

local c = mw.loadData("Module:Bicolor/data")
local colors = c.colors
local colorvalues = c.colorvalues

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')

local p = {}

local function cat_text(args)
	local topic = args.topic or ''
	local color1topic = args.color1topic or ''
	local conj = args.conj or 'and'
	local color_prefix = args.color_prefix or ''
	if topic > '' then
		topic = ' ' .. topic
	end
	if color1topic > '' then
		color1topic = ' ' .. color1topic
	end
	if yesno(args.conj_leading_space or true) then
		conj = ' ' .. conj
	end
	if yesno(args.conj_trailing_space or true) then
		conj = conj .. ' '
	end
	if color_prefix > '' then
		color_prefix = color_prefix .. ' '
	end
	
	local out = {}
	-- outer loop
	for _, color1 in ipairs(colors) do
		local colorname1 = color1.name
		local colorvalue1 = color1.value
		local colortitle1 = color1.title
		local colorname1_caps = colorname1
		local color_prefix_caps = color_prefix
		if color_prefix == '' then
			colorname1_caps = colorname1:sub(1, 1):upper() .. colorname1:sub(2)
		else
			color_prefix_caps = color_prefix:sub(1, 1):upper() .. color_prefix:sub(2)
		end
		 -- inner loop
		for _, color2 in ipairs(colors) do
			local colorname2 = color2.name
			local colorvalue2 = color2.value
			local colortitle2 = color2.title
			local cattitle = mw.title.makeTitle('Category', table.concat({
				color_prefix_caps,
				colorname1_caps,
				color1topic,
				conj,
				colorname2,
				topic}))
			if cattitle and cattitle:getContent() then
				table.insert(out, table.concat({
					'[[:' .. cattitle.prefixedText .. '|',
					'<span class="bicbox" style="background:' .. colorvalue1 .. '" title="' .. colortitle1 .. '"></span>',
					'<span class="bicbox" style="background:' .. colorvalue2 .. '" title="' .. colortitle2 .. '"></span>',
					'&#8239;' .. colorname1 .. ', ' .. colorname2 .. ']]' -- NNBSP (U+202F) prefix
				}))
			end
		end -- inner loop
	end -- outer loop
	return table.concat(out, ' ')
end

function p._bicolor(args)
	args.topic = args.topic or args[1] or ''
	args.color1topic = args.color1topic or ''
	local cattext = args.cattext
	if not cattext and args.color1topic > '' then
		cattext = args.color1topic .. ' bicolor ' .. args.topic
	elseif not cattext then
		cattext = 'Bicolor ' .. args.topic
	end
	local display = {
		mw.getCurrentFrame():extensionTag('templatestyles', '', {src='Bicolor/styles.css'}),
		'<div class="catlinks bic-outer">',
		'<div class="bic-head">[[:Category:' .. cattext .. '|' .. cattext .. ']]</div>',
		'<div class="bic-inner">' .. cat_text(args) .. '</div>',
		'</div>'
	}
	return table.concat(display)
end

function p.bicolor(frame)
	return p._bicolor(getArgs(frame))
end

return p