לדלג לתוכן

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

מתוך צפונות ויקי
אין תקציר עריכה
אין תקציר עריכה
שורה 1: שורה 1:
function foo(frame)
-- This is the main module function
ans = ""
function main(frame)
namespace = "תבנית:"
ans = "" -- answer to be printed
pframe = frame:getParent()
namespace = "תבנית:" -- template namespace
errors = 0
pframe = frame:getParent() -- Article frame
local template = pframe:getTitle()
errors = 0 -- errors counter
local templatename = template
local template = pframe:getTitle() -- template full name
local havesug = ""
local templatename = template -- template name to remove namespace
needsug = false
local havesug = "" -- not enough suggestions error message
if find1(templatename, namespace) == 1
needsug = false -- is there a need to add suggestions
if find1(templatename, namespace) == 1 --remove namespace from template name
then templatename = mw.ustring.sub(templatename, len1(namespace) + 1) end
then templatename = mw.ustring.sub(templatename, len1(namespace) + 1) end
local data = mw.loadData('יחידה:פרמטרים/' .. templatename)
local data = mw.loadData('יחידה:פרמטרים/' .. templatename) -- read template variables data
local numbered = 0
local numbered = 0 -- real nonnamed parameters counter
local parnum = 0
local parnum = 0 -- template data parameters counter
local sugnum = 0
local sugnum = 0 -- template data suggestions counter
local lastparam
local mustnum = data[1] -- template data maximum unnamed parameters number
local mustnum = data[1]
parameters = data[2] -- template data parameters list
parameters = data[2]
must = data[3] -- template data must parameters list
must = data[3]
suggestions = data[4] -- template data suggestions table
suggestions = data[4]
for c, cc in pairs(suggestions) do -- count suggestions
for c, cc in pairs(suggestions) do
sugnum = sugnum + 1 end
sugnum = sugnum + 1 end
if sugnum == 0 then
if sugnum == 0 then -- noone
havesug = "אין הצעות בהגדרת התבנית"
havesug = "אין הצעות בהגדרת התבנית"
else
else for c, cc in pairs(parameters) do -- count parameters
for c, cc in pairs(parameters) do
parnum = parnum + 1 end
parnum = parnum + 1 end
if sugnum < parnum / 2 then
if sugnum < parnum / 2 then -- not enough suggestions
havesug = "אין מספיק הצעות בהגדרת התבנית" end end
havesug = "אין מספיק הצעות בהגדרת התבנית" end end
for tt, t in pairs(must) do
for tt, t in pairs(must) do -- check all must parameters indeed here
checkmust(t) end
checkmust(t) end
for k, v in pairs(pframe.args) do
for k, v in pairs(pframe.args) do -- for each real parameter
if type(k) == "string"
if type(k) == "string" -- if named
then checknamed(k)
then checknamed(k) -- check it
elseif not isempty(v)
elseif not isempty(v) -- if unnamed and not empty
then numbered = k
then numbered = k end end -- remember last such parameter number
lastparam = v end end
if mustnum < numbered -- too many unnamed parameters
if mustnum < numbered
then out("צריך מקסימום " .. mustnum .. " פרמטרים ללא שם, ישנם " .. numbered .. ', והם:')
then out("צריך מקסימום " .. mustnum .. " פרמטרים ללא שם, ישנם " .. numbered .. ', והם:')
errors = errors + 1
errors = errors + 1 -- an error
for k, v in pairs(pframe.args) do
for k, v in pairs(pframe.args) do -- print all unnamed parameters
if type(k) ~= "string" then
if type(k) ~= "string" then
if not isempty(v)
if not isempty(v)
שורה 45: שורה 43:
.. '<span style="color:green">"</span>')
.. '<span style="color:green">"</span>')
else out("''" .. k .. "'' ריק") end end end end
else out("''" .. k .. "'' ריק") end end end end
if ans == ""
if ans == "" -- no problems
then return ans end
then return ans end
if needsug then ans = havesug .. "<br/>" .. ans end
if needsug -- need to print not enough parameters message
if errors > 1
then ans = havesug .. "<br/>" .. ans end
if errors > 1 -- plural
then errors = "ו"
then errors = "ו"
else errors = "" end
else errors = "" end
return '<div class="error parameters"><small>' .. " '''שגיא" .. errors .. "ת פרמטרים ב[["
return '<div class="error parameters"><small>' .. " '''שגיא" .. errors .. "ת פרמטרים ב[["
.. template .. "]]:<br/>" .. ans .. "'''</td></tr></table></small></div>"
.. template .. "]]:<br/>" .. ans .. "'''</td></tr></table></small></div>" -- answer
.. "[[קטגוריה:שגיאות פרמטריות]][[קטגוריה:שגיאות פרמטריות בתבנית " .. templatename .. "]]"
.. "[[קטגוריה:שגיאות פרמטריות]][[קטגוריה:שגיאות פרמטריות בתבנית " .. templatename .. "]]"
end
end


