跳转到内容

模組:沙盒/Kcx36/sandbox

维基百科,自由的百科全书
require ('strict');

local cfg = mw.loadData ('Module:Cite/config');

--[[--------------------------< S U B S T I T U T E >----------------------------------------------------------
Substitutes $1, $2, etc in <message> with data from <data_t>.
]]
local function substitute (message, data_t)
    return data_t and mw.message.newRawMessage (message, data_t):plain() or message;
end

--[[--------------------------< M A K E _ E R R O R _ M S G >--------------------------------------------------
Assembles an error message with module name, message text, help link, and error category.
]]
local function make_error_msg (frame, msg)
    local module_name = frame:getTitle();
    local namespace = mw.title.getCurrentTitle().namespace;
    local category_link = (0 == namespace) and substitute ('[[Category:$1]]', {cfg.settings_t.err_category}) or '';
    return substitute ('<span style="color:#d33">错误:&#x7B;{[[$1|#invoke:$2]]}:$3 ([[:$4|$5]])</span>$6', {
        module_name,
        module_name:gsub ('Module:', ''),
        msg,
        cfg.settings_t.help_text_link,
        cfg.settings_t.help,
        category_link
    });
end

--[[--------------------------< C R E A T E _ F R A M E >-----------------------------------------------------
Creates a frame object compatible with zh-wiki's Module:Citation/CS1
]]
local function create_frame(frame, template, args)
    return {
        getParent = function() return frame end,
        getTitle = function() return 'Template:' .. template end,
        args = args,
        
        -- Metatable to handle function calls
        __index = function(t, k)
            if type(frame[k]) == 'function' then
                return function(_, ...)
                    return frame[k](frame, ...)
                end
            else
                return frame[k]
            end
        end
    }
end

--[[--------------------------< C I T E >---------------------------------------------------------------------
Main citation function that calls Module:Citation/CS1
]]
local function cite(frame, template)
    local args = require('Module:Arguments').getArgs(frame, {frameOnly = true});
    template = template:lower();
    
    if not cfg.known_templates_t[template] then
        return make_error_msg(frame, substitute(cfg.settings_t.unknown_name, {template}));
    end
    
    -- Create compatible frame object
    local citation_frame = create_frame(frame, template, args);
    setmetatable(citation_frame, citation_frame);
    
    -- Call zh-wiki's CS1 module
    local success, result = pcall(function()
        return require('Module:Citation/CS1').citation(citation_frame);
    end);
    
    if not success then
        return make_error_msg(frame, '调用引文模块时出错:' .. tostring(result));
    end
    
    return result;
end

--[[--------------------------< E X P O R T S >---------------------------------------------------------------]]
local p = {};

-- Metatable to handle dynamic template calls
setmetatable(p, {
    __index = function(_, template)
        return function(frame)
            return cite(frame, template);
        end
    end
});

return p;