Модуль:Песочница/stannic

Материал из Википедии — свободной энциклопедии
Перейти к навигации Перейти к поиску
Документация

См. также

[править код]
local p = {}

function _makeTable(rc_function, rows_sequence, row_headers, columns_sequence, column_headers, op_symbol)
	local parts = {
		'{|class="wikitable"\n'
	}
	if column_headers then
		table.insert(parts, '|-\n')
		if row_headers then
			table.insert(parts, '!' .. op_symbol .. '\n')
		end
		for ci, cv in ipairs(columns_sequence) do
			table.insert(parts, '!' .. cv .. '\n')
		end
	end
	for ri, rv in ipairs(rows_sequence) do
		table.insert(parts, '|-\n')
		if row_headers then
			table.insert(parts, '!' .. rv .. '\n')
		end
		for ci, cv in ipairs(columns_sequence) do
			local v = rc_function(rv, cv)
			table.insert(parts, '|' .. v .. '\n')
		end
	end
	table.insert(parts, '|}\n')
	return table.concat(parts)
end

local _op = {
	["*"] = function(row, col) return row * col end,
	["+"] = function(row, col) return row + col end,
	["rc"] = function(row,col) return row .. col end,
	["cr"] = function(row,col) return col .. row end
}

function p.tbl( frame )
	-- how to compute cells?
	local opname = frame.args['operation']
	local opfunc = nil
	if opname ~= nil then
		opfunc = _op[opname]
	end
	if opfunc == nil then
		opfunc = function(row,col) return '<span style="color:red">?</span>' end
	end
	-- construct and return table
	return _makeTable(
		opfunc,
		mw.text.split(frame.args['rows'] or '', '\\'), true,
		mw.text.split(frame.args['columns'] or '', '\\'), true,
		frame.args['operation_symbol'] or opname or '')
end

function p.hello( frame )
    local n = frame.args[1]
    if n == "begin" then
        return [=[
{|class="wikitable"
|-
! n !! n<sup>2</sup> !! n<sup>3</sup>
]=]
    end
    if n == "end" then
        return [=[
|}
]=]
    end
    return [=[
|-
| ]=] .. n .. "||" .. n*n .. "||" .. n*n*n .. "\n"
end

return p