模块:物品图片
外观
此模块的文档可以在模块:物品图片/doc创建
local C = require('模块:物品通用')
local p = {}
local function file(name)
name = name:gsub('^文件:', ''):gsub('^[Ff]ile:', '')
if name:find('[|{}%[%]\r\n]') then return nil end
local title = mw.title.new(name, 6)
if title and title.namespace == 6 and title.file.exists then return title.text end
return nil
end
function p.main(frame)
local c = C.context(frame)
if not c.enabled then return '' end
local specified = c.option('图片', '')
if specified == '无' then return '' end
local selected
if specified ~= '' then selected = file(specified)
else selected = file(c.name .. '.png') or file(c.name .. '.jpg') end
if not selected then
return C.categories(c, { '缺少图片的物品' })
end
local size = c.option('图片大小', '250'):gsub('px$', '')
local number = tonumber(size)
if not number or number % 1 ~= 0 or number < 50 or number > 1000 then error('图片大小必须在 50 至 1000 像素之间。') end
local caption = C.literal(c.option('图片说明', c.name))
return C.finish(c, 'image', '[[文件:' .. selected .. '|缩略图|' .. number .. 'px|' .. caption .. ']]')
end
return p