local module = {}
local getArgs = require('Module:Arguments').getArgs
local getCode = require('Module:getPageCode')
---@type mw
local mw = mw
local randomSeed = 0
function random(count, min, max)
randomSeed = randomSeed + 1
math.randomseed(tostring(os.time()):reverse():sub(1, 7) .. randomSeed)
if count == 1 then
return math.random(min, max)
end
local order = {}
function test(num)
for i = 1, #order do
if num == order[i] then
return true
end
end
return false
end
repeat
local ran = math.random(min, max)
if test(ran) == false then
order[#order + 1] = ran
end
until (#order == count)
return order
end
function _main(args, frame)
local pageName = args[1]
local except = mw.text.split(args['except'] or '', ',')
local data = ''
if string.find(pageName, '%[%[.-%]%]') then
data = pageName
else
local code = getCode(pageName)
if code == nil then
return 0
end
local templates = {links = true, ['links/br'] = true, dl = true, dis = true}
local cheapCode = string.gsub(code, '%b{}', function(s)
local tplName = string.match(s, '^{{%s*([^|]+)%s*|')
if tplName then
return templates[tplName:lower()] or false
end
end)
function saving(code)
local maxCutLen = tonumber((args['rangeLevel'] or 0) + 1) * 1000
if maxCutLen > 1000 then
local cutBorder = code:len() - maxCutLen
if cutBorder < 1 then
return code
end
local cutStart = random(1, 1, cutBorder)
local cutEnd = cutStart + maxCutLen
return code:sub(cutStart, cutEnd)
else
return code
end
end
data = frame:preprocess(saving(cheapCode))
end
if args['except'] and mw.text.trim(args['except']) ~= '' then
for i, v in ipairs(except) do
local ptn = '%[%[' ..
mw.ustring.gsub(mw.text.trim(v), '([%%%(%)%.%+%-%*%?%[%]%^%$])', '%%%1') ..
'.-%]%]'
data = mw.ustring.gsub(data, ptn, '')
end
end
local num = tonumber(args['num']) or 19
local links = {}
local iter = string.gmatch(data, '%[%[(.-)%]%]')
for v in iter do
v = string.gsub(v, '<.->', '')
local link = v
local text = v
if string.find(v, '|') then
link = string.gsub(v, '([^|]*)%|?([^|]*)', '%1')
text = string.gsub(v, '[^|]*%|?([^|]*)', '%1')
end
if mw.ustring.find(text, '^[查论编]$') == nil and mw.ustring.find(link, '^[Ff]ile:') ==
nil then
local r = random(1, 0, 255)
local g = random(1, 0, 255)
local b = random(1, 0, 255)
local element = string.format(
'[[%s|<span class="linksShow-link" style="color:rgb(%d,%d,%d);">%s</span>]]',
link, r, g, b, text)
links[#links + 1] = element
end
end
if #links < 19 then
return 1
end
local set = {}
local ran = random(num, 1, #links)
for i = 1, num do
set[#set + 1] = links[ran[i]]
end
local str = ''
for i = 1, #set do
str = str .. set[i] .. '$'
end
return str
end
function module.main(frame)
local args = getArgs(frame)
return _main(args, frame)
end
---@param frame Frame
function module.containers(frame)
-- args[1] 是待分割字符串,后续参数args[i]是第i-1个容器的样式
local args = getArgs(frame)
local rstable = {}
local i = 1
for text in string.gmatch(args[1], "([^$]+)") do
i = i + 1
local container = mw.html.create('div'):addClass('linksShow-container')
container:cssText(args[i]):wikitext(text)
table.insert(rstable, tostring(container))
end
return table.concat(rstable);
end
return module