Модуль:ДанныеНП

Для документации этого модуля может быть создана страница Модуль:ДанныеНП/doc

local p = {}

local function extractParam(content, paramName)
    if not content then return nil end
    -- Поиск параметра на отдельной строке вида |параметр = значение
    local val = mw.ustring.match(content, '\n%s*|%s*' .. paramName .. '%s*=%s*([^\n\r]+)')
    if not val then
        val = mw.ustring.match(content, '[|]%s*' .. paramName .. '%s*=%s*([^|\n\r}]+)')
    end
    if val then
        -- Очистка от комментариев и закрывающих скобок/пайпов
        val = mw.ustring.gsub(val, '<!%-%-.-%-%->', '')
        val = mw.ustring.gsub(val, '%s*[%|}]+%s*$', '')
        val = mw.text.trim(val)
        if val ~= '' and val ~= '-' then
            return val
        end
    end
    return nil
end

function p.get(frame)
    local args = frame.args[1] and frame.args or frame:getParent().args
    local pageName = args[1] or args['страница']
    local paramName = args[2] or args['параметр'] or 'население'
    
    if not pageName or pageName == '' then return '—' end
    
    local title = mw.title.new(pageName)
    if not title or not title.exists then return '—' end
    
    local content = title:getContent()
    if not content then return '—' end
    
    local val = extractParam(content, paramName)
    if not val then return '—' end
    
    -- Форматирование чисел для параметра "население"
    if paramName == 'население' then
        local cleanNum = mw.ustring.gsub(val, '%s+', '')
        cleanNum = mw.ustring.gsub(cleanNum, '&nbsp;', '')
        local num = tonumber(cleanNum)
        if num then
            local formatted = mw.getContentLanguage():formatNum(num)
            local year = extractParam(content, 'год переписи')
            if year and (args['год'] == 'да' or args['с_годом'] == 'да') then
                return formatted .. ' <small style="color:#57606a;">(' .. year .. ')</small>'
            end
            return formatted
        end
    end
    
    return val
end

return p