Module:Tabs: Difference between revisions

Jump to navigation Jump to search
No edit summary
No edit summary
Tag: Manual revert
 
(33 intermediate revisions by the same user not shown)
Line 2: Line 2:


local p = {}
local p = {}
function trim(s) -- Allows removing of whitespace and line breaks from inputs; adapted from (https://www.lua.org/manual/2.4/node32.html)
local l = 1
while string.sub(s,l,l) == ' ' do
l = l+1
end
while string.sub(s,l,l+1) == '\n' do
l = l+2
end
local r = string.len(s)
while string.sub(s,r,r) == ' ' do
r = r-1
end
while string.sub(s,r,r-1) == '\n' do
r = r-2
end
return string.sub(s,l,r)
end


function p.Main(frame)
function p.Main(frame)
local args = frame.args
local oArgs = frame:getParent().args
local args = {}
for k, v in pairs(oArgs) do
v = v:match('^%s*(.-)%s*$')
if v ~= '' then
args[k] = v
end
end
local i = 0
local i = 0
 
if args['active'] ~= nil then
active = tonumber(trim(args['active']))
else
active = 1
end
if args[1] and args[2] then
if args[1] and args[2] then
local header = mw.html.create( 'div' ):addClass('tab-header')
local header = mw.html.create( 'div' ):addClass('g-tab-header')
local contents = mw.html.create( 'div' ):addClass('tab-contents')
local contents = mw.html.create( 'div' ):addClass('g-tab-contents')
repeat
repeat
header = header:node(mw.html.create( 'span' ):addClass('tab'):wikitext(args[i+2]))
tab = mw.html.create( 'span' ):addClass('g-tab'):wikitext(trim(args[i+1]))
contents = contents:node(mw.html.create( 'div' ):addClass('tab-content'):wikitext(args[i+1]))
tabcontent = mw.html.create( 'div' ):addClass('g-tab-content'):wikitext('\n' .. trim(args[i+2]) .. '\n')
if active == (i + 2) * 0.5 then
tab = tab:addClass('g-tab-active')
tabcontent = tabcontent:addClass('g-tab-content-active')
end
header = header:node(tab)
contents = contents:node(tabcontent)
i = i + 2
i = i + 2
until args[i+1] == nil or args[i+2] == nil
until args[i+1] == nil or args[i+2] == nil
local output = mw.html.create( 'div' ):addClass('tab-container'):wikitext( 'Tab container' ):node(header):node(contents)
local output = mw.html.create( 'div' ):addClass('g-tab-container'):node(header):node(contents)


return output
return output

Latest revision as of 22:05, 28 September 2024

Documentation for this module may be created at Module:Tabs/doc

local getArgument = require('Module:Arguments').getArgument

local p = {}

function trim(s) -- Allows removing of whitespace and line breaks from inputs; adapted from (https://www.lua.org/manual/2.4/node32.html)
	local l = 1
	while string.sub(s,l,l) == ' ' do
	l = l+1
	end
	while string.sub(s,l,l+1) == '\n' do
	l = l+2
	end
	local r = string.len(s)
	while string.sub(s,r,r) == ' ' do
	r = r-1
	end
	while string.sub(s,r,r-1) == '\n' do
	r = r-2
	end
	return string.sub(s,l,r)
end

function p.Main(frame)
	local oArgs = frame:getParent().args
	local args = {}
	for k, v in pairs(oArgs) do
		v = v:match('^%s*(.-)%s*$')
		if v ~= '' then
			args[k] = v
		end
	end
	local i = 0
	if args['active'] ~= nil then
		active = tonumber(trim(args['active']))
	else
		active = 1
	end
	if args[1] and args[2] then
		local header = mw.html.create( 'div' ):addClass('g-tab-header')
		local contents = mw.html.create( 'div' ):addClass('g-tab-contents')
		repeat
			tab = mw.html.create( 'span' ):addClass('g-tab'):wikitext(trim(args[i+1]))
			tabcontent = mw.html.create( 'div' ):addClass('g-tab-content'):wikitext('\n' .. trim(args[i+2]) .. '\n')
			if active == (i + 2) * 0.5 then
				tab = tab:addClass('g-tab-active')
				tabcontent = tabcontent:addClass('g-tab-content-active')
			end
			header = header:node(tab)
			contents = contents:node(tabcontent)
			i = i + 2
		until args[i+1] == nil or args[i+2] == nil
		local output = mw.html.create( 'div' ):addClass('g-tab-container'):node(header):node(contents)

		return output
	else
		return 'Values not filled.'
	end
end

return p