模块:LinksShow
此模块的文档可以在模块:LinksShow/doc创建
local module = {}
local getArgs = require('Module:Arguments').getArgs
local getCode = require('Module:getPageCode')
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 ptn = '^\{\{(links|links\/br|dl|dis)\|'
local cheapCode = string.gsub(code, '%b{}', function(s)
local isMatched = frame:expandTemplate{ title = 'regex', args = { 'test', s, ptn } }
if isMatched ~= '1' then
return (string.gsub(s, '^{', 'ŸŸ'):gsub('}$', '‡‡'))
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 = '[['..link..'|<span class="linksShow-link" style="color:rgb('..r..','..g..','..b..');">'..text..'</span>]]'
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
return module