تضامنًا مع حق الشعب الفلسطيني |
وحدة:On This Day
توثيق الوحدة[أنشئ] [محو الاختزان][استخدامات] [قوالب]
--[[ ====================== حدث في مثل هذه اليوم================
هذه الوحدة تجلب الأحداث من الصفحات الهجرية والميلادية
]]
local p = {} --p stands for package
local hc = require "Module:Hijri/core"
local hijri = require "Module:Hijri"
local text =mw.text
local string = mw.ustring
local sandbox = ""
local maxlength = 1100
--[[========= USEFULL Functions ==========]]
function esc_pattern(str)
return string.gsub(str, "%p", "%%%1")
end
function events_compare_level_year(a,b)
if a.level < b.level then
return true
elseif a.level == b.level then
if a.jd < b.jd then
return true
end
end
return false
end
function events_compare_jd(a,b)
if a.jd < b.jd then
return true
end
return false
end
function get_events(events_array,event_text,isHijri,month,day)
local events = events_array or {}
local ei
local gpic = string.match(s,"%[%[ملف:[^%]]*%]%]")
if gpic then
s= string.gsub(s, esc_pattern(gpic) .. "[\n]*","")
end
local s=text.split(s, "<!%-%-[%s]*الأحداث[%s]*%-%->")
local p= string.find(s[2],"<noinc",1,true)
if p then
s[2] = text.trim(string.sub(s[2], 1,p-1))
end
p=string.find(s[2],"*",1,true)
local e=text.split(string.sub(s[2], p+1), "\n* ",true)
for i=1,#e do
local es,inty,jd={}
local stry=string.match(e[i], "%[%[[0-9 هـ]+%]%]") or string.match(e[i], "%[%[[0-9 هـ]+|")
if stry then
stry = string.sub(stry,3,-3)
stry=string.gsub(string.gsub(stry, "ه",""), "_","")
inty = tonumber(stry)
else
mw.log(e[i-1])
error("Can't Find Year in event " .. i)
end
if isHijri then
jd = hc.hijri2jd(inty,month,day)
else
jd = hc.gregorian2jd(inty,month,day)
end
if string.find(e[i], "**",1,true) then
local e_splits=text.split(e[i],"\n**",true)
for ii=2,#e_splits do
es[ii-1] = e_splits[1] .. e_splits[ii]
end
else
es[1] = e[i]
end
for ii=1,#es do
ei=#events+1
events[ei]={str = es[ii]}
local strl=string.match(es[ii],"<!%-%-[%s]*م[1-3][%s]*%-%->")
if strl then
p =string.find(strl,"م") + 1
events[ei].level=tonumber(string.sub(strl, p,p))
else
events[ei].level = 2
end
events[ei].year = inty
events[ei].jd = jd
events[ei].hijri = isHijri
if string.find(es[ii], "{{ط|(في الصورة)}}",1,true) then
events[ei].pic = gpic
end
end
end
return events,s[1]
end
function p.onthisday(frame)
local d
if frame.args[3] then
d = hc.Date('gregorian',tonumber(frame.args[1]),tonumber(frame.args[2]),tonumber(frame.args[3]))
elseif frame.args[2] then
d = hc.Date('gregorian',os.date("*t").year,tonumber(frame.args[1]),tonumber(frame.args[2]))
else
d = hc.Date('gregorian')
end
local strtoday = {}
strtoday['g'] = d.day .. ' ' .. hijri.julian_month_name(d.month)
local tret,bret,ret = {},{},""
local events = {}
local tg = mw.title.new("أرابيكا:في هذا اليوم/" .. strtoday['g'] .. sandbox)
if not tg.exists and #sandbox>0 then
tg = mw.title.new("أرابيكا:في هذا اليوم/" .. strtoday['g'])
end
d:set_type('hijri')
strtoday['h'] = d.day .. " " .. hijri.hijri_month_name(d.month)
local th = mw.title.new("أرابيكا:في هذا اليوم/" .. strtoday['h'] .. sandbox)
if not th.exists and #sandbox>0 then
th = mw.title.new("أرابيكا:في هذا اليوم/" .. strtoday['h'])
end
local s_events={}
if tg.exists then
s = string.gsub(tg:getContent(),"{{أرابيكا:في هذا اليوم/مناسبة هجرية|" .. strtoday['g'] .. "}}","")
if not th.exists then
-- early exit no hijri page
return frame:preprocess(s)
end
events,tret['g'] = get_events(events,s,false,d.month,d.day)
end
if th.exists then
s = th:getContent()
events,tret['h'] = get_events(events,s,true,d.month,d.day)
end
--order
table.sort(events,events_compare_level_year)
local current_length = 0
for i=1,#events do
--ret = ret .. "*" .. events[i].str .. "-" .. events[i].level .. "-" .. (events[i].year or 'No year').. (events[i].hijri and 'Hijri' or 'Not Hijri') .. "\n" .. (events[i].pic or '')
if string.len(events[i].str) <= (maxlength-current_length) then
--s_events[#s_events+1]=events[i].str .. (string.sub(events[i].str,-1)~="\n" and "\n" or "")
s_events[#s_events+1] = events[i]
current_length = current_length + string.len(events[i].str)
elseif current_length>=maxlength then
break
end
end
table.sort(s_events,events_compare_jd)
local n
local pics={h='',g=''}
bret['h'], bret['g'] = '', ''
for i=1,#s_events do
n = s_events[i].hijri and 'h' or 'g'
bret[n] = bret[n] .. "*" .. s_events[i].str .. (string.sub(s_events[i].str,-1)~="\n" and "\n" or "")
if s_events[i].pic then
pics[n] = pics[n] .. s_events[i].pic .. "\n"
end
end
ret = tret['h'] .. pics['h'] .. pics['g'] .. bret['h'] .. tret['g'] .. bret['g']
return frame:preprocess(ret)
end
return p