İçeriğe atla

Modül:Siyasi parti/testtablosu

Vikipedi, özgür ansiklopedi
Modül belgelemesi[oluştur]
local p = {}

local contrastRatio = require('Modül:Renk karşıtlığı')._ratio

function __genOrderedIndex(t)
    local orderedIndex = {}
    for key in pairs(t) do
        table.insert(orderedIndex, key)
    end
    table.sort(orderedIndex)
    return orderedIndex
end

function orderedNext(t, state)
    -- Equivalent of the next function, but returns the keys in the alphabetic
    -- order. We use a temporary ordered key table that is stored in the
    -- table being iterated.

    local key = nil
    if state == nil then
        -- the first time, generate the index
        t.__orderedIndex = __genOrderedIndex(t)
        key = t.__orderedIndex[1]
    else
        -- fetch the next value
        for i = 1, #(t.__orderedIndex) do
            if t.__orderedIndex[i] == state then
                key = t.__orderedIndex[i + 1]
            end
        end
    end

    if key then
        return key, t[key]
    end

    -- no more value to return, cleanup
    t.__orderedIndex = nil
    return
end

function orderedPairs(t)
    -- Equivalent of the pairs() function on tables. Allows to iterate in order.
    return orderedNext, t, nil
end

local function isContrastValid(text, background)
	if not background or background == "" then
		return ''
	end

	local ratio = tonumber(contrastRatio({text, background}))
	if not ratio then
		return ""
	end
	if ratio > 4.5 and ratio < 7.0 then
		return "AA"
	elseif ratio > 7.0 then
		return "AAA"
	else
		return "Başarısız"
	end
end

local function isColorValid(renk)
	if not renk or renk == "" then
		return ''
	end

	-- Convert to lowercase.
	renk = renk:lower()
	
	-- Check if color is using an HTML color name.
	local HTMLcolor = mw.loadData('Modül:Renk karşıtlığı/renkler')
	if HTMLcolor[renk] then
		return ''
	end
	
	-- Added as a valid color in https://www.w3.org/TR/css-color-3/#transparent
	if renk == "transparent" then
		return ""
	end

	-- Remove leading # if there is one.
	renk = string.gsub(renk, "^#", "")

	local cs = mw.text.split(renk, '')
	if #cs == 6 or #cs == 3 then
		return ''
	end

	return false
end

-- Example of having all the data - color and names - in one table. Requires one page to be edited instead of two when adding a new party.
function p.tables(frame)
	-- Initialise and populate variables
	local args = frame.args
	local index = args.letter
	
	-- Load data from submodule
	local data = require('Modül:Siyasi parti/' .. index)
	
	-- helper function
	local function table_row(party_name, party_info)
		local res = mw.html.create('')
		res:tag('th')
			:attr('scope', 'row')
			:wikitext(party_name)
		res:tag('td')
			:css('background-color', party_info.renk)
			:wikitext(party_info.renk)
		res:tag('td')
			:wikitext(party_info.kisaltma)
		res:tag('td')
			:wikitext(party_info.kisaad)
		res:tag('td')
			:wikitext(tostring(isColorValid(party_info.renk)))
		res:tag('td')
			:wikitext(isContrastValid("black", party_info.renk))
		res:tag('td')
			:wikitext(isContrastValid("#0645AD", party_info.renk))
		res:tag('td')
			:wikitext(isContrastValid("#0B0080", party_info.renk))

		return tostring(res)
	end

	-- build table
	local root = mw.html.create('table')
	root:addClass('wikitable')
		:addClass('sortable')
		:addClass('plainrowheaders')
		:css('background-color', 'transparent')
		:css('font-size', '90%')
		:css('line-height', '100%')
		:cssText(style)
	local row = root:tag('tr')
	row:tag('th')
		:attr('scope', 'col')
		:wikitext('Siyasi parti adı')
	row:tag('th')
		:attr('scope', 'col')
		:addClass('unsortable')
		:wikitext('renk')
	row:tag('th')
		:attr('scope', 'col')
		:wikitext('kısaltma')
	row:tag('th')
		:attr('scope', 'col')
		:wikitext('kısa ad')
	row:tag('th')
		:attr('scope', 'col')
		:wikitext('Renk geçerli mi?')
	row:tag('th')
		:attr('scope', 'col')
		:wikitext('Normal metin kontrastı')
	row:tag('th')
		:attr('scope', 'col')
		:wikitext('Kontrast ziyaret edilmemiş bağlantı')
	row:tag('th')
		:attr('scope', 'col')
		:wikitext('Kontrast ziyaret edilen bağlantı')

	for party_name, party_vals in orderedPairs(data.full) do
		row = root:tag('tr')
		row:wikitext(table_row(party_name, party_vals))
	end
	return tostring(root)

end

return p