Jump to content

Module:RDIndex

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Wnt (talk | contribs) at 07:35, 10 March 2013 (Well that's odd. Expected number for second argument to insert??). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p={} -- ultimate planned purpose is to create table rows of Refdesk questions, type, date, answerer including wikilinks in collaboration with a big template
function p.main(frame)
   local args=frame.args
   local parent=frame:getParent()
   local pargs=parent.args
   local input=pargs.input or args.input or "ERROR"
   local label=pargs.label or args.label or "unlabelled"
   local date=pargs.date or args.date or "undated"
   local arcpage=pargs.arcpage or args.arcpage or nil
   local output="no section headings found"

    local cursor, next_cursor, questioner, users, user_table, last_user;
    local input_length = mw.ustring.len( input );
    
    local breakpoints = {};
    local cut, item
    for cut, item in mw.ustring.gmatch( input, "\n()==+(.-)==+%s+" ) do                
        table.insert( breakpoints, cut );        
        table.insert( breakpoints, item );        
    end     
    
    index = 1;
    output = '';
    repeat
        cursor = tonumber( breakpoints[index] );
        title = breakpoints[index + 1];
             
        if cursor == nil then 
            break;
        end
        next_cursor = tonumber( breakpoints[index+2] ) or input_length;
      
        text = mw.ustring.sub(input,cursor,next_cursor-1)
        tt = mw.ustring.match(title,"[=%s]+(.-)[=%s]+$")
        if tt then 
            title=tt 
        else
            tt = mw.ustring.match(title,"UNIQ.-QINU.(.+)")
            if tt then 
                title=tt 
            end
        end
        text = mw.ustring.gsub(text, "%[%[Special:Contributions/", "[[User:")
        questioner = mw.ustring.gsub(mw.ustring.match(text,"%[%[User:(.-)[|%]]") or "","_"," ")
        user_table = {}
        for tt in mw.ustring.gmatch(text,"%[%[User:(.-)[|%]]" ) do
            tt = mw.ustring.gsub(tt,"_"," ")
            table.insert( user_table, tt );
        end
        table.sort( user_table );
        
        last_user = ''
        users = '';
        for index, tt in pairs( user_table ) do
            if tt ~= last_user and tt ~= questioner then
                users = table.concat( {users, "[[User:", tt, "|", tt, "]]", " "} );
            end
            last_user = tt;
        end        
        users = "[[User:" .. questioner .. "|" .. questioner .. "]]" .. " " .. users
        title = mw.ustring.gsub( title, '%[%[.-|(.-)%]%]', '%1' );
        title = mw.ustring.gsub( title, '%[', '' );
        title = mw.ustring.gsub( title, '%]', '' );
        title = mw.ustring.gsub( title, '%b<>', '' );
        
        if arcpage then 
            title= table.concat( {"[[", arcpage, "#", (title or ""), "|", title,  "]]"} );
        else 
            title= title or "" 
        end
        output = table.concat( {output, "\n|-\n|", title, "\n|", next_cursor-cursor, "\n|", label, "\n|", date, "\n|", users} ); -- debug ; many more goodies should be placed here!

        index = index + 2;
    until next_cursor == input_length
  
    return output
end
 
return p