-- Babel -- 20151021

function subtitle(text)
	-- This function builds subtitles in the box
				local sub_title = mw.html.create('table')
				  :css('text-align','center')
				  :css('width','100%')
				  :css('margin','0 auto')
				local row =  sub_title:tag('tr')
				local sub_title_cell =  row:tag('th')
				   :css('background-color','#EEFFCC')
				   :css('border','1px dotted #FFCC99')
				   :css('padding','0.3em')				   
				   :wikitext(text)
				   :done()
				-- sub_title:done()
				return tostring(sub_title)
end	

function placeholder(text)
	-- This function builds placeholders in the box
				local height = "45px"	
				local t1 = tonumber(text)
				if t1 then
					height = t1 .. ".px"
				end
				local t = " "
				local place = mw.html.create('table')
				  :css('text-align','center')
				  :css('width','100%')
				  :css('margin','0 auto')
				local row =  place:tag('tr')
				local place_cell =  row:tag('th')
				   :css('height',height)	
				   :css('padding','0.3em')				   
				   :wikitext(t)
				   :done()
				-- sub_title:done()
				return tostring(place)
end	

function line (text)
	-- This function builds lines in the box
				local line_div = mw.html.create('div')
				   :css('float','left')
				   :css('width','238px')
				   :css('height','1.5px')
				   :css('margin','1px')
				   :css('background-color','#EEEEEE')
				   :css('border','1px solid #EEEEEE')
				   :wikitext(text)
				   :done()
				return tostring(line_div)			
end	

