Module:WeeklyEvent: Difference between revisions

From Noisebridge
Jump to navigation Jump to search
Mcint (talk | contribs)
m local args handling
Mcint (talk | contribs)
m update for space handling
Line 2: Line 2:


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


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


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


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


   local thisTitle = prefix .. joiner .. thisdate
   local thisTitle = table.concat({prefix, thisdate}, joiner)
   local prevTitle = prefix .. joiner .. prevdate
   local prevTitle = table.concat({prefix, prevdate}, joiner)
   local nextTitle = prefix .. joiner .. nextdate
   local nextTitle = table.concat({prefix, nextdate}, joiner)


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


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


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


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


   return table.concat(buf)
   return table.concat(out)
end
end


return p
return p

Revision as of 15:52, 22 October 2025

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)
  return mw.getContentLanguage():formatDate(fmt, rel)
end

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

function p.links(frame)
  local args = frame:getParent().args
  local prefix   = args.prefix   or 'Meeting Notes'
  local joiner   = args.joiner   or ' '
  local weekday  = args.weekday  or 'Tuesday'
  local t_this   = args.text_this or 'This week'
  local t_prev   = args.text_prev or 'Last week'
  local t_next   = args.text_next or 'Next week'
  local preload  = args.preload

  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 = table.concat({prefix, thisdate}, joiner)
  local prevTitle = table.concat({prefix, prevdate}, joiner)
  local nextTitle = table.concat({prefix, nextdate}, joiner)

  local latestTitle, latestText
  if exists(thisTitle) then
    latestTitle, latestText = thisTitle, t_this
  elseif exists(prevTitle) then
    latestTitle, latestText = prevTitle, t_prev
  end

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

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

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

  return table.concat(out)
end

return p