לדלג לתוכן

יחידה:פרמטרים: הבדלים בין גרסאות בדף

מתוך צפונות ויקי
אין תקציר עריכה
אין תקציר עריכה
שורה 2: שורה 2:
ans = ""
ans = ""
namespace = "תבנית:"
namespace = "תבנית:"
local pframe = frame:getParent()
pframe = frame:getParent()
local template = pframe:getTitle()
local template = pframe:getTitle()
local templatename = template
local templatename = template
שורה 13: שורה 13:
suggestions = data[4]
suggestions = data[4]
for tt, t in ipairs(must) do
for tt, t in ipairs(must) do
checkmust(t, pframe.args)
checkmust(t)
end
end
for k, v in pairs(pframe.args) do
for k, v in pairs(pframe.args) do
if type(k) == 'string' then checknamed(k) elseif not isempty(v) then numbered = k end
if type(k) == "string" then checknamed(k) elseif not isempty(v) then numbered = k end
end
end
if mustnum < numbered then out ("צריך מקסימום " .. mustnum .. " פרמטרים, ישנם " .. numbered) end
if mustnum < numbered then out ("צריך מקסימום " .. mustnum .. " פרמטרים, ישנם " .. numbered) end
if (ans == "") then return ans end
if ans == "" then return ans end
return "<div class=error> '''שגיאת פרמטרים ב[[" .. template .. "]]:<br/>" .. ans .. "''' </div>[[קטגוריה:שגיאות פרמטריות]]"  
return "<div class=error> '''שגיאת פרמטרים ב[[" .. template .. "]]:<br/>" .. ans .. "''' </div>[[קטגוריה:שגיאות פרמטריות]]"  
end
function checkmust(must)
for g, h in pairs(pframe.args) do
if must == g .. "" then if isempty(h) then out("פרמטר חובה ריק: ''" .. g .. "''") end return end
end
out("פרמטר חובה חסר: ''" .. must .. "''")
end
end


שורה 32: שורה 39:
end
end


function checkmust(must, args)
for g, h in pairs(args) do
if must == g .. "" then if isempty(h) then out("פרמטר חובה ריק: ''" .. g .. "''") end return end
end
out("פרמטר חובה חסר: ''" .. must .. "''")
end
function isempty(v)
function isempty(v)
local i = 1
for i = 1, len1(v) do
local b
if mw.ustring.byte(v, i, i) > 32 then return false end
while (i <= len1(v)) do
b = mw.ustring.byte(v, i, i)
if (b > 32) then return false end
i = i + 1
end
end
return true
return true

גרסה מ־09:18, 10 במאי 2015


function foo(frame)
	ans = ""
	namespace = "תבנית:"
	pframe = frame:getParent()
	local template = pframe:getTitle()
	local templatename = template
	if find1(templatename, namespace) == 1 then templatename = mw.ustring.sub(templatename, len1(namespace) + 1) end
	local data = mw.loadData('יחידה:פרמטרים/' .. templatename)
	local numbered = 0
	local mustnum = data[1]
	parameters = data[2]
	must = data[3]
	suggestions = data[4]
	for tt, t in ipairs(must) do
		checkmust(t)
	end
	for k, v in pairs(pframe.args) do
		if type(k) == "string" then checknamed(k) elseif not isempty(v) then numbered = k end
	end
	if mustnum < numbered then out ("צריך מקסימום " .. mustnum .. " פרמטרים, ישנם " .. numbered) end
	if ans == "" then return ans end
	return "<div class=error> '''שגיאת פרמטרים ב[[" .. template .. "]]:<br/>" .. ans .. "''' </div>[[קטגוריה:שגיאות פרמטריות]]" 
end

function checkmust(must)
	for g, h in pairs(pframe.args) do
		if must == g .. "" then if isempty(h) then out("פרמטר חובה ריק: ''" .. g .. "''") end return end
	end
	out("פרמטר חובה חסר: ''" .. must .. "''")
end

function checknamed(k)
	local answer = "פרמטר לא ידוע: ''" .. k .. "''"
	for p1, p in ipairs(parameters) do
		if p == k then return end
	end
	if suggestions[k] ~= nil then answer = answer .. ', <span style = "color:green">מומלץ ' .. "''" .. suggestions[k] .. "''</span>" end
	out(answer)
end

function isempty(v)
	for i = 1, len1(v) do
		if mw.ustring.byte(v, i, i) > 32 then return false end
	end
	return true
end

function out(new)
	ans = ans .. new .. "</br>"
end

function find1(str, what, where)
	local ans = mw.ustring.find(str, what, where, true)
	if ans == nil then ans = 0 end
	return ans
end

function len1(s)
	if s == nil then return 0 end
	return mw.ustring.len(s)
end

return {['main'] = foo}