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

وحدة:Art pref/data

من ويكي مصدر، المكتبة الحرة
local title_object = mw.title.getCurrentTitle();
local content;
local ret = {}
local start;

if 10 == title_object.namespace then
	return ret
end

function getTheContent()
	local content = title_object:getContent() or '';
	local start
	local content_title
	local idxpage
	
	start =  content:find('{{ *تفضيلات مقالة[%s\n]*[|}]') or content:find('{{ *تفضيلات فهرس[%s\n]*[|}]')
	mw.logObject(start)
	if start then
		return content,start
	end
	if 104 ~= title_object.namespace then
		idxpage = content:match('index%s*=%s*["\'](.-)["\']') or content:match('فهرس%s*=%s*(.-)%s*|')
		if idxpage then
			idxpage = mw.ustring.gsub(mw.ustring.gsub(idxpage, "فهرس:", ''), "index:",'')
		end
	elseif 104 == title_object.namespace then
		idxpage = mw.ustring.gsub(title_object.text, "/%d+$",'')
	end
	
	if not idxpage then
		return nil
	end

	local idxtitle = mw.title.new(idxpage, 107)
	content = idxtitle:getContent() or ''
	start = content:find('{{ *تفضيلات فهرس[%s\n]*[|}]')
	return content,start

end

content, start = getTheContent()

if not content then
	return ret
end

if start then
	local config_template = content:match ('%b{}', start);

	if not config_template then
		return ret;
	end

	local params_t = mw.text.split (config_template:gsub ('^{{%s*', ''):gsub ('[%s\n]*}}$', ''), '[%s\n]*|[%s\n]*');
	table.remove (params_t, 1);

	for _, param in ipairs (params_t) do
		local k, v, t = param:match ('([^=]-)%s*=%s*(.+)');					-- <k> is the parameter name; <v> is parameter's assigned value
		if k then
			if k:find (':') then
				t, k = k:match ('([^:]-):(.+)');
			else
				t="عام"
			end
			if ret[t] then
				ret[t][k]=v
			else
				ret[t] = {[k]=v}
			end
		end
	end
end
return ret