Jump to content

Module:CiteConversionTest

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Dragons flight (talk | contribs) at 06:44, 21 March 2013. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
c = {}

function c.test( frame )
    local target = frame.args[1] or frame.args.target;
    
    local tt = mw.title.new( target );
    
    local content = tt:getContent();
    
    local result = '';
    local template;
    local i = 1;
    for template in string.gmatch( content, '%b{}' ) do
        local mode, param;
        mode, param = string.match( template, '{{%s*cite (%w*)%s*|([^}]*)}}' );
        if mode ~= nil and mode ~= 'quick' then
            result = result .. '\n{{cite compare|mode=' .. mode .. " | " .. param .. "}}\n";
            i = i + 1;
        end        
        mode, param = string.match( template, '{{%s*cite quick%s*|%s*(%w*)%s*|([^}]*)}}' );
        if mode ~= nil then
            result = result .. '\n{{cite compare|mode=' .. mode .. " | " .. param .. "}}\n";
            i = i + 1;
        end        
        if i > 90 then break; end --prevent time outs
    end
    
    return frame:preprocess(result);
end

function c.gather( frame )
    local typ = frame.args[1] or frame.args.mode;
    local start = frame.args[2] or frame.args.start;

    local tt = mw.title.new( start );
    local content = tt:getContent();

    targets = {};
    for w in string.gmatch( content, '%[%[(%w-)%]%]' ) do
        targets[w] = true;
    end    

    local cnt = 0;
    for k in pairs( targets ) do
        local tt = mw.title.new( start );

        cnt = cnt + 1;
        if cnt > 10 then break; end
        
        if tt ~= nil then            
            local content = tt:getContent();
            local template;
            local i = 1;
            for template in string.gmatch( content, '%b{}' ) do
                local mode, param;
                mode, param = string.match( template, '{{%s*cite ' .. typ .. '%s*|([^}]*)}}' );
                if mode ~= nil and mode ~= 'quick' then
                    result = result .. '\n{{cite compare|mode=' .. mode .. " | " .. param .. "}}\n";
                    i = i + 1;
                end        
                mode, param = string.match( template, '{{%s*cite quick%s*|%s*' .. typ .. '%s*|([^}]*)}}' );
                if mode ~= nil then
                    result = result .. '\n{{cite compare|mode=' .. mode .. " | " .. param .. "}}\n";
                    i = i + 1;
                end        
            end 
        end        
    end

    return result;
end


return c;