Module:Calendar date

This is an old revision of this page, as edited by GreenC (talk | contribs) at 20:01, 23 August 2018. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

--[[ 

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;">&#124;' .. 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

--[[--------------------------< 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 

  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()
  end

  local version = mw.title.makeTitle( 'Template', 'Holigreg/holidays/' .. holiday .. '.js' )    
  local json = mw.text.jsonDecode( version:getContent() )

  return json.holiday

end

return p