Module:2-ary truth table: Difference between revisions
Appearance
Content deleted Content added
No edit summary |
add some comments |
||
Line 3: | Line 3: | ||
function p.main(frame) |
function p.main(frame) |
||
local args = require('Module:Arguments').getArgs(frame) |
local args = require('Module:Arguments').getArgs(frame) |
||
-- table_headings includes the headings and the class for the heading |
|||
-- fill in the table |
|||
local table_headings = { |
local table_headings = { |
||
{frame:extensionTag("math",args['A'] or 'A',{}), ''}, |
{frame:extensionTag("math",args['A'] or 'A',{}), ''}, |
||
{frame:extensionTag("math",args['B'] or 'B',{}), ''} |
{frame:extensionTag("math",args['B'] or 'B',{}), ''} |
||
} |
} |
||
-- truth_table is the transpose of the rendered table |
|||
local truth_table = { |
local truth_table = { |
||
{false, false, true, true}, |
{false, false, true, true}, |
||
{false, true, false, true} |
{false, true, false, true} |
||
} |
} |
||
-- thick_border marks which columns have a thick left border |
|||
local thick_border = {false, false, true} |
local thick_border = {false, false, true} |
||
-- pre-process the inputs, saving data in the tables defined above |
|||
local col = 3 |
local col = 3 |
||
while args[6*(col-2) - 1] do |
while args[6*(col-2) - 1] do |
||
Line 26: | Line 30: | ||
local total_cols = col - 1 |
local total_cols = col - 1 |
||
-- start table |
-- start building the html table |
||
root = mw.html.create('table') |
root = mw.html.create('table') |
||
:addClass('wikitable') |
:addClass('wikitable') |
||
Line 55: | Line 59: | ||
end |
end |
||
-- get the templatestyles |
|||
local templatestyles = frame:extensionTag{ |
local templatestyles = frame:extensionTag{ |
||
name = 'templatestyles', args = { src = 'Template:2-ary truth table/style.css' } |
name = 'templatestyles', args = { src = 'Template:2-ary truth table/style.css' } |
||
} |
} |
||
-- return the templatestyles plus the html table |
|||
return templatestyles .. tostring(root) |
return templatestyles .. tostring(root) |
||
end |
end |
Revision as of 16:10, 13 March 2024
![]() | This module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected. |
![]() | This module depends on the following other modules: |
![]() | This module uses TemplateStyles: |
This module implements {{2-ary truth table}}
Usage
{{#invoke:2-ary truth table|main}}
local p = {}
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame)
-- table_headings includes the headings and the class for the heading
local table_headings = {
{frame:extensionTag("math",args['A'] or 'A',{}), ''},
{frame:extensionTag("math",args['B'] or 'B',{}), ''}
}
-- truth_table is the transpose of the rendered table
local truth_table = {
{false, false, true, true},
{false, true, false, true}
}
-- thick_border marks which columns have a thick left border
local thick_border = {false, false, true}
-- pre-process the inputs, saving data in the tables defined above
local col = 3
while args[6*(col-2) - 1] do
thick_border[col + 1] = string.lower(args[6*(col-2)] or '') == 'thick'
table_headings[col] = {args[6*(col-2) - 1], 'unsortable'}
truth_table[col] = {}
for i = 1,4 do
truth_table[col][i] = (tonumber(args[6*(col-3) + i]) or 0) == 1
end
col = col + 1
end
local total_cols = col - 1
-- start building the html table
root = mw.html.create('table')
:addClass('wikitable')
:addClass('sortable')
:addClass('two-ary-tt')
-- add headings
local row = root:tag('tr')
for col = 1,total_cols do
local headings = table_headings[col]
row:tag('th')
:addClass(headings[2] ~= '' and headings[2] or nil)
:addClass(thick_border[col] and 'border' or nil)
:wikitext(headings[1])
end
-- add rows
for i = 1,4 do
row = root:tag('tr')
for j = 1,total_cols do
local val = truth_table[j][i]
row:tag('td')
:addClass((j < 3) and 'bold' or nil)
:addClass(thick_border[j] and 'border' or nil)
:addClass(val and 't' or 'f')
:wikitext(val and 'T' or 'F')
end
end
-- get the templatestyles
local templatestyles = frame:extensionTag{
name = 'templatestyles', args = { src = 'Template:2-ary truth table/style.css' }
}
-- return the templatestyles plus the html table
return templatestyles .. tostring(root)
end
return p