Jump to content
Wikipedia The Free Encyclopedia

Module:Political party/testtable

From Wikipedia, the free encyclopedia
Module documentation[view] [edit] [history] [purge]
This module depends on the following other modules:
The above documentation is transcluded from Module:Political party/testtable/doc. (edit | history)
Editors can experiment in this module's sandbox (create | mirror) and testcases (create) pages.
Subpages of this module.

 localp={}

 localcontrastRatio=require('Module:Color contrast')._ratio

 function__genOrderedIndex(t)
 localorderedIndex={}
 forkeyinpairs(t)do
 table.insert(orderedIndex,key)
 end
 table.sort(orderedIndex)
 returnorderedIndex
 end

 functionorderedNext(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.

 localkey=nil
 ifstate==nilthen
 -- the first time, generate the index
 t.__orderedIndex=__genOrderedIndex(t)
 key=t.__orderedIndex[1]
 else
 -- fetch the next value
 fori=1,#(t.__orderedIndex)do
 ift.__orderedIndex[i]==statethen
 key=t.__orderedIndex[i+1]
 end
 end
 end

 ifkeythen
 returnkey,t[key]
 end

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

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

 localfunctionisContrastValid(text,background)
 ifnotbackgroundorbackground==""then
 return''
 end

 localratio=tonumber(contrastRatio({text,background}))
 ifnotratiothen
 return""
 end
 ifratio>4.5andratio<7.0then
 return"AA"
 elseifratio>7.0then
 return"AAA"
 else
 return"Failed"
 end
 end

 localfunctionisColorValid(color)
 ifnotcolororcolor==""then
 return''
 end

 -- Convert to lowercase.
 color=color:lower()

 -- Check if color is using an HTML color name.
 localHTMLcolor=mw.loadData('Module:Color contrast/colors')
 ifHTMLcolor[color]then
 return''
 end

 -- Added as a valid color in https://www.w3.org/TR/css-color-3/#transparent
 ifcolor=="transparent"then
 return""
 end

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

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

 returnfalse
 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.
 functionp.tables(frame)
 -- Initialise and populate variables
 localargs=frame.args
 localindex=args.letter

 -- Load data from submodule
 localdata=require('Module:Political party/'..index)

 -- helper function
 localfunctiontable_row(party_name,party_info)
 localres=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))

 returntostring(res)
 end

 -- build table
 localroot=mw.html.create('table')
 root:addClass('wikitable')
 :addClass('sortable')
 :addClass('plainrowheaders')
 :css('font-size','90%')
 :css('line-height','100%')
 :cssText(style)
 localrow=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 unvisited link')
 row:tag('th')
 :attr('scope','col')
 :wikitext('Contrast visited link')

 forparty_name,party_valsinorderedPairs(data.full)do
 row=root:tag('tr')
 row:wikitext(table_row(party_name,party_vals))
 end
 returntostring(root)

 end

 returnp

AltStyle によって変換されたページ (->オリジナル) /