function beginonly (text,s,c)
		-- changes html codes for the tabel's beginning into wiki codes
		-- removes the end of the table
		-- html	
		local b1 = '<table class="userboxes"'
		local b2 = '><tr><th colspan="' .. s .. '" style="background%-color:' .. c .. ';text%-align:center">'
		local b3 = "</th></tr><tr>"
		-- wiki code
		local b1w = '{|  class="userboxes"'
		local b2w = '\n|-\n! colspan="' .. s .. '" style="background-color:' .. c .. ';text-align:center" | '
		local b3w = "\n<tr>"
 
		local r = text
 
		r = mw.ustring.gsub(r,b1,b1w,1) -- <table ... -> {| ...
		r = mw.ustring.gsub(r,b2,b2w,1) -- <tr><th> ... -> |- !
		r = mw.ustring.gsub(r,b3,b3w,1) -- </tr><tr> -> <tr>
		r = mw.ustring.sub(r,1,-9)      -- </table> -> nothing
 
		return r
end	

local p = {}

function p._specialist(babels,args)
	-- this function builds the box used by the {{Babel-fachowy2}} template
	args.header = args.header or "[[Project:Babel|Fachowe znajomosće]]"  
	args.footer = args.footer or "[[:Kategorija:Wužiwarjo po předmjetach|Wužiwarjo po předmjetach]]"
	return p._babel(babels,args)
end

function p._babel_x(babels,args)
	-- this function builds the box used by the {{Babel-x}} template
	args.header = args.header or "[[Wikipedija:Babel]]:" 
	args.footer = args.footer or "'''[[:Kategorija:Wužiwarjo po rěčach|Wužiwarjo po rěčach]]'''" 
	args['extra-css'] = 'background-color:#FFFFFF;'
	return p._babel(babels,args)
end

function p._babel(babels, args)  
	-- This function builds the babel box used by the {{babel}} template.
	local fl = args.align 
	-- variables for the margins
	local l = "1"
	local r = "0"
	if fl == "left" then
		l = "0"
		r = "1"
	end	
	local ret = mw.html.create('table')
				:addClass('userboxes')
				:css( {
						float = args.align or 'right',
						['margin-left'] = l .. 'em',
						['margin-right'] = r .. 'em',
						['margin-bottom'] = (args.bottom or '0') .. 'em',
						width = (args.width or '248') .. 'px',
						clear = args.align or 'right',
						color = args.textcolor or '#000000',
						border = (args.bordercolor or '#99B3FF') .. ' solid ' .. (args.solid or 1)..'px'
					} )
				
 	if (args.shadow and string.lower(args.shadow) == 'yes') or (args['sćin'] and string.lower(args['sćin']) == 'haj')  then
		ret:css({ ['box-shadow'] = '0 2px 4px rgb(0,0,0,0.2)',
						['-mox-box-shadow'] = '0 2px 4px rgb(0,0,0,0.2)',
						['-webkit-box-shadow'] = '0 2px 4px rgb(0,0,0,0.2)' })
	end
	
	-- formats from {{Bordera radiuso}}  and the old {{Babel}}
	ret:css({ ['border-radius'] = '0.6em',
			  ['-moz-border-radius'] = '0.6em',
			  ['-webkit-border-radius'] = '0.6em' })
	
	ret:cssText( args['extra-css'] or '' )
 
	local color = args.color or 'inherit'
	local row1 = ret:tag('tr')
	local row2 = ret:tag('tr')
	local row3 = ret:tag('tr')
 
	local body_cells = row2:tag('td')
				:css('vertical-align', 'top !important')	
				
	local userboxes
	local k = args.nocat or ''
	local mf = args['mž'] or ''
	local frame = mw.getCurrentFrame()	
	-- Special message for when first argument is blank; otherwise treat it as normal
	if babels[1] and babels[1]:find('%S') then
		if mw.ustring.sub(babels[1],1,2) == "[[" then
			body_cells:wikitext(babels[1])
		elseif mw.ustring.sub(babels[1],1,2) == "{{" and mw.ustring.sub(babels[1],1,3) ~= "{{{" then		
			body_cells:wikitext(babels[1])
		elseif mw.ustring.sub(babels[1],1,1) == "<" then		
			body_cells:wikitext(babels[1])
		elseif mw.ustring.sub(babels[1],1,2) == "##" then
				local v = mw.ustring.sub(babels[1],3)
				body_cells:wikitext(subtitle(v))		
		elseif mw.ustring.sub(babels[1],1,2) == "--" then
				local v = mw.ustring.sub(babels[1],3)
				body_cells:wikitext(placeholder(v))	
		-- code for {{#Babel, because the parser analyzed this before Lua can work			
		elseif mw.ustring.sub(babels[1],1,3) == "#b#" or mw.ustring.sub(babels[1],1,3) == "#B#"	then
				local v = mw.ustring.sub(babels[1],4) 
				-- removes the internal links 
				v = mw.ustring.gsub(v,"%[%[Project:Babel%|Babela informo pri uzanto%]%]","",1)
				v = mw.ustring.gsub(v,"%[%[:Kategorio:Vikipediistoj laŭ_lingvo%|Serĉu uzulajn lingvojn%]%]","",1)				
				v = '<div style="width:246px; float:left;">' .. v .. "</div>"
				body_cells:wikitext(v)					
		elseif mw.ustring.sub(babels[1],1,1) == "~" then		
			body_cells:wikitext(line())					
		else	
			userboxes = frame:preprocess('{{User|'..babels[1]..'|mž=' .. mf .. '|nocat=' .. k .. '|b-šěrokosć=haj|wusměrjenje=left}}')
		end
	else
		userboxes = args.noboxestext or "''Tutón wužiwar njemóže čitać ani pisać někajku rěč. [[Předłoha:Babel/Dokumentacija|Pomoc]] je trjeba.''"
	end
 
	body_cells:wikitext(userboxes)				

	-- "remove" args[1] so it isn't looked at in the loop
	-- table.remove(args,1) doesn't produce desired result
	babels[1] = ''
 
	-- Keep track of how many columns are in this table
	local col_span = 1
	for _, v in ipairs( babels ) do
		-- ! indicates a new cell should be created
		if v:find('%S') and v ~= '!' then
			if mw.ustring.sub(v,1,2) == "[[" then
				body_cells:wikitext(v)
			elseif mw.ustring.sub(v,1,2) == "{{" and mw.ustring.sub(v,1,3) ~= "{{{" then	
				body_cells:wikitext(v)
			elseif mw.ustring.sub(v,1,1) == "<" then
				body_cells:wikitext(v)	
			-- converts the text with ## at the begining into subtitle	
			elseif mw.ustring.sub(v,1,2) == "##" then
				v = mw.ustring.sub(v,3)
				body_cells:wikitext(subtitle(v))	
			elseif mw.ustring.sub(v,1,2) == "--" then
				v = mw.ustring.sub(v,3)
				body_cells:wikitext(placeholder(v))	
			elseif mw.ustring.sub(v,1,1) == "~" then
				body_cells:wikitext(line())	
			-- code for {{#Babel, because the parser analyzed this before Lua can work						
			elseif mw.ustring.sub(v,1,3) == "#b#" or mw.ustring.sub(v,1,3) == "#B#"	then
				v = mw.ustring.sub(v,4) 
				-- removes the internal links 
				v = mw.ustring.gsub(v,"%[%[Project:Babel%|Babela informo pri uzanto%]%]","",1)
				v = mw.ustring.gsub(v,"%[%[:Kategorio:Vikipediistoj laŭ_lingvo%|Serĉu uzulajn lingvojn%]%]","",1)
				v = '<div style="width:246px; float:left;">' .. v .. "</div>"
				body_cells:wikitext(v)							
			else	
				body_cells:wikitext( frame:preprocess('{{User|'..v..'|mž=' .. mf .. '|nocat=' .. k .. '|b-šěrokosć=haj|wusměrjenje=left}}') )
			end
		-- Recycling body_cells for <td>
		elseif v and v == '!' then
			col_span = col_span + 1
			body_cells:done()
			body_cells = row2:tag('td')
		end
	end
 
	if (args.header ~= "bjez") and (args['hłowna linka'] ~= "bjez" ) then
	local header = args.header or args['hłowna linka']	
	row1:tag('th')
			:css({ ['background-color'] = color,
					['text-align'] = 'center' })
			:attr('colspan',col_span)
			:wikitext( header or '[[Wikipedija:Babel|Rěčne kmanosće]]' )
			:done()
	end

 	if (args.footer ~= "bjez") and (args['spódnja linka'] ~= "bjez") then
 	local footer = args.footer or args['spódnja linka'] 		
	row3:tag('td')
			:css({ ['background-color'] = color,
					['text-align'] = 'center' })
			:attr('colspan',col_span)
			:wikitext( footer or '[[:Kategorija:Wužiwarjo po rěčach|Wužiwarjo po rěčach]]' )
			:done()
	end	
	
	-- code for the template {{WužiwarskiKraj}}
	if args.kraj ~= nil and args.kraj ~= "" then
			local k = args.kraj
			local m = args['městno'] or ""
			local dm = args['družina městna'] or ""
			local mj = args.mjeno or ""
			local kon = args.kontinent or ""
			local r = args.region or ""
			local zk = args['zwjazkowy kraj'] or ""
			local wok = args.wokrjes or ""
			local woj = args['wojewódstwo'] or ""
			-- for title before the information about the user's country
			local title = args['titul před krajom'] or ""
			if title ~= "" then
					body_cells:wikitext(subtitle(title))
			end
			body_cells:wikitext( frame:preprocess('{{WužiwarskiKraj|kraj=' .. k .. '|městno=' .. m 
				.. '|družina městna=' .. dm .. '|mjeno=' .. mj 
				.. '|kontinent=' .. kon .. '|region=' .. r .. '|zwjazkowy kraj=' .. zk 
				.. '|wokrjes=' .. wok .. '|wojewódstwo=' .. woj .. '|nocat=' .. k .. '|b-šěrokosć=haj|wusměrjenje=left}}') )
	end		
	
	-- code for the template {{Narodniny}}
	if args.narodniny ~= nil and args.narodniny ~= "" then
			local n1 = args.narodniny
			-- for title before the information about the user's birth day
			local title = args['titul před narodninami'] or ""
			if title ~= "" then
					body_cells:wikitext(subtitle(title))
			end			
			body_cells:wikitext( frame:preprocess('{{Narodniny|1=' 
				.. n1 .. '|nocat=' .. k .. '|b-šěrokosć=haj|wusměrjenje=left}}') )
	end	
	
	-- code for the template {{Wiki-narodniny}}
	if args['wiki-narodniny'] ~= nil and args['wiki-narodniny'] ~= "" then
			local wn1 = args['wiki-narodniny']
			-- for title before the information about the user's wiki birth
			local title = args['titul před wiki-narodninami'] or ""
			if title ~= "" then
					body_cells:wikitext(subtitle(title))
			end				
			body_cells:wikitext( frame:preprocess('{{Wiki-narodniny|1=' 
				.. wn1 .. '|nocat=' .. k .. '|b-šěrokosć=haj|wusměrjenje=left}}') )
	end		
	
	-- code for the template {{User UTC}}
	if args.utc ~= nil and args.utc ~= "" then
			local u = args.utc
			-- for title before the information about the user's UTC
			local title = args['titul před utc'] or ""
			if title ~= "" then
					body_cells:wikitext(subtitle(title))
			end				
			body_cells:wikitext( frame:preprocess('{{User UTC|utc=' .. u .. '|nocat=' .. k .. '}}') )
	end	
	
	-- code for the template {{User UTC2}}
	if args.utc1 ~= nil and args.utc1 ~= "" then
			local u1 = args.utc1
			local u2 = args.utc2
			-- for title before the information about the user's UTC, with summer time
			local title = args['titul před utc1 a utc2'] or ""
			if title ~= "" then
					body_cells:wikitext(subtitle(title))
			end				
			body_cells:wikitext( frame:preprocess('{{User UTC2|utc1=' .. u1 .. '|utc2=' .. u2 
				.. '|nocat=' .. k .. '|b-šěrokosć=haj|wusměrjenje=left}}') )
	end	
	
	-- code for the template {{Kašćik wikidny}}
	if args.wikidny ~= nil and args.wikidny ~= "" then
			local wd = args.wikidny
			local wm = args['wikiměsacy'] or ""
			local wl = args['wikilěta'] or ""
			-- for title before the information about the user's day of first log in
			local title = args['titul před wikidnjemi'] or ""
			if title ~= "" then
					body_cells:wikitext(subtitle(title))
			end				
			body_cells:wikitext( frame:preprocess('{{Kašćik wikidny|1=' .. wd .. '|2=' .. wm 
				.. '|3=' .. wl .. '|nocat=' .. k .. '|wusměrjenje=left}}') )
	end	
	
	-- code for the template {{User SUL}}
	if args['sul-projekt'] ~= nil and args['sul-projekt'] ~= "" then
			local p = args['sul-projekt']
			local l = args['sul-rěč'] or ""
			-- for title before the information about the user's SUL
			local title = args['titul před sul'] or ""
			if title ~= "" then
					body_cells:wikitext(subtitle(title))
			end				
			body_cells:wikitext( frame:preprocess('{{User SUL|1=' .. l .. '|2=' .. p 
				.. '|nocat=' .. k .. '|b-šěrokosć=haj|wusměrjenje=left}}') )
	end		

	-- for special boxes
	local maxspec = 15
	if args['specialny kašćik'] then
			-- for a title before the special box
			local title = args['titul specialneho kašćika'] or ""
			if title ~= "" then
				body_cells:wikitext(subtitle(title))
			end		
		local s = args['specialny kašćik']		
		body_cells:wikitext(s)
	end
	for i = 2, maxspec do
		local s = args['specialny kašćik' .. i] or ""
		if s ~="" then
			-- for a title before the special box
			local title = args['titul specialneho kašćika' .. i] or ""
			if title ~= "" then
				body_cells:wikitext(subtitle(title))
			end				
			body_cells:wikitext(s)
		end
	end	
 
	if args['special-boxes'] or args['specialne kašćiki'] then
		local sp = args['special-boxes'] or args['specialne kašćiki']
		body_cells:wikitext(sp)
	end
 
	body_cells:done()
 
	local result = tostring(ret)
 
	-- changes the html coding of the tabel's beginning into wiki code
	-- removes the end of the html coding of the table
	if args['begin only'] =="yes" or args['jenož spočatk'] =="haj" then
		local s = col_span
		result = beginonly (result,s,color)
 	end
 
	return result
	
end


local function processBabelArgs(args)
	-- This function processes a table of arguments and returns two tables: an array of portal names for processing by ipairs, and a table of
	-- the named arguments that specify style options, etc. We need to use ipairs because we want to list all the portals in the order
	-- they were passed to the template, but we also want to be able to deal with positional arguments passed explicitly, for example
	-- {{portal|2=Politics}}. The behaviour of ipairs is undefined if nil values are present, so we need to make sure they are all removed.
	args = type(args) == 'table' and args or {}
	local babels = {}
	local namedArgs = {}
	for k, v in pairs(args) do
		if type(k) == 'number' and type(v) == 'string' then -- Make sure we have no non-string babel names.
			table.insert(babels, k)
		elseif type(k) ~= 'number' then
			namedArgs[k] = v
		end
	end
	table.sort(babels)
	for i, v in ipairs(babels) do
		babels[i] = args[v]
	end
	return babels, namedArgs
end
 
local function makeWrapper(funcName)
	-- Processes external arguments and sends them to the alia functions.
	return function (frame)
		-- If called via #invoke, use the args passed into the invoking
		-- template, or the args passed to #invoke if any exist. Otherwise
		-- assume args are being passed directly in from the debug console
		-- or from another Lua modulo.
		local origArgs
		if frame == mw.getCurrentFrame() then
			origArgs = frame:getParent().args
			for k, v in pairs(frame.args) do
				origArgs = frame.args
				break
			end
		else
			origArgs = frame
		end
		-- Trim whitespace and remove blank arguments.
		local args = {}
		for k, v in pairs(origArgs) do
			if type(v) == 'string' then
				v = mw.text.trim(v)
			end
			if v ~= '' then
				args[k] = v
			end
		end
		return p[funcName](processBabelArgs(args)) -- passes two tables to func: an array of portal names, and a table of named arguments.
	end
end
 
local funcNames = {'babel', 'specialist', 'babel_x'}
 
for _, funcName in ipairs(funcNames) do
	p[funcName] = makeWrapper('_' .. funcName)
end


return p
Z Wikipedije, swobodneje encyklopedije