bugfix250107.1

本WIKI于23/4/26申请开通,编辑权限开放,欢迎收藏,欢迎更多殿下加入我们来一起搭建!Wiki反馈催更群:945258792
编辑帮助:指南 | 捉虫许愿:反馈

全站通知:

模块:随机背景

来自代号鸢WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

此模块的文档可以在模块:随机背景/doc创建

local bg={}
local getArgs = require('Module:Arguments').getArgs
local DEBUG=false

function bg.getImgArgs(args)
	local imgs={}
	
	local index=1
	while true do
		if DEBUG then
			mw.log("getImgArgs,index="..index)
		end
		local argn=args[tostring(index)]
		if (argn=='' or argn ==nil) then
			if DEBUG then
				mw.log("getImgArgs,break out,index="..index)
			end
			break
		else
			if DEBUG then
				mw.log("getImgArgs,argn="..argn)
			end
			local imgUrl=bg.bgImage(argn)
			if DEBUG then
				mw.log("getImgArgs,imgUrl="..imgUrl)
			end
			if imgUrl~=nil then
				table.insert(imgs,imgUrl)
			end
		end
		index=index+1
	end
	return imgs
end

function bg.bgImage(imgAddr)
	if mw.ustring.match(imgAddr,'^https?://') ~=nil then
		return imgAddr
	else
		local filePage=nil
		if mw.ustring.match(imgAddr,'^[Ff]ile:') ~=nil or mw.ustring.match(imgAddr,'^文件:') ~=nil then 
			filePage=mw.title.new(imgAddr,0)
		else	
			filePage=mw.title.new(imgAddr,6)
		end
		if DEBUG then
			mw.logObject(filePage,"getImgArgs,filePage=")
		end
		
		if filePage~=nil and (filePage.fileExists) then
			local title=filePage.text
			local url=mw.getCurrentFrame():callParserFunction('filepath',title)
			if url~=nil and url~='' then 
				return url
			end
		end
	end
	return nil
end

function bg.chooseImg(imgs,args)
	if #imgs==1 then
		return bg.buildDataDiv(imgs[1],1,args)
	else
		local randomVal=args['choose']
		local choose=nil
		if tonumber(randomVal)~=nil then 
			 choose=tonumber(randomVal)
			 if not (choose>=1 and choose<= #imgs) then 
				 choose=1
			 end
		elseif randomVal=='random' then
			choose=nil
		elseif randomVal==nil or randomVal =='' then
			choose=1
		end
 	
		if choose==nil then
			math.randomseed(os.time())
			choose=math.random(1,#imgs)
		end
	 
		return bg.buildDataDiv(imgs[choose],choose,args)
	end
end

function bg.buildDataDiv(url,index,args)
	local cssModel=
[[body{
background:url('%s') no-repeat fixed 50%% 50%%;
background-size: cover;
%s
}]]
    local otherCss=(args['css'..index] or args['css']) or ''
	local css=mw.ustring.format(cssModel,url,otherCss)
	return mw.getCurrentFrame():extensionTag('bstyle',css)
end

function bg._main(args)
	local imgs=bg.getImgArgs(args)
	if #imgs==0 then
		return ''
	else
		local output=bg.chooseImg(imgs,args)
		return output
	end
end

function bg.main(frame)
	local args = getArgs(frame, {wrappers='Template:随机背景'})
	return bg._main(args)
end

return bg