-- This function checks each template data must parameter
function checkmust(must)
function checkmust(must)
for g, h in pairs(pframe.args) do
for g, h in pairs(pframe.args) do -- for each real parameters
if must == g .. ""
if must == g .. "" -- found
then if isempty(h)
then if isempty(h) -- but empty
then out("פרמטר חובה ריק: ''" .. g .. "''")
then out("פרמטר חובה ריק: ''" .. g .. "''")
errors = errors + 1 end
errors = errors + 1 end
return end end
return end end
out("פרמטר חובה חסר: ''" .. must .. "''")
out("פרמטר חובה חסר: ''" .. must .. "''") -- not found
errors = errors + 1
errors = errors + 1
end
end


-- This function checks each named real parameter
function checknamed(k)
function checknamed(k)
local answer = "פרמטר לא ידוע: ''" .. k .. "''"
local answer = "פרמטר לא ידוע: ''" .. k .. "''" -- answer if will be needed
for p1, p in pairs(parameters) do
for p1, p in pairs(parameters) do -- for each template data parameter
if p == k
if p == k -- match
then return end end
then return end end
errors = errors + 1
errors = errors + 1
if suggestions[k] == nil
if suggestions[k] == nil -- is there a suggestion
then needsug = true
then needsug = true -- need more suggestions
else answer = answer .. ', <span style = "color:green">מומלץ ' .. "''" .. suggestions[k] .. "''</span>" end
else answer = answer .. ', <span style = "color:green">מומלץ ' .. "''" .. suggestions[k] .. "''</span>" end -- yes
out(answer)
out(answer) -- output
end
end


-- This procedure checks is the argument is nothing then whitespace
function isempty(v)
function isempty(v)
for i = 1, len1(v) do
for i = 1, len1(v) do -- each character
if mw.ustring.byte(v, i, i) > 32
if mw.ustring.byte(v, i, i) > 32 -- real text
then return false end end
then return false end end
return true
return true -- indeed empty
end
end


-- This function add a line to the output
function out(new)
function out(new)
ans = ans .. new .. "</br>"
ans = ans .. new .. "</br>"
end
end


-- This functions finds substring in string
function find1(str, what, where)
function find1(str, what, where)
local ans = mw.ustring.find(str, what, where, true)
local ans = mw.ustring.find(str, what, where, true)
if ans == nil
if ans == nil -- not found
then ans = 0 end
then ans = 0 end
return ans
return ans
end
end


-- This function calculates string length
function len1(s)
function len1(s)
if s == nil
if s == nil -- no string
then return 0 end
then return 0 end
return mw.ustring.len(s)
return mw.ustring.len(s)
end
end


return {['main'] = foo}
return {main = main}

גרסה מ־18:55, 15 במאי 2015


