Module:Political party/testtable: Difference between revisions
Appearance
Content deleted Content added
cleaner |
added color accessibility validation |
||
Line 1: | Line 1: | ||
local p = {} |
local p = {} |
||
local contrastRatio = require('Module:Color contrast')._ratio |
|||
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 "Failed" |
|||
end |
|||
end |
|||
local function isColorValid(color) |
local function isColorValid(color) |
||
if not color or color == "" then |
if not color or color == "" then |
||
return |
return '' |
||
end |
end |
||
Line 12: | Line 32: | ||
local HTMLcolor = mw.loadData('Module:Color contrast/colors') |
local HTMLcolor = mw.loadData('Module:Color contrast/colors') |
||
if HTMLcolor[color] then |
if HTMLcolor[color] then |
||
return |
return '' |
||
end |
end |
||
Line 20: | Line 40: | ||
local cs = mw.text.split(color, '') |
local cs = mw.text.split(color, '') |
||
if #cs == 6 or #cs == 3 then |
if #cs == 6 or #cs == 3 then |
||
return |
return '' |
||
end |
end |
||
return false |
|||
mw.log(color) |
|||
return "" -- TODO: return an error. |
|||
end |
end |
||
Line 39: | Line 58: | ||
local function table_row(party_name, party_info) |
local function table_row(party_name, party_info) |
||
local res = mw.html.create('') |
local res = mw.html.create('') |
||
res:tag(' |
res:tag('th') |
||
:attr('scope', 'row') |
|||
:wikitext(party_name) |
:wikitext(party_name) |
||
res:tag('td') |
res:tag('td') |
||
Line 48: | Line 68: | ||
res:tag('td') |
res:tag('td') |
||
:wikitext(party_info.shortname) |
:wikitext(party_info.shortname) |
||
res:tag('td') |
|||
⚫ | |||
res:tag('td') |
|||
:wikitext(isContrastValid("black", party_info.color)) |
|||
res:tag('td') |
|||
:wikitext(isContrastValid("#0645AD", party_info.color)) |
|||
res:tag('td') |
|||
:wikitext(isContrastValid("#0B0080", party_info.color)) |
|||
return tostring(res) |
return tostring(res) |
||
end |
end |
||
Line 53: | Line 82: | ||
-- build table |
-- build table |
||
local root = mw.html.create('table') |
local root = mw.html.create('table') |
||
root:addClass('wikitable |
root:addClass('wikitable') |
||
:addClass('sortable') |
|||
:addClass('plainrowheaders') |
|||
:css('background-color', 'transparent') |
:css('background-color', 'transparent') |
||
:css('font-size', '90%') |
:css('font-size', '90%') |
||
Line 60: | Line 91: | ||
local row = root:tag('tr') |
local row = root:tag('tr') |
||
row:tag('th') |
row:tag('th') |
||
:attr('scope', 'col') |
|||
:wikitext('Political party name') |
:wikitext('Political party name') |
||
row:tag('th') |
row:tag('th') |
||
:attr('scope', 'col') |
|||
:addClass('unsortable') |
:addClass('unsortable') |
||
:wikitext('color') |
:wikitext('color') |
||
row:tag('th') |
row:tag('th') |
||
:attr('scope', 'col') |
|||
:wikitext('abbrev') |
:wikitext('abbrev') |
||
row:tag('th') |
row:tag('th') |
||
:attr('scope', 'col') |
|||
:wikitext('shortname') |
:wikitext('shortname') |
||
row:tag('th') |
|||
:attr('scope', 'col') |
|||
:wikitext('Is color valid?') |
|||
row:tag('th') |
|||
:attr('scope', 'col') |
|||
:wikitext('Contrast normal text') |
|||
row:tag('th') |
|||
:attr('scope', 'col') |
|||
:wikitext('Contrast unvisted link') |
|||
row:tag('th') |
|||
:attr('scope', 'col') |
|||
:wikitext('Contrast visted link') |
|||
for party_name, party_vals in pairs(data.full) do |
for party_name, party_vals in pairs(data.full) do |
||
⚫ | |||
row = root:tag('tr') |
row = root:tag('tr') |
||
row:wikitext(table_row(party_name, party_vals)) |
row:wikitext(table_row(party_name, party_vals)) |
Revision as of 15:06, 19 November 2021
![]() | This module depends on the following other modules: |
local p = {}
local contrastRatio = require('Module:Color contrast')._ratio
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 "Failed"
end
end
local function isColorValid(color)
if not color or color == "" then
return ''
end
-- Convert to lowercase.
color = color:lower()
-- Check if color is using an HTML color name.
local HTMLcolor = mw.loadData('Module:Color contrast/colors')
if HTMLcolor[color] then
return ''
end
-- Remove leading # if there is one.
color = string.gsub(color, "^#", "")
local cs = mw.text.split(color, '')
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 = mw.loadData('Module:Political party/' .. 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.color)
:wikitext(party_info.color)
res:tag('td')
:wikitext(party_info.abbrev)
res:tag('td')
:wikitext(party_info.shortname)
res:tag('td')
:wikitext(tostring(isColorValid(party_info.color)))
res:tag('td')
:wikitext(isContrastValid("black", party_info.color))
res:tag('td')
:wikitext(isContrastValid("#0645AD", party_info.color))
res:tag('td')
:wikitext(isContrastValid("#0B0080", party_info.color))
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('Political party name')
row:tag('th')
:attr('scope', 'col')
:addClass('unsortable')
:wikitext('color')
row:tag('th')
:attr('scope', 'col')
:wikitext('abbrev')
row:tag('th')
:attr('scope', 'col')
:wikitext('shortname')
row:tag('th')
:attr('scope', 'col')
:wikitext('Is color valid?')
row:tag('th')
:attr('scope', 'col')
:wikitext('Contrast normal text')
row:tag('th')
:attr('scope', 'col')
:wikitext('Contrast unvisted link')
row:tag('th')
:attr('scope', 'col')
:wikitext('Contrast visted link')
for party_name, party_vals in pairs(data.full) do
row = root:tag('tr')
row:wikitext(table_row(party_name, party_vals))
end
return tostring(root)
end
return p