Module:WeeklyEvent

From Noisebridge
Revision as of 15:41, 22 October 2025 by Mcint (talk | contribs) (create module)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

docs

mw:Extension:Scribunto

mw:Module:Arguments

https://dev.fandom.com/wiki/Lua_reference_manual/Scribunto_libraries

https://en.wikipedia.org/wiki/Wikipedia:Comparable_Lua_functions_to_wikitext


local p = {}

local function d(fmt, rel)
  -- uses site content language date parser (Parsoid-safe)
  return mw.getContentLanguage():formatDate(fmt, rel)
end

local function page_exists(title)
  local t = mw.title.new(title)
  return t and t.exists
end

-- args: prefix=Meeting  weekday=Tuesday  text_this, text_prev, text_next, preload
function p.links(frame)
  local args = frame:getParent().args
  local prefix  = args.prefix  or 'Meeting'
  local weekday = args.weekday or 'Tuesday'
  local text_this = args.text_this or 'This week'
  local text_prev = args.text_prev or 'Last week'
  local text_next = args.text_next or 'Next week'
  local preload   = args.preload   -- optional

  local thisdate = d('Y-m-d', 'this ' .. weekday)
  local prevdate = d('Y-m-d', 'last ' .. weekday)
  local nextdate = d('Y-m-d', 'next ' .. weekday)

  local thisTitle = prefix .. '/' .. thisdate
  local prevTitle = prefix .. '/' .. prevdate
  local nextTitle = prefix .. '/' .. nextdate

  local latestTitle, latestText
  if page_exists(thisTitle) then
    latestTitle, latestText = thisTitle, text_this
  elseif page_exists(prevTitle) then
    latestTitle, latestText = prevTitle, text_prev
  else
    latestTitle, latestText = nil, "no recent page"
  end

  local buf = {}

  if latestTitle then
    table.insert(buf, string.format('[[%s|%s]]', latestTitle, latestText))
  else
    table.insert(buf, "''no recent page''")
  end

  table.insert(buf, ' · ')
  table.insert(buf, string.format('[[%s|%s]]', nextTitle, text_next))

  if preload then
    local url = mw.uri.canonicalUrl(nextTitle, { action='edit', preload=preload })
    table.insert(buf, ' · ')
    table.insert(buf, string.format('[%s create next]', tostring(url)))
  end

  return table.concat(buf)
end

return p