Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.
Revision as of 11:59, 4 July 2024 by Luke (talk | contribs) (Created page with "local p = {} --[[local data = mw.loadJsonData( 'Module:RandomTip/data.json' ) local function getTipsCount() local tips_count = 0 for key,value in pairs(data.tips) do tips_count = tips_count + 1 end return tips_count end function p.getRandomDailyTip() local seed = os.date("%Y%m%d") math.randomseed(tonumber(seed)) local tips_count = getTipsCount() local index = math.random(1, tips_count) return data.tips[index] end function p.createTable(...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local p = {}

--[[local data = mw.loadJsonData( 'Module:RandomTip/data.json' )

local function getTipsCount()
	local tips_count = 0
	for key,value in pairs(data.tips) do 
		tips_count = tips_count + 1
	end	
	return tips_count
end

function p.getRandomDailyTip()
    local seed = os.date("%Y%m%d")
    math.randomseed(tonumber(seed))
    
    local tips_count = getTipsCount()
	local index = math.random(1, tips_count)
   
	return data.tips[index]
end

function p.createTable() 
	local table_index = 0
	local tbl = mw.html.create('table'):addClass("wikitable")
	tbl:tag('tr')
		:tag('th'):wikitext("ID"):done()
		:tag('th'):wikitext("Tip"):done()
	:done()
	for k, v in pairs(data.tips) do
		table_index = table_index + 1
		tbl:tag('tr')
			:tag('td')
				:wikitext(table_index)
			:tag('td')
				:wikitext(v)
	end
	return tostring(tbl)
end--]]

local function getDailyRandomNumber(maxNum) 
   local seed = os.date("%Y%m%d")
   math.randomseed(tonumber(seed))
   return math.random(1, maxNum)
end

local function getPageContent(page)
	local title = mw.title.new(page)
	if not title then return nil end
	return title:getContent()
end

local function parseTableContent(content)
	local rows  = {}
	for line in content:gmatch("[^\r\n]+") do
        if line:match("^|%-$") then
            inRow = true
            row = nil
        elseif inRow then
        	if not line:match("^|(%d+)$") then
	            row = line:match("^|(.+)$")
	            if row then
	                table.insert(rows, row)
	                inRow = false
	            end
	    	end
        end
	end
	return rows
end

function p.main(frame)
	local rows = {}
	local page = frame.args[1]
	local content = getPageContent(page)
	if not content then return 'Page not found' end
	
	local rows = parseTableContent(content)
	if(#rows == 0) then return 'No rows found' end
    
    local index = getDailyRandomNumber(#rows)
	return rows[index]
end

return p