Jump to content

Module:Tree chart/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
use require('strict') instead of require('Module:No globals')
No edit summary
Line 1: Line 1:
--<nowiki>
require('strict')

local p = {}
local p = {}


local cells = mw.loadData('Module:TreeChart/data/sandbox')
local cells = mw.loadData('Module:Tree chart/data/sandbox')


function p._main(cell_args)
function p._main(cell_args)
local ret = mw.html.create()
local ret = mw.html.create()
local top = ret:tag('tr')
local top = ret:tag('tr')
:css{ height = '1px',
:css{ height = '1px',
['text-align'] = 'center' }
['text-align'] = 'center' }
local bottom = ret:tag('tr')
local bottom = ret:tag('tr')
:css{ height = '1px',
:css{ height = '1px',
['text-align'] = 'center' }
['text-align'] = 'center' }
for _, v in ipairs(cell_args) do
for _, v in ipairs(cell_args) do
if type(v) == 'string' then
if type(v) == 'string' then
top:wikitext(cells[v].t)
top:wikitext(cells[v].t)
bottom:wikitext(cells[v].b)
bottom:wikitext(cells[v].b)
else
else
top:tag('td')
top:tag('td')
:attr{ colspan = v.colspan or cell_args.colspan or 6,
:attr{ colspan = v.colspan or cell_args.colspan or 6,
rowspan = v.rowspan or cell_args.rowspan or 2 }
rowspan = v.rowspan or cell_args.rowspan or 2,
class = 'ft-colored-box' }
:css{ padding = '0.2em',
:css{ padding = '0.2em',
border = (v.border or cell_args.border or '2') .. 'px solid black' }
:cssText(v.boxstyle or cell_args.boxstyle)
border = (v.border or cell_args.border or '2') .. 'px solid' }
:cssText(v.boxstyle or cell_args.boxstyle)
:wikitext(v.text)
:wikitext(v.text)
end
end
end
end
return tostring(ret)
return tostring(ret)
end
end


function p.main(frame)
function p.main(frame)
local args = frame:getParent().args
local args = require('Module:Arguments').getArgs(frame, {wrappers = 'Template:Chart', trim = false, removeBlanks = false})
local cell_args = {
local cell_args = {
colspan = args.colspan,
colspan = args.colspan,
rowspan = args.rowspan,
rowspan = args.rowspan,
border = args.border,
border = args.border,
boxstyle = args.boxstyle
boxstyle = args.boxstyle
}
}
for _, val in ipairs(args) do
for _, val in ipairs(args) do
local trimmedVal = val:match('^%s*(.-)%s*$')
local trimmedVal = val:match('^%s*(.-)%s*$')
if trimmedVal == '' then
if trimmedVal == '' then
trimmedVal = '$'
trimmedVal = '$'
end
end
if cells[trimmedVal] then
if cells[trimmedVal] then
table.insert(cell_args, trimmedVal)
table.insert(cell_args, trimmedVal)
else
else
-- Unnamed params behave weirdly
-- Unnamed params behave weirdly
-- white space at the front counts for param_{{{1}}}, but not whitespace at the end, so remove it
-- white space at the front counts for param_{{{1}}}, but not whitespace at the end, so remove it
local rightTrimmedVal = val:gsub('%s+$','')
local rightTrimmedVal = val:gsub('%s+$','')
table.insert(cell_args, {
--Shortcut for coloring female, male, and unknown boxes
text = args[trimmedVal] or ('{{{'..trimmedVal..'}}}'),
colspan = args['colspan_'..rightTrimmedVal] ,
local style = args['boxstyle_'..rightTrimmedVal]
local content = args[trimmedVal] or ('{{{'..trimmedVal..'}}}')
rowspan = args['rowspan_'..rightTrimmedVal],
if style ~= nil then
border = args['border_'..rightTrimmedVal],
boxstyle = args['boxstyle_'..rightTrimmedVal]
style = string.lower(args['boxstyle_'..rightTrimmedVal])
if style == 'f' then
})
style = 'background-color: #FFDDE4;'
end
content = content .. "&nbsp;♀"
end
elseif style == 'm' then
style = 'background-color: #BDE3FF;'
return p._main(cell_args)
content = content .. "&nbsp;♂"
elseif style == 'u' then
style = 'background-color: #CECECE;'
elseif style == 'i' then
style = 'background-color: #CECECE; font-style: italic;'
else
style = args['boxstyle_'..rightTrimmedVal]
end
end
table.insert(cell_args, {
text = content,
colspan = args['colspan_'..rightTrimmedVal],
rowspan = args['rowspan_'..rightTrimmedVal],
border = args['border_'..rightTrimmedVal],
boxstyle = style
})
end
end
return p._main(cell_args)
end
end



Revision as of 04:09, 24 August 2023

--<nowiki>
local p = {}

local cells = mw.loadData('Module:Tree chart/data/sandbox')

function p._main(cell_args)
    local ret = mw.html.create()
    local top = ret:tag('tr')
                        :css{ height = '1px',
                                ['text-align'] = 'center' }
    local bottom = ret:tag('tr')
                        :css{ height = '1px',
                                ['text-align'] = 'center' }
    for _, v in ipairs(cell_args) do
        if type(v) == 'string' then
            top:wikitext(cells[v].t)
            bottom:wikitext(cells[v].b)
        else
            top:tag('td')
                :attr{ colspan = v.colspan or cell_args.colspan or 6,
                        rowspan = v.rowspan or cell_args.rowspan or 2,
                        class = 'ft-colored-box' }
                :css{ padding = '0.2em',
                        border = (v.border or cell_args.border or '2') .. 'px solid' }
                :cssText(v.boxstyle or cell_args.boxstyle)
                :wikitext(v.text)
        end
    end
    return tostring(ret)
end

function p.main(frame)
    local args = frame:getParent().args
    local cell_args = {
        colspan = args.colspan,
        rowspan = args.rowspan,
        border = args.border,
        boxstyle = args.boxstyle
    }
    for _, val in ipairs(args) do
        local trimmedVal = val:match('^%s*(.-)%s*$')
        if trimmedVal == '' then
            trimmedVal = '$'
        end
        if cells[trimmedVal] then
            table.insert(cell_args, trimmedVal)
        else
            -- Unnamed params behave weirdly
            -- white space at the front counts for param_{{{1}}}, but not whitespace at the end, so remove it
            local rightTrimmedVal = val:gsub('%s+$','')
            
            --Shortcut for coloring female, male, and unknown boxes
            local style = args['boxstyle_'..rightTrimmedVal]
            local content = args[trimmedVal] or ('{{{'..trimmedVal..'}}}')
            if style ~= nil then
                style = string.lower(args['boxstyle_'..rightTrimmedVal])
                if style == 'f' then 
                    style = 'background-color: #FFDDE4;'
                    content = content .. "&nbsp;♀"
                elseif style == 'm' then
                    style = 'background-color: #BDE3FF;'
                    content = content .. "&nbsp;♂"
                elseif style == 'u' then
                    style = 'background-color: #CECECE;'
                elseif style == 'i' then
                    style = 'background-color: #CECECE; font-style: italic;'
                else
                    style = args['boxstyle_'..rightTrimmedVal]
                end
            end
                
            table.insert(cell_args, {
                text = content,
                colspan = args['colspan_'..rightTrimmedVal],
                rowspan = args['rowspan_'..rightTrimmedVal],
                border = args['border_'..rightTrimmedVal],
                boxstyle = style
            })
        end
    end
    
    return p._main(cell_args)
end

return p