Module:DidYouKnow

From Piñata Journal
Revision as of 23:36, 11 January 2026 by Admin Jeremy (talk | contribs)

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