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

وحدة:Cite Shamela

من ويكاموس، القاموس الحر
local string=mw.ustring
local function eastern2number(strnumber)
	local str_number = mw.text.trim(strnumber)
	local ret_str = ''	
	for i=1, mw.ustring.len(str_number) do
		local numcode=mw.ustring.codepoint(string.sub(str_number,i,i))
		ret_str = ret_str .. mw.ustring.char(numcode-1584)
	end
	return ret_str
end


local function cite(frame)
	local b,c,v,p,s,q,txt,book_id,u
	local args = {}
	local targs = {}
	local turath = false

	
	for k, v in pairs(frame.args) do
		if v ~= "" then args[k] = v end
	end
	for k, v in pairs(frame:getParent().args) do
		if v ~= "" then args[k] = v end
	end
    local expand = args.expand
	
	local function myerror(txt)
		if expand == "json" then
			return mw.text.jsonEncode({
				error = txt
			});
		else
			return error(txt)
		end	
	end
	
	txt = args[1]
	if string.find(txt,'https://shamela.ws/book/') ~= nil or string.find(txt,'https://app.turath.io/book/') ~= nil then
		local q,c,u,ch,p,v
		if 	string.match(txt,'^([^\n]+)[\n ]%-%-%-%-%-%-%-%-[\n ]([^\n]+) %- المكتبة الشاملة[\n ]%-%-%-%-%-%-%-%-[\n ]الرابط:(https://shamela.ws/book/%d+/%d+#p%d+)$') then
			q,c,u = string.match(txt,'^([^\n]+)[\n ]%-%-%-%-%-%-%-%-[\n ]([^\n]+) %- المكتبة الشاملة[\n ]%-%-%-%-%-%-%-%-[\n ]الرابط:(https://shamela.ws/book/%d+/%d+#p%d+)$')
		elseif string.match(txt,'(ص[^\n]+) %- المكتبة الشاملة[\n ]%-%-%-%-%-%-%-%-[\n ]الرابط:(https://shamela.ws/book/%d+/%d+#p%d+)$') then
			c,u = string.match(txt,'(ص[^\n]+) %- المكتبة الشاملة[\n ]%-%-%-%-%-%-%-%-[\n ]الرابط:(https://shamela.ws/book/%d+/%d+#p%d+)$')
		elseif string.match(txt,'(https://shamela.ws/book/%d+/%d+#p%d+)$') or string.match(txt,'(https://shamela.ws/book/%d+/%d+)$') or string.match(txt,'(https://shamela.ws/book/%d+)$') then
			u = string.match(txt,'(https://shamela.ws/book/%d+/%d+#p%d+)$') or string.match(txt,'(https://shamela.ws/book/%d+/%d+)$') or string.match(txt,'(https://shamela.ws/book/%d+)$')
		elseif string.match(txt,'^[^\n]+ %- ([٠-٩ ‏/]+)[\n ](https://app.turath.io/book/%d+?page=%d+)$') then
			c,u = string.match(txt,'^[^\n]+ %- ([٠-٩ ‏/]+)[\n ](https://app.turath.io/book/%d+?page=%d+)$')
		elseif string.match(txt,'https://app.turath.io/book/%d+?page=%d+$') or string.match(txt,'https://app.turath.io/book/%d+$') then
			u = string.match(txt,'(https://app.turath.io/book/%d+?page=%d+)$') or string.match(txt,'(https://app.turath.io/book/%d+)$')
		else
			return myerror('صيغة غير قياسية')
		end
		
		local ou = u
		if string.find(txt,'app.turath.io') then
			u = u:gsub('app.turath.io','shamela.ws')
			u = u:gsub('?page=','/')
			turath = true
		end

		book_id = string.match(u,'^https://shamela.ws/book/(%d+)$') or string.match(u,'^https://shamela.ws/book/(%d+)/')
		if not book_id then
			return myerror("تحقق من رابط الموقع")
		else
			book_id=tonumber(book_id)
			if not turath and c and string.match(u,'^https://shamela.ws/book/(%d+)/(%d+)') then
				p,ch = string.match(c,'^ص([^\n ]+) %- [^\n]+ %- ([^\n%-]+)$')
				if ch then
					targs['مسار الفصل'] = ou
					targs['الفصل'] = ch
				else
					targs['المسار']= ou 
				end
				if p then
					targs['الصفحة']= p
					v = args['volume'] or args['ج'] or args['vol'] or args['المجلد'] or args['الجزء']
					if v then
						targs['المجلد']=v
					end
				end
			elseif turath and c then
				c=string.gsub(c,'‏', '')
				if string.find(c,'/') then
					v, p = string.match(c,'^([٠-٩]+) / ?([٠-٩]+)$')
					v = eastern2number(mw.text.trim(v))
					p = eastern2number(mw.text.trim(p))
				else
					p = eastern2number(mw.text.trim(c))
				end
				targs['الصفحة']= p
				if v then
					targs['المجلد']=v
				end
				targs['المسار'] = ou
			else
				targs['المسار'] = ou
			end

			if q then
				targs['اقتباس'] = q
				targs['عبر'] = '[[المكتبة الشاملة]]'
			end
			local tp=args['page'] or args['ص'] or args['p'] or args['الصفحة'] or args['صفحة']
			if tp then
				targs['الصفحة']= tp
			end

			local tv = args['volume'] or args['ج'] or args['vol'] or args['المجلد'] or args['الجزء']
			if tv then
				targs['المجلد']=tv
			end

			
		end
	else
		q = string.match(txt,':[%s\n]*«([^%)]+)»$')
		if q then
			targs['اقتباس'] = q
			targs['عبر'] = '[[المكتبة الشاملة]]'
		end
		b,c = string.match(txt,'^([^%(0-9]+) %(([^%)]+)%)')

		if not b then
			return myerror("صيغة غير قياسية")
		elseif string.sub(b,1,1) == '«' and string.sub(b,-1,-1) == '»' then
			b = mw.text.trim(string.sub(b,2,-2))
		end

		if c then
			if string.match(c,'^ص(%d+)$') then
				p = string.match(c,'^ص(%d+)$')
			elseif string.match(c,'^ص(%d+) ([ء-ي].+)$') then
				p,s = string.match(c,'^ص(%d+) ([ء-ي].+)$')
			elseif string.match(c,'^([^/]+)/ (%d+)$') then
				v,p = string.match(c,'^([^/]+)/ (%d+)$')
			elseif string.match(c,'^([^/]+)/ (%d+) ([ء-ي].+)$') then
				v,p,s = string.match(c,'^([^/]+)/ (%d+) ([ء-ي].+)$')
			else
				return myerror("صيغة غير قياسية")
			end
			if p then
				targs['الصفحة'] = p
			end
			if v then
				targs['المجلد'] = v
			end
		end

	end
	
	if args.qid then
		targs.qid = args.qid
	elseif b or book_id then
		shamela = mw.loadJsonData('Module:Cite Shamela/data.json')
		if book_id then
			for k,v in pairs(shamela) do
				if v.id == book_id then
					b = v.name
					targs.qid = v.q
					break
				end
			end
		elseif b then

			for k,v in pairs(shamela) do
				if (v.pre or v.name) == b and s == v.suf then
					book_id = v.id
					targs.qid = v.q
					break
				end
			end
		end
	end
	
	if targs.qid == nil then
		return myerror('لم يعثر على بيانات الكتاب في ويكي بيانات')
	end

    if expand == "json" then
		local params = {}
		for idx, val in pairs(targs) do
			params[idx] = { wt = val}
		end
		return mw.text.jsonEncode({
			template = {
				target= {
					href = "Template:استشهاد بويكي بيانات",
					wt= "استشهاد بويكي بيانات"
				},
				params = params
			}
		});
	elseif expand then
		local expand_args = { "{{استشهاد بويكي بيانات"  }		-- init with citation template
		-- add contents to expand_args
		for idx, val in pairs(targs) do
			table.insert(expand_args, idx .. '=' .. val )
		end
		-- make the nowiki'd string and done
		return table.concat (expand_args, '| ') .. "}}"
	end
    return table.concat ({frame:expandTemplate ({title='استشهاد بويكي بيانات/نسخ', args=targs}), '<!-- إخراج تلقائي من قالب:استشهاد بالشاملة -->'})
	
end



return { 
	cite = cite

}