Module:Calendar date/sandbox: Difference between revisions

Content deleted Content added
No edit summary
use require('strict') instead of require('Module:No globals')
 
(30 intermediate revisions by 4 users not shown)
Line 1:
--[[
 
Display Gregorian date of a holiday that moves year to year. Date data can be obtained from multiple sources as configured in ~Module:Calendar date/Configuration.jsevents
 
"localfile" = local JSON data file (eg. https://en.wikipedia.org/wiki/Template:Calendar_date/holidayslocalfiles/Hanukkah.js)
"calculator" = user-supplied date calculator (eg. )
"wikidata" = <tbd> for holidays with their own date entity page such as https://www.wikidata.org/wiki/Q51224536
]]
 
require('strict')
 
local p = {}
local cfg -- Data structure from ~/events
local cfg
local eventdata -- Data structure from ~/localfiles/<holiday name>
local track = {} -- globalTracking tracking-category tablecontainer
 
--[[--------------------------< inlineError >-----------------------
Line 153 ⟶ 156:
 
local function makeDate(year, month, day, df, format)
local formatFull = {
['dmy'] = 'j F Y',
['mdy'] = 'F j, Y',
['ymd'] = 'Y F j',
['iso'] = 'Y-m-d'
}
local formatInfobox = {
['dmy'] = 'j F',
['mdy'] = 'F j',
['ymd'] = 'F j',
['iso'] = 'Y-m-d'
}
 
if not year or year == "" or not month or month == "" or not day or day == "" and format[df] then
return nil
end
 
local date = table.concat ({year, month, day}) -- assemble iso format date
if format ~= "infobox" then
return mw.getContentLanguage():formatDate (formatFull[df], date)
else
return mw.getContentLanguage():formatDate (formatInfobox[df], date)
end
end
 
Line 186 ⟶ 189:
]]
 
local function dateOffset(origdate, offset)
 
local year, month, day = origdate:match ('(%d%d%d%d)-(%d%d)-(%d%d)')
Line 201 ⟶ 204:
]]
local function renderHoli(cfg,eventdata,calcdate,date,df,format,tname,cite)
 
local hits = 0
Line 210 ⟶ 213:
-- user-supplied date calculator
if cfg.datatype == "calculator" then
if cfg.datasource then
startdate = calcdate
enddate = dateOffset(startdate, cfg.days - 1)
else
return inlineError("holiday", 'Invalidinvalid calculator result', tname )
end
 
-- read dates from localfile -- it assumes dates are in chrono order, need a more flexible method
Line 235 ⟶ 238:
end
-- Verify data isand OKspecial conditions
if startdate == nil or enddate == nil then
if cfg.holidayname == "Hanukkah" and startdate and not enddate then -- Hanukkah bug, template doesn't support cross-year boundary
enddate = dateOffset(startdate, 8)
ifelseif cfg.datatype == "localfile" and cfg.days > "1" and startdate then
enddate = dateOffset(startdate, cfg.days - 1)
elseif startdate and not enddate then
return inlineError("year", 'cannot find enddate', tname) .. createTracking()
else
return inlineError("holiday", 'cannot find startdate and enddate', tname) .. createTracking()
return nil
end
end
Line 307 ⟶ 314:
local cite = nil -- leave a citation at end
local calcdate = ""
 
track = {} -- global tracking-category table
 
--- Determine holiday
Line 315 ⟶ 320:
holiday = trimArg(args.event) -- event alias
if not holiday then
return inlineError("holiday", "Missing'missing holiday argument"', tname) .. createTracking()
end
end
Line 322 ⟶ 327:
date = trimArg(args.year) -- required
if not date then
return inlineError("year", "Missing'missing year argument"', tname) .. createTracking()
elseif not isValidDate(date, "01", "01") then
return inlineError("year", "Invalid'invalid year"', tname) .. createTracking()
end
 
Line 336 ⟶ 341:
 
-- Load configuration file
local eventsfile = mw.loadData ('Module:Calendar date/Eventsevents')
if eventsfile.hebrew_calendar[mw.ustring.upper(holiday)] then
cfg = eventsfile.hebrew_calendar[mw.ustring.upper(holiday)]
elseif eventsfile.christian_events[mw.ustring.upper(holiday)] then
cfg = eventsfile.christian_events[mw.ustring.upper(holiday)]
elseif eventsfile.carnivals[mw.ustring.upper(holiday)] then
cfg = eventsfile.carnivals[mw.ustring.upper(holiday)]
elseif eventsfile.chinese_events[mw.ustring.upper(holiday)] then
cfg = eventsfile.chinese_events[mw.ustring.upper(holiday)]
elseif eventsfile.misc_events[mw.ustring.upper(holiday)] then
cfg = eventsfile.misc_events[mw.ustring.upper(holiday)]
else
return inlineError("holiday", '{{Calendar date}} – unknown holiday ' .. holiday, tname) .. createTracking()
end
 
-- If datatype = localfile
if cfg.datatype == "localfile" then
local eventfile = nil
eventfile = mw.loadData ('Module:Calendar date/localfiles/' .. holiday)
eventfile = mw.loadData(cfg.datasource)
if eventfile.event then
eventdata = eventfile.event
else
return inlineError("holiday", '{{Calendar date}} – unknown holiday file Module:Calendar date/localfiles/' .. holidaycfg.datasource .. '</span>', tname) .. createTracking()
end
 
Line 359 ⟶ 371:
local year, month, day = calcdate:match ('(%d%d%d%d)-(%d%d)-(%d%d)')
if not isValidDate(year, month, day) then
return inlineError("holiday", '{{Calendar date}} – invalid calculated date ' .. calcdate, tname) .. createTracking()
end
else
return inlineError("holiday", '{{Calendar date}} – unknown "datatype" in configuration', tname) .. createTracking()
end
 
Line 377 ⟶ 389:
cite = trimArg2(args.cite)
if cite then
if (cite ~= "no") then
cite = ""
if cfg.datatype == "localfile" then
if cfg.citeurl and cfg.accessdate and cfg.source and cfg.holidayname then
local citetitle = cfg.citetitle
cite = frame:preprocess('<ref name="' .. holiday .. ' dates">{{cite web |url=' .. cfg.citeurl .. ' |title=Dates for ' .. cfg.name .. ' |publisher=' .. cfg.source .. ' |via=[[Template:' .. tname .. '|' .. tname .. ']] and [[Module:' .. tname .. '/localfiles/' .. holiday .. '|' .. holiday .. ']] |accessdate=' .. cfg.accessdate .. '}}</ref>')
if citetitle == nil then
citetitle = 'Dates for ' .. cfg.name
end
cite = frame:preprocess('<ref name="' .. holiday .. ' dates">{{cite web |url=' .. cfg.citeurl .. ' |title=Dates for ' .. cfg.namecitetitle .. ' |publisher=' .. cfg.source .. ' |via=[[Template:' .. tname .. '|' .. tname .. ']] and [[Module:' .. tname .. '/localfiles/' .. holiday .. '|' .. holiday .. ']] |accessdate=' .. cfg.accessdate .. '}}</ref>')
elseif cfg.datatype == "calculator"source then
cite = (cfg.source and (frame:preprocess('<ref name="' .. holiday .. ' dates">' .. cfg.source:gsub("YYYY", date) .. '</ref>')))
else
cite = ""
end
else
elseif cfg.datatype == "calculator" then
cite = ""
cite = (cfg.source and (frame:preprocess('<ref name="' .. holiday .. ' dates">' .. cfg.source .. '</ref>')))
end
else