פתיחת התפריט הראשי

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

--[[

templatedata services for other modules. 
template-invokable method, testTemplateData() for testing purposes only.


Methods:
	_readTemplateData( templateName )
		internal method. receives a page name, tries to see if it exists, and contains valid templatedata.
		if there is valid templatedata, it extracts the json, and uses mw methods to return a lua table containing the data.
		otherwise, returns nil

	readTemplateData( templateName )
		gets a pagename or an array of names, and cycles through them, trying to get templatedata from this file.
		when it gets a valid structure, it returns it, otherwise keep cycling.
		templateName can be a string or a table. if it's a string, it's assumed to be a template page name, 
		where valid templatedata exists in the page or in a subpage whose name is in the local variable docSubPage

	testTemplateData( templateName ): for testing. invokable from template. dumps templatedata lua object, using mw.dumpObject.
	
	compat( templateName ): backword-compatability for validation module expecting subpage in specific structure. 
]]


local docSubPage = 'תיעוד'

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

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

function testTemplateData( frame )
	local tmplateName = frame.args['שם התבנית'] or frame.args[1]
	local td = readTemplateData( tmplateName )

	return td and mw.dumpObject( td ) or ''
end

function compat( template ) 
	local td = readTemplateData( template )
	if not td or not td['params'] then return nil end
	
	local params = td['params']
	local unnamedCount = #params
	local paramnames = {}
	local required = {}
	
	for paramname, paramobject in pairs( params ) do
		table.insert( paramnames, paramname )
		if paramobject.required then 
			table.insert( required, paramname )	
		end
	end
	return { unnamedCount, paramnames, required, {} }
end


return { 
	['ReadTemplateData'] = readTemplateData,
	['compat'] = compat,
	['בדיקה'] = testTemplateData, 
	['testTemplateData'] = testTemplateData,
	}