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

תוכן שנמחק תוכן שנוסף
תיעוד
הוצאת פונקציות שירות ליחידה נפרדת
שורה 96:
]=]
 
local util = require( 'Module:Utility' )
function empty( s )
return not s or type( s ) == 'string' and mw.text.trim( s ) == ''
end
 
function extract_options( frame )
local ok, options = pcall( mw.text.jsonDecode, frame.args.options )
options = ok and options or {}
local n, more = 0
repeat
n = n + 1
ok, more = pcall( mw.text.jsonDecode, frame.args['options' .. n] )
if ok then
for k, v in pairs( more ) do options[k] = v end
end
until not ok
return options
end
 
function build_namelist( template_name, sp )
local res = { template_name }
if sp then
if type( sp ) == 'string' then sp = { sp } end
for _, p in ipairs( sp ) do table.insert( res, template_name .. '/' .. p ) end
end
return res
end
 
-- this is the function to be called by other modules. it expects the frame, and then an optional list of subpages, e.g. { "Documentation" }.
שורה 135 ⟵ 110:
local t_frame = frame:getParent()
local t_args, template_name = t_frame.args, t_frame:getTitle()
local td_source = util.build_namelist( template_name, subpages )
local templatedata = require( 'Module:ReadTd' ).ReadTemplateData( td_source )
local td_params = templatedata and templatedata.params
שורה 146 ⟵ 121:
-- handle undeclared and deprecated
for p_name, value in pairs( t_args ) do
local tp_param, noval, numeric, table_name = td_params[p_name], util.empty( value ), tonumber( p_name )
if not tp_param then -- not in TD: this is called undeclared
שורה 169 ⟵ 144:
-- test for empty/missing paraeters declared "required"
for p_name, param in pairs( td_params ) do
if param.required and util.empty( t_args[p_name] ) then
res['empty-required'] = res['empty-required'] or {}
res['empty-required'][p_name] = ''
שורה 180 ⟵ 155:
-- this is the "user" version, called with {{#invoke:}} returns a string, as defined by the options parameter
function validateParams( frame )
local options, report, template_name = util.extract_options( frame ), '', frame:getParent():getTitle()
 
local wrap_report = function()
if util.empty( report ) then return '' end
local naked = mw.title.new( template_name )['text']