Module:Daily: Difference between revisions
Jump to navigation
Jump to search
TeamSpecBot (talk | contribs) Daily rotation from a list page |
TeamSpecBot (talk | contribs) Skip the list page's noinclude instructions, which showed as today's entry |
||
| Line 9: | Line 9: | ||
local title = mw.title.new( listTitle ) | local title = mw.title.new( listTitle ) | ||
local text = title and title:getContent() or '' | local text = title and title:getContent() or '' | ||
-- The list page's own instructions sit in <noinclude>; they are not entries. | |||
text = text:gsub( '<noinclude>.-</noinclude>', '' ) | |||
local entries = {} | local entries = {} | ||
for line in mw.text.gsplit( text, '\n', true ) do | for line in mw.text.gsplit( text, '\n', true ) do | ||
Latest revision as of 23:31, 28 October 2026
Documentation for this module may be created at Module:Daily/doc
-- Picks one line of a list page per day, the same line for every visitor.
-- {{#invoke:Daily|pick|list=Template:Did you know/list}} returns the whole line;
-- add |field=2 to return the second part of a line split on "|".
local p = {}
local SECONDS_PER_DAY = 86400
local function readList( listTitle )
local title = mw.title.new( listTitle )
local text = title and title:getContent() or ''
-- The list page's own instructions sit in <noinclude>; they are not entries.
text = text:gsub( '<noinclude>.-</noinclude>', '' )
local entries = {}
for line in mw.text.gsplit( text, '\n', true ) do
line = mw.text.trim( ( line:gsub( '^%*+', '' ) ) )
if line ~= '' then
entries[ #entries + 1 ] = line
end
end
return entries
end
function p.pick( frame )
local args = frame.args
local entries = readList( args.list or '' )
if #entries == 0 then
return ''
end
local day = math.floor( tonumber( mw.getContentLanguage():formatDate( 'U' ) ) / SECONDS_PER_DAY )
local entry = entries[ ( day + ( tonumber( args.offset ) or 0 ) ) % #entries + 1 ]
local field = tonumber( args.field )
if field then
entry = mw.text.trim( mw.text.split( entry, '|', true )[ field ] or '' )
end
return frame:preprocess( entry )
end
return p