K wobsahej skočić

Modul:Babel

Z Wikipedije
-- 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 znaśa]]"  
	args.footer = args.footer or "[[:Kategorija:Wužywarje pó fachowych pólach|Wužywarje pó fachowych pólach]]"
	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['seń'] and string.lower(args['seń']) == 'jo')  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-šyrokosć=jo|wusměrjenje=left}}')
		end
	else
		userboxes = args.noboxestext or "''Toś ten wužywaŕ njamóžo cytaś daniž pisaś někaku rěc. [[Pśedłoga:Babel/Dokumentacija|Pomoc]] jo 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-šyrokosć=jo|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 ~= "bźez") and (args['głowna cera'] ~= "bźez") then
	local header = args.header or args['głowna cera']	
	row1:tag('th')
			:css({ ['background-color'] = color,
					['text-align'] = 'center' })
			:attr('colspan',col_span)
			:wikitext( header or '[[Wikipedija:Babel|Rěcne zamóžnosći]]' )
			:done()
	end

 	if (args.footer ~= "bźez") and (args['spódna cera'] ~= "bźez") then
 	local footer = args.footer or args['spódna cera'] 		
	row3:tag('td')
			:css({ ['background-color'] = color,
					['text-align'] = 'center' })
			:attr('colspan',col_span)
			:wikitext( footer or '[[:Kategorija:Wužywarje pó rěcach|Wužywarje pó rěcach]]' )
			:done()
	end	
	
	-- code for the template {{WužywarskiKraj}}
	if args.kraj ~= nil and args.kraj ~= "" then
			local k = args.kraj
			local m = args['městno'] or ""
			local dm = args['družyna městna'] or ""
			local mj = args['mě'] or ""
			local kon = args.kontinent or ""
			local r = args.region or ""
			local zk = args['zwězkowy kraj'] or ""
			local wok = args.wokrejs or ""
			local woj = args['wojewódstwo'] or args['wójwodstwo'] or ""
			-- for title before the information about the user's country
			local title = args['titel pśed krajom'] or ""
			if title ~= "" then
					body_cells:wikitext(subtitle(title))
			end			
			body_cells:wikitext( frame:preprocess('{{WužywarskiKraj|kraj=' .. k .. '|městno=' .. m 
				.. '|družyna městna=' .. dm .. '|mě=' .. mj 
				.. '|kontinent=' .. kon .. '|region=' .. r .. '|zwězkowy kraj=' .. zk 
				.. '|wokrejs=' .. wok .. '|wojewódstwo=' .. woj .. '|nocat=' .. k .. '|b-šyrokosć=jo|wusměrjenje=left}}') )
	end		
	
	-- code for the template {{Wužywaŕ 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['titel pśed narodninami'] or ""
			if title ~= "" then
					body_cells:wikitext(subtitle(title))
			end				
			body_cells:wikitext( frame:preprocess('{{Wužywaŕ narodniny|1=' 
				.. n1 .. '|nocat=' .. k .. '|b-šyrokosć=jo|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 day
			local title = args['titel 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-šyrokosć=jo|wusměrjenje=left}}') )
	end		
	
	-- code for the template {{Wužywaŕ 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['titel pśed utc'] or ""
			if title ~= "" then
					body_cells:wikitext(subtitle(title))
			end				
			body_cells:wikitext( frame:preprocess('{{Wužywaŕ 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['titel pśed utc1 a utc2'] or ""
			if title ~= "" then
					body_cells:wikitext(subtitle(title))
			end				
			body_cells:wikitext( frame:preprocess('{{Wužywaŕ UTC2|utc1=' .. u1 .. '|utc2=' .. u2 .. '|nocat=' 
				.. k .. '|b-šyrokosć=jo|wusměrjenje=left}}') )
	end	
	
	-- code for the template {{Wužywaŕ wikidny}}
	if args.wikidny ~= nil and args.wikidny ~= "" then
			local wd = args.wikidny
			local wm = args['wikimjasece'] or ""
			local wl = args['wikilěta'] or ""
			-- for title before the information about the user's day of first log in
			local title = args['titel pśed wikidnjami'] or ""
			if title ~= "" then
					body_cells:wikitext(subtitle(title))
			end				
			body_cells:wikitext( frame:preprocess('{{Wužywaŕ wikidny|1=' .. wd .. '|2=' .. wm 
				.. '|3=' .. wl .. '|nocat=' .. k .. '|b-šyrokosć=jo|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ěc'] or ""
			-- for title before the information about the user's SUL
			local title = args['titel pśed sul'] or ""
			if title ~= "" then
					body_cells:wikitext(subtitle(title))
			end				
			body_cells:wikitext( frame:preprocess('{{Wužywaŕ SUL|1=' .. l .. '|2=' .. p 
				.. '|nocat=' .. k .. '|b-šyrokosć=jo|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['titel specialnego 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['titel specialnego 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['jano zachopjeńk'] =="jo" 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'}
 
for _, funcName in ipairs(funcNames) do
	p[funcName] = makeWrapper('_' .. funcName)
end


return p