模块:MsgCell

来自Rizline中文维基
SSTaf讨论 | 贡献2024年5月11日 (六) 14:34的版本

可在模块:MsgCell/doc创建此模块的帮助文档

-- {{#invoke:MsgCell|func}}
local getArgs = require('Module:Arguments').getArgs;
local yesno = require('Module:Yesno');

local MsgCell = {}
function MsgCell:Create()
    local Cell = mw.html.create('div');
    if self.cellType == 'FmCell' then
        Cell = Cell:addClass('mw-parser-output')
                    :tag('div');-- 外面套一层mw-p-o,解决TemplateStyle问题
    end
    Cell:addClass('msgCell'):addClass(self.cellType);
    if self.isSmall then Cell:addClass('smallCell');end
    if self.class then Cell:addClass(self.class);end
    if self.style then Cell:cssText(self.style);end
    if self.bgColor then Cell:css('background-color',self.bgColor);end
    if self.color then Cell:css('color',self.color);end

    local image = Cell:tag('div'):addClass('image');
    if self.image then
        local img = self.image;
        if self.isSpecialImg or img:sub(1,2) == '[[' then image:wikitext(img);
        else image:wikitext('[[File:' .. img .. '|64px]]')
        end
    end
    if self.imgStyle then image:cssText(self.imgStyle);end
    if self.imgColor then image:css('background-color',self.imgColor);end

    local text = Cell:tag('div'):addClass('text');
    self.textFunc(text);
    return tostring(Cell:allDone());
end
function MsgCell:Main()
    -- TODO:Init
    self.isSmall = yesno(self.Small or self.small or false,false);
    self.isSpecialImg = yesno(self.specialImg)
    self.textFunc = function (node)
        -- class和style就不提供中文版了,别问为啥。
        local title = self.title or self['标题']
        if self.title then
            local cnode = node:tag('span'):addClass('title'):wikitext(title);

            local claxx = self.titleClass;
            if claxx then cnode:addClass(claxx);end

            local style = self.titleStyle;
            if style then cnode:cssText(style);end
        end

        local text = self.text or self['正文']
        if self.text then
            local cnode = node:tag('span'):addClass('text'):wikitext(text);

            local claxx = self.textClass;
            if claxx then cnode:addClass(claxx);end

            local style = self.textStyle;
            if style then cnode:cssText(style);end
        else error('缺失参数:text!')
        end

        local ptext = self.postscript or self['附言']
        if self.title then
            local cnode = node:tag('span'):addClass('ptext'):wikitext(ptext);

            local claxx = self.postClass;
            if claxx then cnode:addClass(claxx);end

            local style = self.postStyle;
            if style then cnode:cssText(style);end
        end
    end

    return MsgCell.Create(self);
end

local p = setmetatable({},{
    __index = function (t, k)
        local rtnFunc = function(templateFrame)
            local tab = getArgs(templateFrame);
            tab.cellType = k;
            tab.frame = templateFrame;
            return MsgCell.Main(tab)
        end
        rawset(t,k,rtnFunc)
        return rtnFunc;
    end
});
function p.Test(args)
    args.frame = mw.getCurrentFrame();
    mw.log(MsgCell.Main(args));
end
return p;