![]() | This module is rated as alpha. It is ready for third-party input, and may be used on a few pages to see if problems arise, but should be watched. Suggestions for new features or changes in their input and output mechanisms are welcome. |
![]() | This module depends on the following other modules: |
This module implements Template:Calendar date (talk · links · edit).
Usage
{{#invoke:Calendar date|function_name}}
--[[
Display non-Gregorian holiday dates using equivalent Gregorian date
]]
local p = {}
--[[--------------------------< inlineError >-----------------------
Critical error. Render output completely in red. Add to tracking category.
]]
local function inlineError(arg, msg, tname)
track["Category:Holigreg template errors"] = 1
return '<span style="font-size:100%" class="error citation-comment">Error in [[Template:' .. tname .. ']] - Check <code style="color:inherit; border:inherit; padding:inherit;">|' .. arg .. '=</code> ' .. msg .. '</span>'
end
--[[--------------------------< trimArg >-----------------------
trimArg returns nil if arg is "" while trimArg2 returns 'true' if arg is ""
trimArg2 is for args that might accept an empty value, as an on/off switch like nolink=
]]
local function trimArg(arg)
if arg == "" or arg == nil then
return nil
else
return mw.text.trim(arg)
end
end
local function trimArg2(arg)
if arg == nil then
return nil
else
return mw.text.trim(arg)
end
end
--[[--------------------------< tableLength >-----------------------
Given a 1-D table, return number of elements
]]
local function tableLength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
--[[--------------------------< createTracking >-----------------------
Return data in track[] ie. tracking categories
]]
local function createTracking()
local sand = ""
if tableLength(track) > 0 then
for key,_ in pairs(track) do
sand = sand .. "[[" .. key .. "]]"
end
end
return sand
end
--[[--------------------------< verifyDate >-----------------------
Given the date arg, return true if within date range 2000-2050 else return false
]]
local function verifyDate(date)
if not date or date == "" then
return nil
end
if tonumber(date) > 1999 and tonumber(date) < 2051 then
return "true"
else
return nil
end
end
--[[--------------------------< makeDate >-----------------------
Given a zero-padded 4-digit year, 2-digit month and 2-digit day, return a full date in df format
df = mdy, dmy, iso, ymd
]]
local function makeDate(year, month, day, df)
if not year or year == "" or not month or month == "" or not day or day == "" then
return nil
end
local zmonth = month -- month with leading 0
month = month:match("0*(%d+)") -- month without leading 0
if tonumber(month) < 1 or tonumber(month) > 12 then
return year
end
local nmonth = os.date("%B", os.time{year=2000, month=month, day=1} ) -- month in name form
if not nmonth then
return year
end
local zday = day
day = zday:match("0*(%d+)")
if tonumber(day) < 1 or tonumber(day) > 31 then
if df == "mdy" or df == "dmy" then
return nmonth .. " " .. year
elseif df == "iso" then
return year .. "-" .. zmonth
elseif df == "ymd" then
return year .. " " .. nmonth
else
return nmonth .. " " .. year
end
end
if df == "mdy" then
return nmonth .. " " .. day .. ", " .. year -- September 1, 2016
elseif df == "dmy" then
return day .. " " .. nmonth .. " " .. year -- 1 September 2016
elseif df == "iso" then
return year .. "-" .. zmonth .. "-" .. zday -- 2016-09-01
elseif df == "ymd" then
return year .. " " .. nmonth .. " " .. day -- 2016 September 1
else
return nmonth .. " " .. day .. ", " .. year -- September 1, 2016
end
end
--[[--------------------------< renderHoli >-----------------------
Render the data
]]
function renderHoli(json,holiday,date,df,format)
local numRecords = tableLength(json.items)
local hits = 0
local matchdate = "^" .. date
local startdate,enddate = ""
for i = 1, numRecords do
if mw.ustring.find( json.items[i].date, matchdate ) then
if hits == 0 then
startdate = json.items[i].date
hits = 1
end
if hits >= json.days then
enddate = json.items[i].date
break
end
hits = hits + 1
end
end
return startdate .. " - " .. enddate
end
--[[--------------------------< holigreg >-----------------------
Main function
]]
function p.holigreg(frame)
local pframe = frame:getParent()
local args = pframe.args
local tname = "Holigreg" -- name of calling template. Change if template rename.
local holiday = nil -- name of holiday
local date = nil -- date of holiday
local df = nil -- date format (mdy, dmy, iso - default: iso)
local format = nil -- template display format options
track = {} -- global tracking-category table
--- Determine holiday
holiday = trimArg(args.holiday) -- required
if not holiday then
return inlineError("holiday", "Missing holiday argument", tname) .. createTracking()
end
--- Determine date
date = trimArg(args.date) -- required
if not date then
return inlineError("date", "Missing date argument", tname) .. createTracking()
elseif not verifyDate(date) then
return inlineError("date", "Invalid date", tname) .. createTracking()
end
--- Determine df
df = trimArg(args.df)
if not df then
df = "iso"
elseif df ~= "mdy" or df ~= "dmy" then
df = "iso"
end
--- Determine format type
format = trimArg(args.format)
if not format then
format = "none"
elseif format ~= "box" or format ~= "infobox" then
format = "none"
end
local version = mw.title.makeTitle( 'Template', 'Holigreg/holidays/' .. holiday .. '.js' )
local json = mw.text.jsonDecode( version:getContent() )
rend = renderHoli(json,holiday,date,df,format)
return rend
end
return p