יחידה:ParamValidator: הבדלים בין גרסאות בדף
מ תקלדה |
ניסיון לחסוך הכללות |
||
| שורה 96: | שורה 96: | ||
]=] | ]=] | ||
local util = | local util = { | ||
empty = function( s ) | |||
return s == nil or type( s ) == 'string' and mw.text.trim( s ) == '' | |||
end | |||
, | |||
extract_options = function ( frame, optionsPrefix ) | |||
optionsPrefix = optionsPrefix or 'options' | |||
local options, n, more = {} | |||
if frame.args['module_options'] then | |||
local module_options = mw.loadData( frame.args['module_options'] ) | |||
if type( module_options ) ~= 'table' then return {} end | |||
local title = frame:getTitle() | |||
options = module_options[ title.namespace ] or module_options[ title.nsText ] or {} | |||
if not next( options ) then return options end | |||
end | |||
repeat | |||
ok, more = pcall( mw.text.jsonDecode, frame.args[optionsPrefix .. ( n or '' )] ) | |||
if ok and type( more ) == 'table' then | |||
for k, v in pairs( more ) do options[k] = v end | |||
end | |||
n = ( n or 0 ) + 1 | |||
until not ok | |||
return options | |||
end | |||
, | |||
build_namelist = function ( 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 | |||
} | |||
local function _readTemplateData( templateName ) | |||
local title = mw.title.makeTitle( 0, templateName ) | |||
local templateContent = title and title.exists and title:getContent() -- template's raw content | |||
local capture = templateContent and mw.ustring.match( templateContent, '<templatedata%s*>(.*)</templatedata%s*>' ) -- templatedata as text | |||
-- capture = capture and mw.ustring.gsub( capture, '"(%d+)"', tonumber ) -- convert "1": {} to 1: {}. frame.args uses numerical indexes for order-based params. | |||
if capture then return pcall( mw.text.jsonDecode, capture ) end | |||
return false | |||
end | |||
local function readTemplateData( templateName ) | |||
if type( templateName ) == 'string' then | |||
templateName = { templateName, templateName .. '/' .. docSubPage } | |||
end | |||
if type( templateName ) == "table" then | |||
for _, name in ipairs( templateName ) do | |||
local td, result = _readTemplateData( name ) | |||
if td then return result end | |||
end | |||
end | |||
return nil | |||
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" }. | -- this is the function to be called by other modules. it expects the frame, and then an optional list of subpages, e.g. { "Documentation" }. | ||
| שורה 114: | שורה 175: | ||
local t_args, template_name = t_frame.args, t_frame:getTitle() | local t_args, template_name = t_frame.args, t_frame:getTitle() | ||
local td_source = util.build_namelist( template_name, subpages ) | local td_source = util.build_namelist( template_name, subpages ) | ||
local templatedata = | local templatedata = ReadTemplateData( td_source ) | ||
local td_params = templatedata and templatedata.params | local td_params = templatedata and templatedata.params | ||
local all_aliases = {} | local all_aliases = {} | ||