יחידה:תבנית חוזרת

מתוך ויקיסוגיה
קפיצה לניווט קפיצה לחיפוש

ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:תבנית חוזרת/תיעוד

local p = {}

function p.parse( frame )
	local template = frame.args['תבנית']
	local i = 0
	local req_params = {}
	-- extract parameters from templat
	while i~=nil do
		i = mw.ustring.find( template, '\{\{\{', i )
		if i ~= nil then
			start_ind = i+3
			i = mw.ustring.find( template, '\}\}\}', i )
			param_name = mw.ustring.sub( template, start_ind, i+2 )
			req_params[param_name] = 1
		end
	end
	
	-- expand argumetns using the template
	i=1
	local found = true
	local segments = {}
	local pFrame = frame:getParent()
	while found do
		local curr_template = template
		found = true
		for k,v in pairs(req_params) do
			cur_k = mw.ustring.gsub( k, 'X}}}', tostring(i), 1 )
			if pFrame.args[cur_k]  then 
				local val = pFrame.args[cur_k]
				local param_name = '\{\{\{'..k
				local start_ind = mw.ustring.find( curr_template, param_name )
				curr_template = mw.ustring.sub( curr_template, 0, start_ind-1) .. val .. mw.ustring.sub( curr_template, start_ind+mw.ustring.len( param_name ))
			elseif k=='?X}}}' then
					curr_template = mw.ustring.gsub( curr_template, '{{{%?X}}}', tostring(i)  )
			else
				found = false
				break
			end
		end
		if  found then
			segments[i] = curr_template
		end
      	i=i+1
    end
    return table.concat( segments, '\n' )
end

p['חזרה'] = p.parse
return p