Module:DidYouKnow: Difference between revisions

From Piñata Journal
(Created page with "local p = {} -- CONFIG local DATA_PAGE = "Template:Did you know/Data" function p.main(frame) local title = mw.title.new(DATA_PAGE) if not title or not title.exists then return "<span class='error'>DidYouKnow: data page not found</span>" end local content = title:getContent() if not content then return "<span class='error'>DidYouKnow: no content</span>" end local items = {} -- Parse bullet list for line in content:g...")
 
No edit summary
Line 29: Line 29:
     end
     end


     -- Random selection
     -- Safe random seed
     math.randomseed(os.time() + tonumber(tostring({}):match("0x(.*)"), 16))
     math.randomseed(os.time())
    math.random() -- throw away first value
 
     local choice = items[math.random(#items)]
     local choice = items[math.random(#items)]
     return choice
     return choice
end
end


return p
return p

Revision as of 23:36, 11 January 2026

Documentation for this module may be created at Module:DidYouKnow/doc

local p = {}

-- CONFIG
local DATA_PAGE = "Template:Did you know/Data"

function p.main(frame)
    local title = mw.title.new(DATA_PAGE)
    if not title or not title.exists then
        return "<span class='error'>DidYouKnow: data page not found</span>"
    end

    local content = title:getContent()
    if not content then
        return "<span class='error'>DidYouKnow: no content</span>"
    end

    local items = {}

    -- Parse bullet list
    for line in content:gmatch("[^\r\n]+") do
        local item = line:match("^%*%s*(.+)")
        if item then
            table.insert(items, item)
        end
    end

    if #items == 0 then
        return "<span class='error'>DidYouKnow: no items found</span>"
    end

    -- Safe random seed
    math.randomseed(os.time())
    math.random() -- throw away first value

    local choice = items[math.random(#items)]
    return choice
end

return p