Модуль:Votes

ХӀокху модулах лаьцна хааман Модуль:Votes/doc агӀо кхолла мега

 ---- Этот модуль подсчитывает голоса в секциях голосования
local votes = {}

function votes.count(frame)
    local new_args = votes._getParameters( frame.args, { 'page', 'level', 'support', 'oppose', 'neutral', 'namespace', 'format', 'template', 'unordered' } );
    local page  = new_args['page']  or '';
    local namespace = new_args['namespace']  or '';
    local level = new_args['level'] or 3;
    local fmt =  new_args['format'] or 'row';
    local template = new_args['template'] or '';
    local unordered = new_args['unordered'] or false;
    local sections = {
        support = { name = new_args['support'] or 'Реза', count = 0},
        oppose  = { name = new_args['oppose']  or 'Дуьхьала', count = 0},
        neutral = { name = new_args['neutral'] or 'Юхаозабеларш', count = 0}
    }
    local pagepointer;
    local pattern = "\n#[^#*:][^\n]+"; -- подсчёт нумерованных списков
    if page == '' then
        pagepointer=mw.title.getCurrentTitle()
        assert(pagepointer,"failed to access getCurrentTitle")
    else
        pagepointer=mw.title.new(page, namespace)
        assert(pagepointer,"failed to access mw.title.new("..tostring(page)..")")
    end
    if type( unordered ) == 'string' and unordered ~= 'false' and unordered ~= 'no' and unordered ~= '0' then
    	pattern = "\n%*[^#*:][^\n]+"; -- подсчёт ненумерованных списков
    end 
    local text=pagepointer.getContent(pagepointer);
    if text ~= nil then
	    text= mw.ustring.gsub( text, "<!%-%-.-%-%->", "" ); -- убираем HTML комментарии
	    local hpref = mw.ustring.rep("=", tonumber(level));
	    for k, v in pairs(sections) do 
	    	local name = mw.ustring.gsub( v.name, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" );
	        local t = mw.ustring.match(text, "\n" .. hpref .. "[^=]-" .. name .. "[^=]-" .. hpref .."(\n.-)\n=");
	        if t ~= nil then
	            t, v.count = mw.ustring.gsub(t, pattern, ""); -- количество голосов
	        end
	    end
	end
    local percent;
    if sections.support.count == 0 then
        percent = "0&nbsp;%"
    else
        percent = mw.ustring.format("%.2f&nbsp;%%", sections.support.count * 100 / (sections.support.count + sections.oppose.count))
    end
    if template ~= '' then
    	local targs = {};
    	local pframe = frame:getParent();
    	if pframe ~= nil then
    		for key, value in pairs(pframe.args) do
    			targs[key] =  value;
    		end
    	end
    	targs['реза'] = tostring(sections.support.count);
    	targs['дуьхьала'] = tostring(sections.oppose.count);
    	targs['юхаозабеларш'] = tostring(sections.neutral.count);
    	return frame:expandTemplate{ title = template, args = targs }
    end	
    if fmt == 'mini' then
		return sections.support.count .. '/' .. sections.oppose.count .. '/' .. sections.neutral.count .. '&nbsp;—&nbsp;' .. percent;
    else
		return "\n<tr>" ..
	      "<td>" .. sections.support.count .. " </td>" ..
	      "<td>" .. sections.oppose.count .. " </td>" ..
	      "<td>" .. sections.neutral.count .. " </td>" ..
	      "<td>" .. percent .. " </td>" ..
	     "</tr>";
	end
end

-- Таблица параметров
function votes._getParameters( frame_args, arg_list )
    local new_args = {};
    local index = 1;
    local value;
 
    for i,arg in ipairs( arg_list ) do
        value = frame_args[arg]
        if value == nil then
            value = frame_args[index];
            index = index + 1;
        end
        new_args[arg] = value;
    end
 
    return new_args;
end  

return votes