全站通知:

模块:Error

来自无畏契约WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

此模块的文档可以在模块:Error/doc创建

-- 这个模块实现{{error}}。

local p = {}

local function _error(args)
    local message = args.message or args[1] or error('没有指定的信息', 2)
    message = tostring(message)
    local tag = mw.ustring.lower(tostring(args.tag))

    -- 计算使用哪个html标签。
    if not (tag == 'p' or tag == 'span' or tag == 'div') then
        tag = 'strong'
    end

    -- 生成html。
    local root = mw.html.create(tag)
    root
        :addClass('error')
        :wikitext(message)

    return tostring(root)
end

function p.error(frame)
    local args
    if frame == mw.getCurrentFrame() then
        -- 通过#invoke调用模块。参数通过模板传递给模块,所以使用传递到模板的参数。
        args = frame.args
    else
        -- 通过另一个模块或从调试控制台被调用,所以使用被直接传入的参数。
        args = frame
    end
    -- 如果有消息参数但是是空白,则将其更改为nil,来让Lua认为它是false的。
    if args.message == "" then
        args.message = nil
    end
    return _error(args)
end

return p