-- This is the main module function
function main(frame)
	ans = "" -- answer to be printed
	namespace = "תבנית:" -- template namespace
	pframe = frame:getParent() -- Article frame
	errors = 0 -- errors counter
	local template = pframe:getTitle() -- template full name
	local templatename = template -- template name to remove namespace
	local havesug = "" -- not enough suggestions error message
	needsug = false -- is there a need to add suggestions
	if find1(templatename, namespace) == 1 --remove namespace from template name
		then templatename = mw.ustring.sub(templatename, len1(namespace) + 1) end
	local data = mw.loadData('יחידה:פרמטרים/' .. templatename) -- read template variables data
	local numbered = 0 -- real nonnamed parameters counter
	local parnum = 0 -- template data parameters counter
	local sugnum = 0 -- template data suggestions counter
	local mustnum = data[1] -- template data maximum unnamed parameters number
	parameters = data[2] -- template data parameters list
	must = data[3] -- template data must parameters list
	suggestions = data[4] -- template data suggestions table
	for c, cc in pairs(suggestions) do -- count suggestions
		sugnum = sugnum + 1 end
	if sugnum == 0 then -- noone
		havesug = "אין הצעות בהגדרת התבנית"
		else for c, cc in pairs(parameters) do -- count parameters
				parnum = parnum + 1 end
			if sugnum < parnum / 2 then -- not enough suggestions
				havesug = "אין מספיק הצעות בהגדרת התבנית" end end
	for tt, t in pairs(must) do -- check all must parameters indeed here
		checkmust(t) end
	for k, v in pairs(pframe.args) do -- for each real parameter
		if type(k) == "string" -- if named
			then checknamed(k) -- check it
			elseif not isempty(v) -- if unnamed and not empty
					then numbered = k end end -- remember last such parameter number
	if mustnum < numbered -- too many unnamed parameters
		then out("צריך מקסימום " .. mustnum .. " פרמטרים ללא שם, ישנם " .. numbered .. ', והם:')
		errors = errors + 1 -- an error
		for k, v in pairs(pframe.args) do -- print all unnamed parameters
			if type(k) ~= "string" then
				if not isempty(v)
					then out("''" .. k .. "'': " .. '<span style="color:green">"</span>' .. v
						.. '<span style="color:green">"</span>')
					else out("''" .. k .. "'' ריק") end end end end
	if ans == "" -- no problems
		then return ans end
	if needsug -- need to print not enough parameters message
		then ans = havesug .. "<br/>" .. ans end
	if errors > 1 -- plural
		then errors = "ו"
		else errors = "" end
	return '<div class="error parameters"><small>' .. " '''שגיא" .. errors .. "ת פרמטרים ב[["
		.. template .. "]]:<br/>" .. ans .. "'''</td></tr></table></small></div>" -- answer
		.. "[[קטגוריה:שגיאות פרמטריות]][[קטגוריה:שגיאות פרמטריות בתבנית " .. templatename .. "]]"
end

-- This function checks each template data must parameter
function checkmust(must)
	for g, h in pairs(pframe.args) do -- for each real parameters
		if must == g .. "" -- found
			then if isempty(h) -- but empty
					then out("פרמטר חובה ריק: ''" .. g .. "''")
						errors = errors + 1 end
				return end end
	out("פרמטר חובה חסר: ''" .. must .. "''") -- not found
	errors = errors + 1
end

-- This function checks each named real parameter
function checknamed(k)
	local answer = "פרמטר לא ידוע: ''" .. k .. "''" -- answer if will be needed
	for p1, p in pairs(parameters) do -- for each template data parameter
		if p == k -- match
			then return end end
	errors = errors + 1
	if suggestions[k] == nil -- is there a suggestion
		then needsug = true -- need more suggestions
		else answer = answer .. ', <span style = "color:green">מומלץ ' .. "''" .. suggestions[k] .. "''</span>" end -- yes
	out(answer) -- output
end

-- This procedure checks is the argument is nothing then whitespace
function isempty(v)
	for i = 1, len1(v) do -- each character
		if mw.ustring.byte(v, i, i) > 32 -- real text
			then return false end end
	return true -- indeed empty
end

-- This function add a line to the output
function out(new)
	ans = ans .. new .. "</br>"
end

-- This functions finds substring in string
function find1(str, what, where)
	local ans = mw.ustring.find(str, what, where, true)
	if ans == nil -- not found
		then ans = 0 end
	return ans
end

-- This function calculates string length
function len1(s)
	if s == nil -- no string
		then return 0 end
	return mw.ustring.len(s)
end

return {main = main}