انتقل إلى المحتوى

وحدة:TemplateData

صفحة محمية جزئيًّا (سماح للمحررين)
من ويكيبيديا، الموسوعة الحرة

local p = {}

local docSubPage = 'شرح'

function _readTemplateData( temptitle ) 
	--local templateName = "صندوق معلومات شخص/شرح"
	local templateName =  temptitle .. '/' .. docSubPage 
	local title = mw.title.makeTitle( 10, templateName )
	--mw.log("Module:4td: " .. title)
	local templateContent = title and title.exists and title:getContent() -- template's raw content
	--mw.log(Module:4td: templateContent)
	local capture =  templateContent and mw.ustring.match( templateContent, '<templatedata%s*>(.*)</templatedata%s*>' ) -- templatedata as text
	if capture then
		return mw.text.jsonDecode(capture) 
		--return pcall( mw.text.jsonDecode, capture )
	end
	
	return false
end

function p.readTemplateData( template_Name )
	--local ok, result = _readTemplateData( name ) 
	local result = _readTemplateData( template_Name ) 
	if result then 
		return result 
	end
	return nil
end

function p.Dump(frame)
	--local title = (frame.args[1] or frame.args.id) and frame or frame:getParent()
	local title = frame.args[1]
	local data = p.readTemplateData( title )
	if data then
		return frame:extensionTag("source", mw.dumpObject(data),{ lang= 'lua'})
	end
end

return p