Module:Calendar date/sandbox: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
Line 189:
]]
function renderHoli(jsonisAvail,holidayit,jsoncfg,jsonlocal,date,df,format,tname,cite,usercalc)
 
local hits = 0
Line 197:
 
-- user-supplied date calculator
if usercalcisAvail == "calculator" then
if jsoncfg.main.event[it].datasource then
startdate = usercalc
enddate = dateOffset(startdate, json.days= - 1)jsoncfg.main.event[it].datasource
enddate = dateOffset(startdate, json.days - 1)
else
else
-- read dates from json.items
return inlineError("holiday", 'Invalid calculator result' )
-- Get first and last date of holiday
end
local numRecords = tableLength(json.items)
 
-- read dates from jsonlocal.items -- it assumes dates in the json are in chrono order, needs a more flexible method
elseelseif isAvail == "jsonlocal" then
local numRecords = tableLength(jsonlocal.items) -- Get first and last date of holiday
for i = 1, numRecords do
if mw.ustring.find( jsonjsonlocal.items[i].date, matchdate ) then
if hits == 0 then
startdate = jsonjsonlocal.items[i].date
starttitle = json.items[i].title
hits = 1
end
if hits >= tonumber(jsonjsoncfg.main.event[it].days) then
enddate = json.items[i].date
endtitle = json.items[i].title
break
end
Line 223 ⟶ 225:
-- Verify data is OK
if startdate == nil or enddate == nil then
if mwjsoncfg.ustringmain.find(event[it].holiday starttitle,== "ChanukahHanukkah" )and startdate and not enddate then -- Hanukkah bug, template doesn't support cross-year boundary
enddate = dateOffset(startdate, 8)
else
Line 231 ⟶ 233:
-- Generate start-date offset (ie. holiday starts the evening before the given date)
if jsonjsoncfg.main.event[it].startoffset then
startdate = dateOffset(startdate, jsonjsoncfg.main.event[it].startoffset)
if startdate ~= enddate then
enddate = dateOffset(enddate, jsonjsoncfg.main.event[it].startoffset)
else
if jsonjsoncfg.main.event[it].days == "1" then
jsonjsoncfg.main.event[it].days = "2"
end
end
Line 243 ⟶ 245:
-- Generate end-date outside-Irael offset (ie. outside Israel the holiday ends +1 day later)
if jsonjsoncfg.main.event[it].endoutoffset then
endoutoffset = dateOffset(enddate, jsonjsoncfg.main.event[it].endoutoffset)
end
 
Line 274 ⟶ 276:
-- return output
if startdate == enddate or jsonjsoncfg.main.event[it].days == "1" then -- single date
return jsonjsoncfg.main.event[it].prepend1 .. startdate .. endoutoffset .. cite
else
return jsonjsoncfg.main.event[it].prepend1 .. startdate .. format .. jsonjsoncfg.main.event[it].prepend2 .. enddate .. endoutoffset .. cite
end
Line 295 ⟶ 297:
if cfg.main.event[i].datatype:lower() == "calculator" then
return "calculator", i
elseif cfg.main.event[i].datatype:lower() == "localjsonjsonlocal" or cfg.main.event[i].datatype:lower() == "local json local" then
return "localjsonjsonlocal", i
else
return nil, nil
Line 355 ⟶ 357:
return inlineError("holiday", 'File missing: Template:' .. tname .. '/Configuration.js', tname) .. createTracking()
end
local jsoncfg, jsonitemsjsonlocal = nil
jsoncfg = mw.text.jsonDecode( cfgfp:getContent() )
if not jsoncfg then
Line 365 ⟶ 367:
return inlineError("holiday", 'Template:' .. tname .. '/Configuration.js misconfigured: missing datasource', tname) .. createTracking()
end
if isAvail == "localjsonjsonlocal" then -- dates are contained in a local JSON file
local version = mw.title.makeTitle( jsoncfg.main.event[it].datasource )
if not version.exists then
Line 371 ⟶ 373:
end
if version.isRedirect then
jsonitemsjsonlocal = mw.text.jsonDecode( version.redirectTarget:getContent() )
else
jsonitemsjsonlocal = mw.text.jsonDecode( version:getContent() )
end
elseif isAvail == "calculator" then -- dates are calculated with a user-provided calculator
Line 404 ⟶ 406:
if cite then
cite = ""
if isAvail == "localjsonjsonlocal" then
if jsoncfg.main.event[it].citeurl and jsoncfg.main.event[it].accessdate and jsoncfg.main.event[it].source and jsoncfg.main.event[it].holiday then
cite = frame:preprocess('<ref name="' .. holiday .. ' dates">{{cite web |url=' .. jsoncfg.main.event[it].citeurl .. ' |title=Dates for ' .. jsoncfg.main.event[it].holiday .. ' |publisher=' .. jsoncfg.main.event[it].source .. ' |via=[[Template:' .. tname .. '|' .. tname .. ']] and [[Template:' .. tname .. '/holidays/' .. holiday .. '.js|' .. holiday .. '.js]] |accessdate=' .. jsoncfg.main.event[it].accessdate .. '}}</ref>')
Line 428 ⟶ 430:
 
-- Render
local rend = renderHoli( isAvail,it,jsoncfg,holidayjsonlocal,date,df,format,tname,cite,jsoncfg.main.event[it].datasource )
if not rend then
rend = '<span style="font-size:100%" class="error citation-comment">Error in [[:Template:' .. tname .. ']]: Unknown problem. Please report on template talk page.</span>'