Module:Random image:修订间差异

来自Arcaea中文维基
(使用 require("Module:Random") 直接加载随机数模块进行随机,并直接从Songlist中提取曲目总数)
(添加分隔符和相关的seq参数)
 
(未显示同一用户的1个中间版本)
第28行: 第28行:
local songlist=require("Module:Songlist")
local songlist=require("Module:Songlist")
local count=#songlist['songs']
local limit=tonumber(frame.args.count)
local i=random.number{count}
if not limit then
local id=songlist['songs'][i]['id']
limit=5
local title=SpecialPageName(songlist['songs'][i]['title_localized']['en'])
end
local seq=frame.args.seq
local result="[[file:Songs_"..id..".jpg|100px|link="..title.."]]"
if not seq then
seq="\n"
end
local selected=random.main("array",{t=songlist['songs'],limit=limit})
local result=""
for i, v in ipairs( selected ) do
if i>1 then
result=result..seq
end
local id=v['id']
local title=SpecialPageName(v['title_localized']['en'])
result=result.."[[file:Songs_"..id..".jpg|100px|link="..title.."]]"
end
return result
return result

2021年2月9日 (二) 19:09的最新版本

可在Module:Random image/doc创建此模块的帮助文档

local p = {}

local pagename_escape={
	["#"]="#",
	["<"]="<",
	[">"]=">",
	["["]="[",
	["]"]="]",
	["|"]="|",
	["{"]="{",
	["}"]="}",
}

function SpecialPageName(pagename)
	-- 请在这里添加页面名转换
	-- 当页面名和title_localized:en的值不同时,请在这里转换
	
	pagename=string.gsub(pagename,"[#<>%[%]|{}]",pagename_escape)
	
	return pagename

end

local random=require("Module:Random")

function p.image(frame)

	local songlist=require("Module:Songlist")
	
	local limit=tonumber(frame.args.count)
	if not limit then
		limit=5	
	end
	local seq=frame.args.seq
	if not seq then
		seq="\n"	
	end
	local selected=random.main("array",{t=songlist['songs'],limit=limit})
	local result=""
	for i, v in ipairs( selected ) do
		if i>1 then 
			result=result..seq
		end
		local id=v['id']
		local title=SpecialPageName(v['title_localized']['en'])
		result=result.."[[file:Songs_"..id..".jpg|100px|link="..title.."]]"
	end
	
	return result
end

return p