模块:物品通用
外观
此模块的文档可以在模块:物品通用/doc创建
local p = {}
function p.trim(value)
return mw.text.trim(tostring(value or ''))
end
function p.literal(value)
local text = mw.text.nowiki(tostring(value or ''))
return (text:gsub('\r\n', '\n'):gsub('\r', '\n'):gsub('\n', '<br>'))
end
function p.probability(entry)
if entry.probabilityStatus == 'known' then
return string.format('%.6g', entry.probability * 100) .. '%'
end
return '未知'
end
function p.context(frame)
local parent = frame:getParent()
local parentArgs = parent and parent.args or {}
local args = frame.args or {}
local context = {}
function context.raw(key)
if args[key] ~= nil then return args[key] end
return parentArgs[key]
end
function context.text(key, fallback)
local value = context.raw(key)
if p.trim(value) == '' then return fallback end
-- Preserve editor-supplied markup and line boundaries.
return value
end
function context.option(key, fallback)
local value = p.trim(context.raw(key))
if value == '' then return fallback end
return value
end
function context.flag(key, fallback)
local value = context.option(key, '')
if value == '' then return fallback end
local lowered = string.lower(value)
return not (value == '否' or value == '无' or lowered == 'no' or lowered == 'false' or value == '0')
end
context.title = mw.title.getCurrentTitle()
context.name = context.option('物品名', context.title.text)
context.kind = context.option('类型', '物品')
context.enabled = context.flag('显示', true)
context.categorize = context.title.namespace == 0 and context.flag('分类', true)
return context
end
function p.heading(context, title, defaultLevel)
if not context.flag('显示标题', true) then return '' end
local level = tonumber(context.option('标题级别', tostring(defaultLevel or 2)))
if not level or level % 1 ~= 0 or level < 2 or level > 6 then
error('标题级别必须为 2 至 6 的整数。')
end
local marks = string.rep('=', level)
return '\n' .. marks .. ' ' .. title .. ' ' .. marks .. '\n'
end
function p.categories(context, categories)
if not context.categorize then return '' end
local result, seen = {}, {}
local names = { '物品' }
if context.kind == '种子' or context.kind == '装备' then names[#names + 1] = context.kind end
for _, name in ipairs(categories or {}) do names[#names + 1] = name end
for _, name in ipairs(names) do
if not seen[name] then
result[#result + 1] = '[[分类:' .. name .. ']]'
seen[name] = true
end
end
return table.concat(result)
end
function p.finish(context, class, body, categories)
if not context.enabled or not body or body == '' then return '' end
return '<div class="wum-item-block wum-item-' .. class .. '">\n' .. body ..
'\n</div>' .. p.categories(context, categories) .. '\n'
end
function p.diagnostic(result)
local details = result.page and ('(' .. p.literal(result.page) .. ')') or ''
return '<strong class="error">物品数据异常:' .. p.literal(result.code or 'unknown') .. details .. '</strong>'
end
function p.link(name, label)
local title = mw.title.new(name)
if not title or title.interwiki ~= '' then return p.literal(label or name) end
return '[[:' .. title.prefixedText .. '|' .. p.literal(label or name) .. ']]'
end
return p